Adding to linked list struct using a while loop doesn't save the data (IN C)











up vote
0
down vote

favorite












This is one of my first questions so I hope I formulate it well. I checked some other similar questions on StackOverflow but I got no real answers from them. I have the following struct:



struct process {
int priority;
char* path;
char* parameters;
struct process *next;
};


I am reading each line from a file and adding the string tokens I get in my struct linked list using a while loop. Here is the adding method:



struct process * add(int prio,char* pat, char* par,  struct process *head) {
struct process *new_node;
new_node = ( struct process *) malloc(sizeof( struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
head = new_node;
return head;
}


So the main algorithm gets the lines from a file using fgets in a while loop :



while (fgets(line, sizeof(line), file))


then I tokenize all the strings I need and use the add method to add them to my linked list. I convert the first string to int to respect the types.



This is my while loop and main algo :



        FILE *file = fopen(filename , "r");
char line[124];
// can be put outside and passed as argument.
struct process *head = ( struct process *)malloc(sizeof(struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
char* priority,*path,*parameters;

while (fgets(line, sizeof(line), file)) {
priority=strtok(line," ");
// The fix here is the following :
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1);
strcpy(path, path_token);
char *par_token = strtok(NULL, "n");
parameters = malloc(strlen(par_token) + 1);
strcpy(parameters, par_token);
// End of edit Thanks to the answers bellow
char* junk;
int p = strtol(priority,&junk,10);
printf("prio : %d ",p);
head = add(p, path, parameters,head);
printf("n");
pront(head);
printf("n");
}
\ free the mallocs


We notice here that I use the pront() method to print my linked list at every step. I also do it after my while loop. Here is the code for pront():



void pront(struct process *head) {
struct process *current_node = head;
while ( current_node != NULL) {
printf("%d , %s , %s ", current_node->priority, current_node->path, current_node->parameters);
printf("n");
current_node = current_node->next;
}


}



the method prints nonsense :



prio : 2  
2 , ./printchars , a 12 b

prio : 15
15 , ./printchars , c 23 d
2 , ,

prio : 7
7 , ./printchars , e 34 f
15 , /printchars , 34 f
2 , ./printchars , e 34 f


it is supposed to print :



7 , ./printchars , e 34 f 
15 , ./printchars , c 23 d
2 , ./printchars , a 12 b


I am sure that the problem comes from the while loop since when using the adding method outside of the loop and then printing, I get valid results. But as soon as it gets in the loop, the Head linked list doesn't store the values properly!
Thanks for the help



EDIT : The issue is fixed and the issue was my awfully wrong use of strtok and pointers to char's without malloc. Lesson learned !










share|improve this question




















  • 2




    head = NULL;... Doing this inside the algorithm you provided just after you malloc()-ed it... is in fact, flat out wrong!
    – Ruks
    Nov 22 at 6:27












  • @Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
    – Mamoun Debbarh
    Nov 22 at 6:30








  • 1




    No!, you should not use head = NULL; for that! memset(head, 0, 1); is the way... You are trying to leak the object by assigning NULL to it!
    – Ruks
    Nov 22 at 6:33












  • However, it's wrong. struct process *head = ( struct process *)malloc(sizeof(struct process)); allocates memory and stores it's address in head. head = NULL; overrides head. Now, the just allocated memory is lost - it's a memory leak.
    – Scheff
    Nov 22 at 6:33










  • path and parameters are pointers. What are they pointing at? Do path and parameters from different struct process point at different places? What are these places?
    – n.m.
    Nov 22 at 6:35

















up vote
0
down vote

favorite












This is one of my first questions so I hope I formulate it well. I checked some other similar questions on StackOverflow but I got no real answers from them. I have the following struct:



struct process {
int priority;
char* path;
char* parameters;
struct process *next;
};


I am reading each line from a file and adding the string tokens I get in my struct linked list using a while loop. Here is the adding method:



struct process * add(int prio,char* pat, char* par,  struct process *head) {
struct process *new_node;
new_node = ( struct process *) malloc(sizeof( struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
head = new_node;
return head;
}


So the main algorithm gets the lines from a file using fgets in a while loop :



while (fgets(line, sizeof(line), file))


then I tokenize all the strings I need and use the add method to add them to my linked list. I convert the first string to int to respect the types.



This is my while loop and main algo :



        FILE *file = fopen(filename , "r");
char line[124];
// can be put outside and passed as argument.
struct process *head = ( struct process *)malloc(sizeof(struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
char* priority,*path,*parameters;

while (fgets(line, sizeof(line), file)) {
priority=strtok(line," ");
// The fix here is the following :
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1);
strcpy(path, path_token);
char *par_token = strtok(NULL, "n");
parameters = malloc(strlen(par_token) + 1);
strcpy(parameters, par_token);
// End of edit Thanks to the answers bellow
char* junk;
int p = strtol(priority,&junk,10);
printf("prio : %d ",p);
head = add(p, path, parameters,head);
printf("n");
pront(head);
printf("n");
}
\ free the mallocs


We notice here that I use the pront() method to print my linked list at every step. I also do it after my while loop. Here is the code for pront():



void pront(struct process *head) {
struct process *current_node = head;
while ( current_node != NULL) {
printf("%d , %s , %s ", current_node->priority, current_node->path, current_node->parameters);
printf("n");
current_node = current_node->next;
}


}



the method prints nonsense :



prio : 2  
2 , ./printchars , a 12 b

prio : 15
15 , ./printchars , c 23 d
2 , ,

prio : 7
7 , ./printchars , e 34 f
15 , /printchars , 34 f
2 , ./printchars , e 34 f


it is supposed to print :



7 , ./printchars , e 34 f 
15 , ./printchars , c 23 d
2 , ./printchars , a 12 b


I am sure that the problem comes from the while loop since when using the adding method outside of the loop and then printing, I get valid results. But as soon as it gets in the loop, the Head linked list doesn't store the values properly!
Thanks for the help



EDIT : The issue is fixed and the issue was my awfully wrong use of strtok and pointers to char's without malloc. Lesson learned !










share|improve this question




















  • 2




    head = NULL;... Doing this inside the algorithm you provided just after you malloc()-ed it... is in fact, flat out wrong!
    – Ruks
    Nov 22 at 6:27












  • @Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
    – Mamoun Debbarh
    Nov 22 at 6:30








  • 1




    No!, you should not use head = NULL; for that! memset(head, 0, 1); is the way... You are trying to leak the object by assigning NULL to it!
    – Ruks
    Nov 22 at 6:33












  • However, it's wrong. struct process *head = ( struct process *)malloc(sizeof(struct process)); allocates memory and stores it's address in head. head = NULL; overrides head. Now, the just allocated memory is lost - it's a memory leak.
    – Scheff
    Nov 22 at 6:33










  • path and parameters are pointers. What are they pointing at? Do path and parameters from different struct process point at different places? What are these places?
    – n.m.
    Nov 22 at 6:35















up vote
0
down vote

favorite









up vote
0
down vote

favorite











This is one of my first questions so I hope I formulate it well. I checked some other similar questions on StackOverflow but I got no real answers from them. I have the following struct:



struct process {
int priority;
char* path;
char* parameters;
struct process *next;
};


I am reading each line from a file and adding the string tokens I get in my struct linked list using a while loop. Here is the adding method:



struct process * add(int prio,char* pat, char* par,  struct process *head) {
struct process *new_node;
new_node = ( struct process *) malloc(sizeof( struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
head = new_node;
return head;
}


So the main algorithm gets the lines from a file using fgets in a while loop :



while (fgets(line, sizeof(line), file))


then I tokenize all the strings I need and use the add method to add them to my linked list. I convert the first string to int to respect the types.



This is my while loop and main algo :



        FILE *file = fopen(filename , "r");
char line[124];
// can be put outside and passed as argument.
struct process *head = ( struct process *)malloc(sizeof(struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
char* priority,*path,*parameters;

while (fgets(line, sizeof(line), file)) {
priority=strtok(line," ");
// The fix here is the following :
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1);
strcpy(path, path_token);
char *par_token = strtok(NULL, "n");
parameters = malloc(strlen(par_token) + 1);
strcpy(parameters, par_token);
// End of edit Thanks to the answers bellow
char* junk;
int p = strtol(priority,&junk,10);
printf("prio : %d ",p);
head = add(p, path, parameters,head);
printf("n");
pront(head);
printf("n");
}
\ free the mallocs


We notice here that I use the pront() method to print my linked list at every step. I also do it after my while loop. Here is the code for pront():



void pront(struct process *head) {
struct process *current_node = head;
while ( current_node != NULL) {
printf("%d , %s , %s ", current_node->priority, current_node->path, current_node->parameters);
printf("n");
current_node = current_node->next;
}


}



the method prints nonsense :



prio : 2  
2 , ./printchars , a 12 b

prio : 15
15 , ./printchars , c 23 d
2 , ,

prio : 7
7 , ./printchars , e 34 f
15 , /printchars , 34 f
2 , ./printchars , e 34 f


it is supposed to print :



7 , ./printchars , e 34 f 
15 , ./printchars , c 23 d
2 , ./printchars , a 12 b


I am sure that the problem comes from the while loop since when using the adding method outside of the loop and then printing, I get valid results. But as soon as it gets in the loop, the Head linked list doesn't store the values properly!
Thanks for the help



EDIT : The issue is fixed and the issue was my awfully wrong use of strtok and pointers to char's without malloc. Lesson learned !










share|improve this question















This is one of my first questions so I hope I formulate it well. I checked some other similar questions on StackOverflow but I got no real answers from them. I have the following struct:



struct process {
int priority;
char* path;
char* parameters;
struct process *next;
};


I am reading each line from a file and adding the string tokens I get in my struct linked list using a while loop. Here is the adding method:



struct process * add(int prio,char* pat, char* par,  struct process *head) {
struct process *new_node;
new_node = ( struct process *) malloc(sizeof( struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
head = new_node;
return head;
}


So the main algorithm gets the lines from a file using fgets in a while loop :



while (fgets(line, sizeof(line), file))


then I tokenize all the strings I need and use the add method to add them to my linked list. I convert the first string to int to respect the types.



This is my while loop and main algo :



        FILE *file = fopen(filename , "r");
char line[124];
// can be put outside and passed as argument.
struct process *head = ( struct process *)malloc(sizeof(struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
char* priority,*path,*parameters;

while (fgets(line, sizeof(line), file)) {
priority=strtok(line," ");
// The fix here is the following :
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1);
strcpy(path, path_token);
char *par_token = strtok(NULL, "n");
parameters = malloc(strlen(par_token) + 1);
strcpy(parameters, par_token);
// End of edit Thanks to the answers bellow
char* junk;
int p = strtol(priority,&junk,10);
printf("prio : %d ",p);
head = add(p, path, parameters,head);
printf("n");
pront(head);
printf("n");
}
\ free the mallocs


We notice here that I use the pront() method to print my linked list at every step. I also do it after my while loop. Here is the code for pront():



void pront(struct process *head) {
struct process *current_node = head;
while ( current_node != NULL) {
printf("%d , %s , %s ", current_node->priority, current_node->path, current_node->parameters);
printf("n");
current_node = current_node->next;
}


}



the method prints nonsense :



prio : 2  
2 , ./printchars , a 12 b

prio : 15
15 , ./printchars , c 23 d
2 , ,

prio : 7
7 , ./printchars , e 34 f
15 , /printchars , 34 f
2 , ./printchars , e 34 f


it is supposed to print :



7 , ./printchars , e 34 f 
15 , ./printchars , c 23 d
2 , ./printchars , a 12 b


I am sure that the problem comes from the while loop since when using the adding method outside of the loop and then printing, I get valid results. But as soon as it gets in the loop, the Head linked list doesn't store the values properly!
Thanks for the help



EDIT : The issue is fixed and the issue was my awfully wrong use of strtok and pointers to char's without malloc. Lesson learned !







c pointers struct while-loop linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 7:01

























asked Nov 22 at 6:23









Mamoun Debbarh

84




84








  • 2




    head = NULL;... Doing this inside the algorithm you provided just after you malloc()-ed it... is in fact, flat out wrong!
    – Ruks
    Nov 22 at 6:27












  • @Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
    – Mamoun Debbarh
    Nov 22 at 6:30








  • 1




    No!, you should not use head = NULL; for that! memset(head, 0, 1); is the way... You are trying to leak the object by assigning NULL to it!
    – Ruks
    Nov 22 at 6:33












  • However, it's wrong. struct process *head = ( struct process *)malloc(sizeof(struct process)); allocates memory and stores it's address in head. head = NULL; overrides head. Now, the just allocated memory is lost - it's a memory leak.
    – Scheff
    Nov 22 at 6:33










  • path and parameters are pointers. What are they pointing at? Do path and parameters from different struct process point at different places? What are these places?
    – n.m.
    Nov 22 at 6:35
















  • 2




    head = NULL;... Doing this inside the algorithm you provided just after you malloc()-ed it... is in fact, flat out wrong!
    – Ruks
    Nov 22 at 6:27












  • @Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
    – Mamoun Debbarh
    Nov 22 at 6:30








  • 1




    No!, you should not use head = NULL; for that! memset(head, 0, 1); is the way... You are trying to leak the object by assigning NULL to it!
    – Ruks
    Nov 22 at 6:33












  • However, it's wrong. struct process *head = ( struct process *)malloc(sizeof(struct process)); allocates memory and stores it's address in head. head = NULL; overrides head. Now, the just allocated memory is lost - it's a memory leak.
    – Scheff
    Nov 22 at 6:33










  • path and parameters are pointers. What are they pointing at? Do path and parameters from different struct process point at different places? What are these places?
    – n.m.
    Nov 22 at 6:35










2




2




head = NULL;... Doing this inside the algorithm you provided just after you malloc()-ed it... is in fact, flat out wrong!
– Ruks
Nov 22 at 6:27






head = NULL;... Doing this inside the algorithm you provided just after you malloc()-ed it... is in fact, flat out wrong!
– Ruks
Nov 22 at 6:27














@Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
– Mamoun Debbarh
Nov 22 at 6:30






@Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
– Mamoun Debbarh
Nov 22 at 6:30






1




1




No!, you should not use head = NULL; for that! memset(head, 0, 1); is the way... You are trying to leak the object by assigning NULL to it!
– Ruks
Nov 22 at 6:33






No!, you should not use head = NULL; for that! memset(head, 0, 1); is the way... You are trying to leak the object by assigning NULL to it!
– Ruks
Nov 22 at 6:33














However, it's wrong. struct process *head = ( struct process *)malloc(sizeof(struct process)); allocates memory and stores it's address in head. head = NULL; overrides head. Now, the just allocated memory is lost - it's a memory leak.
– Scheff
Nov 22 at 6:33




However, it's wrong. struct process *head = ( struct process *)malloc(sizeof(struct process)); allocates memory and stores it's address in head. head = NULL; overrides head. Now, the just allocated memory is lost - it's a memory leak.
– Scheff
Nov 22 at 6:33












path and parameters are pointers. What are they pointing at? Do path and parameters from different struct process point at different places? What are these places?
– n.m.
Nov 22 at 6:35






path and parameters are pointers. What are they pointing at? Do path and parameters from different struct process point at different places? What are these places?
– n.m.
Nov 22 at 6:35














2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Your linked list does not store the values at all.



The issue isn't the loop, but strtok - you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.



So, to save the values, you must allocate memory for your strings (path and parameters) and use strcpy to copy the strings to the allocated memory.
Remember to free each item fields before freeing list items!



Example:



char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));





share|improve this answer





















  • This worked and Your explanation was clear! thanks
    – Mamoun Debbarh
    Nov 22 at 6:55


















up vote
2
down vote













Issue is with below code.



new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;


Your structure has a character pointer for path and parameters



What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat or par changes value or has some garbage value, the structure element will also have garbage value.



What you need to do is to allocate memory for each element and strcpy the data



Additionally, as pointed out by @Ruks -



struct process *head = ( struct process *)malloc(sizeof(struct process)); 
head = NULL;


is wrong. You are loosing the pointer returned by malloc. If you want to ensure that the linked list is empty initially, you should initialise all elements of head.



head-> priority = 0; 
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;





share|improve this answer























  • I'm trying a solution right now with what you suggest here! I'll be back in 5!
    – Mamoun Debbarh
    Nov 22 at 6:41










  • This helped a lot. Thanks man!
    – Mamoun Debbarh
    Nov 22 at 6:55











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424977%2fadding-to-linked-list-struct-using-a-while-loop-doesnt-save-the-data-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Your linked list does not store the values at all.



The issue isn't the loop, but strtok - you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.



So, to save the values, you must allocate memory for your strings (path and parameters) and use strcpy to copy the strings to the allocated memory.
Remember to free each item fields before freeing list items!



Example:



char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));





share|improve this answer





















  • This worked and Your explanation was clear! thanks
    – Mamoun Debbarh
    Nov 22 at 6:55















up vote
1
down vote



accepted










Your linked list does not store the values at all.



The issue isn't the loop, but strtok - you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.



So, to save the values, you must allocate memory for your strings (path and parameters) and use strcpy to copy the strings to the allocated memory.
Remember to free each item fields before freeing list items!



Example:



char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));





share|improve this answer





















  • This worked and Your explanation was clear! thanks
    – Mamoun Debbarh
    Nov 22 at 6:55













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Your linked list does not store the values at all.



The issue isn't the loop, but strtok - you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.



So, to save the values, you must allocate memory for your strings (path and parameters) and use strcpy to copy the strings to the allocated memory.
Remember to free each item fields before freeing list items!



Example:



char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));





share|improve this answer












Your linked list does not store the values at all.



The issue isn't the loop, but strtok - you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.



So, to save the values, you must allocate memory for your strings (path and parameters) and use strcpy to copy the strings to the allocated memory.
Remember to free each item fields before freeing list items!



Example:



char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 6:38









Lev M.

44918




44918












  • This worked and Your explanation was clear! thanks
    – Mamoun Debbarh
    Nov 22 at 6:55


















  • This worked and Your explanation was clear! thanks
    – Mamoun Debbarh
    Nov 22 at 6:55
















This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55




This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55












up vote
2
down vote













Issue is with below code.



new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;


Your structure has a character pointer for path and parameters



What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat or par changes value or has some garbage value, the structure element will also have garbage value.



What you need to do is to allocate memory for each element and strcpy the data



Additionally, as pointed out by @Ruks -



struct process *head = ( struct process *)malloc(sizeof(struct process)); 
head = NULL;


is wrong. You are loosing the pointer returned by malloc. If you want to ensure that the linked list is empty initially, you should initialise all elements of head.



head-> priority = 0; 
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;





share|improve this answer























  • I'm trying a solution right now with what you suggest here! I'll be back in 5!
    – Mamoun Debbarh
    Nov 22 at 6:41










  • This helped a lot. Thanks man!
    – Mamoun Debbarh
    Nov 22 at 6:55















up vote
2
down vote













Issue is with below code.



new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;


Your structure has a character pointer for path and parameters



What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat or par changes value or has some garbage value, the structure element will also have garbage value.



What you need to do is to allocate memory for each element and strcpy the data



Additionally, as pointed out by @Ruks -



struct process *head = ( struct process *)malloc(sizeof(struct process)); 
head = NULL;


is wrong. You are loosing the pointer returned by malloc. If you want to ensure that the linked list is empty initially, you should initialise all elements of head.



head-> priority = 0; 
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;





share|improve this answer























  • I'm trying a solution right now with what you suggest here! I'll be back in 5!
    – Mamoun Debbarh
    Nov 22 at 6:41










  • This helped a lot. Thanks man!
    – Mamoun Debbarh
    Nov 22 at 6:55













up vote
2
down vote










up vote
2
down vote









Issue is with below code.



new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;


Your structure has a character pointer for path and parameters



What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat or par changes value or has some garbage value, the structure element will also have garbage value.



What you need to do is to allocate memory for each element and strcpy the data



Additionally, as pointed out by @Ruks -



struct process *head = ( struct process *)malloc(sizeof(struct process)); 
head = NULL;


is wrong. You are loosing the pointer returned by malloc. If you want to ensure that the linked list is empty initially, you should initialise all elements of head.



head-> priority = 0; 
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;





share|improve this answer














Issue is with below code.



new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;


Your structure has a character pointer for path and parameters



What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat or par changes value or has some garbage value, the structure element will also have garbage value.



What you need to do is to allocate memory for each element and strcpy the data



Additionally, as pointed out by @Ruks -



struct process *head = ( struct process *)malloc(sizeof(struct process)); 
head = NULL;


is wrong. You are loosing the pointer returned by malloc. If you want to ensure that the linked list is empty initially, you should initialise all elements of head.



head-> priority = 0; 
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 6:50

























answered Nov 22 at 6:35









Rishikesh Raje

5,4501826




5,4501826












  • I'm trying a solution right now with what you suggest here! I'll be back in 5!
    – Mamoun Debbarh
    Nov 22 at 6:41










  • This helped a lot. Thanks man!
    – Mamoun Debbarh
    Nov 22 at 6:55


















  • I'm trying a solution right now with what you suggest here! I'll be back in 5!
    – Mamoun Debbarh
    Nov 22 at 6:41










  • This helped a lot. Thanks man!
    – Mamoun Debbarh
    Nov 22 at 6:55
















I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41




I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41












This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55




This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424977%2fadding-to-linked-list-struct-using-a-while-loop-doesnt-save-the-data-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)