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?

Allocate enough room for the string and if successful strcpy it.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.