0

If I have a struct defined as

struct node {
        char *data;
        struct node *next;
};
typedef struct node* nodePtr;
nodePtr newNode;
...
int main(void) {
newNode = (nodePtr *) malloc(sizeof (nodePtr)); //dynamically allocate memory
        if(newNode == NULL) {
                printf("Error: could not allocate a new node\n");
                exit (1);
        }
        ...

and read in a string with fgets that I want to assign to data of the current node, what does the line look like?

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by sukhmal.sena
0
struct node {
        char *data;
        struct node *next;
};
typedef struct node* nodePtr;
nodePtr newNode;
...
int main(void) {
newNode = (nodePtr *) malloc(sizeof (nodePtr)); //dynamically allocate

In the above code there is a problem in the last line:

struct node {
        char *data;
        struct node *next;
};
typedef struct node* nodePtr;
nodePtr newNode;
...
int main(void) {
newNode = (nodePtr) malloc(sizeof (struct node)); //dynamically allocate

code in red is the correct code.

;)
/Sukhmal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.