Jawad Khutaba Jawad Khutaba -4 years ago 62
C Question

completing this code (c) if you can

#include<stdlib.h>
typedef struct node
{
int data;
int help;
struct node* next;
}Node;
void Nodes_maker(int nums,Node *currentnode);
int main()
{
int count2;
Node* root;
Node* currentnode;
currentnode=root;
printf("How many numbers do you want? ");
scanf("%d",&count2);
Nodes_maker(count2,&currentnode);
return 0;
}
void Nodes_maker(int nums,Node *currentnode)
{
int i;
for(i=0;i<nums;i++)
{
currentnode->next=(Node*)malloc(sizeof(Node));
}
}


would someone help me complete this code?
i have the Node struct that contains 'data','help','next'.
i want to scanf a number from the user about how many numbers he wants (how many 'data' fields he wants) and making those Node structs (The 'next' field contains a pointer to another new 'data' field in another Node struct.

Answer Source

Change this:

Nodes_maker(count2,&currentnode);

to this:

Nodes_maker(count2, currentnode);

and the error will go away. That is because the prototype of the function is Nodes_maker(int nums,Node *currentnode) and you have Node* currentnode;.

However, you need to work on your logic. I mean you dynamically allocate memory, but you don't return the pointer (your function returns void). Good luck!


PS: Do I cast the result of malloc? No.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download