0

this is a code about push strings and pop strings from a stack, the string name must be fixed (not input by user). But the code is unable to run (it cannot display anything). And the point is because strcpy(temp->id,val);, if i get rid of strcpy(temp->id,val);, then the code can be run properly (but printf unreadable characters). Can I know whats is the problem with my strcpy?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct LLNode
{
    char id[4];
    struct LLNode *next;
};

struct LLNode * createNode(char val[])
{
    struct LLNode *temp;
    temp=(struct LLNode *)malloc(sizeof(struct LLNode));
    strcpy(temp->id,val);
    temp-> id[4] = val;
    temp-> next = NULL;
    return (temp);
};

char push(char val[], struct LLNode *head)
{
    struct LLNode *temp;
    temp = createNode(val);
    temp->next = head->next;
    head->next = temp;
};

char pop(struct LLNode *head)
{
    struct LLNode *temp;
    char val;
    val = head->next->id;
    temp = head->next;
    head->next = head->next->next;
    free(temp);
    return(val);
};

int main()
{
    char value;
    struct LLNode *head = NULL;
    struct LLNode *tail = NULL;
    struct LLNode *curr;

    head = createNode('\0');
    tail = createNode('\0');

    char id[6][4] = {"A123","B234","C345","D456","E567","F678"};
    int n;

    for (int i=0;i<6;i++)
    {
        push(id[i],&head->id);
        printf("%s\n",&head->id);
    }


};
Scarlet
  • 1
  • 1
  • 2
    Your `id` field and array do not have enough space for the null-terminators. – Eugene Sh. Nov 30 '20 at 15:06
  • `head = createNode('\0');` - is invalid, and the compiler should have warned you. This function expects a string, but you pass single `char`. Replace with `head = createNode("");` – Eugene Sh. Nov 30 '20 at 15:17
  • @EugeneSh. yes I'd replaced but the code still display empty result (with `strcpy(temp->id,val);`) or unreadable characters (without `strcpy(temp->id,val);`) Just to make sure, is my `strcpy(temp->id,val);` correct? – Scarlet Nov 30 '20 at 15:23
  • Also see: https://stackoverflow.com/q/605845/1216776 – stark Nov 30 '20 at 16:46

1 Answers1

0
char id[4];

char id[6][4] = {"A123","B234","C345","D456","E567","F678"};

A string is a nul-terminated sequence of non-nul elements including the terminator.

You are always forgetting that small but significant terminator.

And while that works fine when initializing an array with explicit length with a string-literal, as in you may drop the implicit terminator that way, remember that you thereafter don't have a string.

Anyway, your nodes arrays don't take advantage of that rule, and are also too short.


struct LLNode * createNode(char val[]);

head = createNode('\0');

A function that expects a string should not be fed a single character. Worse still, '\0' is a legitimate null-pointer-literal. Even though that's not what you wanted.


temp-> id[4] = val;

Assigning a pointer to past-the-end of a character array is beyond the pale. Are you requesting warnings from the compiler, and adherence to a specific standard? For gcc / clang, I suggest at least -Wextra -std=c99 -pedantic.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118