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);
}
};