Inserting Node Linked List C





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to create a linked list which stores name and age of a student.
I am having trouble with insertion.



#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct node{

char Name[50];
int studentAge;
struct node* next;

}MyNode;


this is how i defined my Struct which constains the requried data and a pointer 'next' which points to the next node.



Below is my insertion function
so in the first if condition i am saying if there isnt a head ie head = NULL then create memory space for the head using malloc.. after this i copy all the data into the head node and making sure that the next of head points to null.



In the second condition i am saying if there is a head ie Head ! = NULL
then traverse the list to the end using the current pointer and then copy all the data in.



void InsertStudent(char givenName[50], int age, MyNode* head){

if(head == NULL){
head = (MyNode*) malloc(sizeof(MyNode));
strcpy(head->Name,givenName);
head->studentAge = age;
head->next = NULL;
}


if(head != NULL){
MyNode* current = head;
while(current->next != NULL){
current = current->next;
}
current->next = (MyNode*) malloc(sizeof(MyNode));
strcpy(current->next->Name,givenName);
current->next->studentAge = age;
current->next->next = NULL;
}

}


Now i am not sure if there is a problem in my printing or inserting because it doesn't print my nodes when i try the code out



void PrintList(MyNode* head){
MyNode* current = head;

while(current != NULL){
printf("Name is %s Age is %dn",current->Name,current->studentAge);
current = current->next;
}

}


this is my main function.. is there a problem with the MyNode* head = NULL; line of code is that allowed?



  int main()
{


MyNode* head = NULL;

int r = 0;
while(r!=1)
{
printf("Data Structures - Linked Listn");
printf("Choose one Option:nn");
printf("1.Insert Studentn");
printf("2.Remove Studentn");
printf("3.Print all studentn");
printf("4.Exitn");

int option=0;
char givenName[50];
int givenAge;
scanf("%d",&option);

switch(option){

case 1:
printf("Enter name of student: ");
scanf("%s",givenName);
printf("nEnter Age of student: ");
scanf("%d",&givenAge);
InsertStudent(givenName,givenAge,head);
break;

case 2:
printf("Enter name of student: ");
scanf("%s",givenName);
printf("nEnter Age of student: ");
scanf("%d",&givenAge);
RemoveStudent(givenName,givenAge);
break;

case 3:
PrintList(head);
break;
case 4:
r=1;
break;
default:
r=1;
printf("nNot an optionn");
break;

}

}
}









share|improve this question


















  • 1





    head = (MyNode*) malloc(sizeof(MyNode)) in InsertStudent means nothing to the caller of that function. The head pointer is passed by value. Assigning to it as such just modifies a local variable; the caller's pointer remains unchanged. Either utilize the otherwise-unused return value of the function to communicate a possibly-updated head pointer, or pass the head pointer by address (so a pointer-to-pointer) and change the code in InsertStudent accordingly. There are at least a thousand duplicates of this problem on SO. I'll try and hunt one down.

    – WhozCraig
    Nov 23 '18 at 20:50













  • Found one here. The first and second answers actually demonstrate both methods I described above.

    – WhozCraig
    Nov 23 '18 at 20:52













  • from what i have seen, are you trying to suggesting to either change void Insert(..) to MyNode* insert(...) and return the new node... or the second method is to use void ( double pointer)... is that what your saying? i would like to go with double pointers as that seems harder but i don't really know how to do it..

    – EEECS
    Nov 23 '18 at 20:55











  • The comment I posted, in conjunction with the answer and prior question I linked, cannot possibly describe better what needs to be done. The second answer in the linked question shows precisely how to do this with pointer-to-pointer syntax.

    – WhozCraig
    Nov 23 '18 at 20:56











  • thanks. ill fix it now. i just saw ur second post so i didnt know you linked an example

    – EEECS
    Nov 23 '18 at 20:59




















1















I am trying to create a linked list which stores name and age of a student.
I am having trouble with insertion.



#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct node{

char Name[50];
int studentAge;
struct node* next;

}MyNode;


this is how i defined my Struct which constains the requried data and a pointer 'next' which points to the next node.



Below is my insertion function
so in the first if condition i am saying if there isnt a head ie head = NULL then create memory space for the head using malloc.. after this i copy all the data into the head node and making sure that the next of head points to null.



In the second condition i am saying if there is a head ie Head ! = NULL
then traverse the list to the end using the current pointer and then copy all the data in.



void InsertStudent(char givenName[50], int age, MyNode* head){

if(head == NULL){
head = (MyNode*) malloc(sizeof(MyNode));
strcpy(head->Name,givenName);
head->studentAge = age;
head->next = NULL;
}


if(head != NULL){
MyNode* current = head;
while(current->next != NULL){
current = current->next;
}
current->next = (MyNode*) malloc(sizeof(MyNode));
strcpy(current->next->Name,givenName);
current->next->studentAge = age;
current->next->next = NULL;
}

}


Now i am not sure if there is a problem in my printing or inserting because it doesn't print my nodes when i try the code out



void PrintList(MyNode* head){
MyNode* current = head;

while(current != NULL){
printf("Name is %s Age is %dn",current->Name,current->studentAge);
current = current->next;
}

}


this is my main function.. is there a problem with the MyNode* head = NULL; line of code is that allowed?



  int main()
{


MyNode* head = NULL;

int r = 0;
while(r!=1)
{
printf("Data Structures - Linked Listn");
printf("Choose one Option:nn");
printf("1.Insert Studentn");
printf("2.Remove Studentn");
printf("3.Print all studentn");
printf("4.Exitn");

int option=0;
char givenName[50];
int givenAge;
scanf("%d",&option);

switch(option){

case 1:
printf("Enter name of student: ");
scanf("%s",givenName);
printf("nEnter Age of student: ");
scanf("%d",&givenAge);
InsertStudent(givenName,givenAge,head);
break;

case 2:
printf("Enter name of student: ");
scanf("%s",givenName);
printf("nEnter Age of student: ");
scanf("%d",&givenAge);
RemoveStudent(givenName,givenAge);
break;

case 3:
PrintList(head);
break;
case 4:
r=1;
break;
default:
r=1;
printf("nNot an optionn");
break;

}

}
}









share|improve this question


















  • 1





    head = (MyNode*) malloc(sizeof(MyNode)) in InsertStudent means nothing to the caller of that function. The head pointer is passed by value. Assigning to it as such just modifies a local variable; the caller's pointer remains unchanged. Either utilize the otherwise-unused return value of the function to communicate a possibly-updated head pointer, or pass the head pointer by address (so a pointer-to-pointer) and change the code in InsertStudent accordingly. There are at least a thousand duplicates of this problem on SO. I'll try and hunt one down.

    – WhozCraig
    Nov 23 '18 at 20:50













  • Found one here. The first and second answers actually demonstrate both methods I described above.

    – WhozCraig
    Nov 23 '18 at 20:52













  • from what i have seen, are you trying to suggesting to either change void Insert(..) to MyNode* insert(...) and return the new node... or the second method is to use void ( double pointer)... is that what your saying? i would like to go with double pointers as that seems harder but i don't really know how to do it..

    – EEECS
    Nov 23 '18 at 20:55











  • The comment I posted, in conjunction with the answer and prior question I linked, cannot possibly describe better what needs to be done. The second answer in the linked question shows precisely how to do this with pointer-to-pointer syntax.

    – WhozCraig
    Nov 23 '18 at 20:56











  • thanks. ill fix it now. i just saw ur second post so i didnt know you linked an example

    – EEECS
    Nov 23 '18 at 20:59
















1












1








1








I am trying to create a linked list which stores name and age of a student.
I am having trouble with insertion.



#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct node{

char Name[50];
int studentAge;
struct node* next;

}MyNode;


this is how i defined my Struct which constains the requried data and a pointer 'next' which points to the next node.



Below is my insertion function
so in the first if condition i am saying if there isnt a head ie head = NULL then create memory space for the head using malloc.. after this i copy all the data into the head node and making sure that the next of head points to null.



In the second condition i am saying if there is a head ie Head ! = NULL
then traverse the list to the end using the current pointer and then copy all the data in.



void InsertStudent(char givenName[50], int age, MyNode* head){

if(head == NULL){
head = (MyNode*) malloc(sizeof(MyNode));
strcpy(head->Name,givenName);
head->studentAge = age;
head->next = NULL;
}


if(head != NULL){
MyNode* current = head;
while(current->next != NULL){
current = current->next;
}
current->next = (MyNode*) malloc(sizeof(MyNode));
strcpy(current->next->Name,givenName);
current->next->studentAge = age;
current->next->next = NULL;
}

}


Now i am not sure if there is a problem in my printing or inserting because it doesn't print my nodes when i try the code out



void PrintList(MyNode* head){
MyNode* current = head;

while(current != NULL){
printf("Name is %s Age is %dn",current->Name,current->studentAge);
current = current->next;
}

}


this is my main function.. is there a problem with the MyNode* head = NULL; line of code is that allowed?



  int main()
{


MyNode* head = NULL;

int r = 0;
while(r!=1)
{
printf("Data Structures - Linked Listn");
printf("Choose one Option:nn");
printf("1.Insert Studentn");
printf("2.Remove Studentn");
printf("3.Print all studentn");
printf("4.Exitn");

int option=0;
char givenName[50];
int givenAge;
scanf("%d",&option);

switch(option){

case 1:
printf("Enter name of student: ");
scanf("%s",givenName);
printf("nEnter Age of student: ");
scanf("%d",&givenAge);
InsertStudent(givenName,givenAge,head);
break;

case 2:
printf("Enter name of student: ");
scanf("%s",givenName);
printf("nEnter Age of student: ");
scanf("%d",&givenAge);
RemoveStudent(givenName,givenAge);
break;

case 3:
PrintList(head);
break;
case 4:
r=1;
break;
default:
r=1;
printf("nNot an optionn");
break;

}

}
}









share|improve this question














I am trying to create a linked list which stores name and age of a student.
I am having trouble with insertion.



#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct node{

char Name[50];
int studentAge;
struct node* next;

}MyNode;


this is how i defined my Struct which constains the requried data and a pointer 'next' which points to the next node.



Below is my insertion function
so in the first if condition i am saying if there isnt a head ie head = NULL then create memory space for the head using malloc.. after this i copy all the data into the head node and making sure that the next of head points to null.



In the second condition i am saying if there is a head ie Head ! = NULL
then traverse the list to the end using the current pointer and then copy all the data in.



void InsertStudent(char givenName[50], int age, MyNode* head){

if(head == NULL){
head = (MyNode*) malloc(sizeof(MyNode));
strcpy(head->Name,givenName);
head->studentAge = age;
head->next = NULL;
}


if(head != NULL){
MyNode* current = head;
while(current->next != NULL){
current = current->next;
}
current->next = (MyNode*) malloc(sizeof(MyNode));
strcpy(current->next->Name,givenName);
current->next->studentAge = age;
current->next->next = NULL;
}

}


Now i am not sure if there is a problem in my printing or inserting because it doesn't print my nodes when i try the code out



void PrintList(MyNode* head){
MyNode* current = head;

while(current != NULL){
printf("Name is %s Age is %dn",current->Name,current->studentAge);
current = current->next;
}

}


this is my main function.. is there a problem with the MyNode* head = NULL; line of code is that allowed?



  int main()
{


MyNode* head = NULL;

int r = 0;
while(r!=1)
{
printf("Data Structures - Linked Listn");
printf("Choose one Option:nn");
printf("1.Insert Studentn");
printf("2.Remove Studentn");
printf("3.Print all studentn");
printf("4.Exitn");

int option=0;
char givenName[50];
int givenAge;
scanf("%d",&option);

switch(option){

case 1:
printf("Enter name of student: ");
scanf("%s",givenName);
printf("nEnter Age of student: ");
scanf("%d",&givenAge);
InsertStudent(givenName,givenAge,head);
break;

case 2:
printf("Enter name of student: ");
scanf("%s",givenName);
printf("nEnter Age of student: ");
scanf("%d",&givenAge);
RemoveStudent(givenName,givenAge);
break;

case 3:
PrintList(head);
break;
case 4:
r=1;
break;
default:
r=1;
printf("nNot an optionn");
break;

}

}
}






c printing linked-list insertion






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 20:42









EEECSEEECS

82




82








  • 1





    head = (MyNode*) malloc(sizeof(MyNode)) in InsertStudent means nothing to the caller of that function. The head pointer is passed by value. Assigning to it as such just modifies a local variable; the caller's pointer remains unchanged. Either utilize the otherwise-unused return value of the function to communicate a possibly-updated head pointer, or pass the head pointer by address (so a pointer-to-pointer) and change the code in InsertStudent accordingly. There are at least a thousand duplicates of this problem on SO. I'll try and hunt one down.

    – WhozCraig
    Nov 23 '18 at 20:50













  • Found one here. The first and second answers actually demonstrate both methods I described above.

    – WhozCraig
    Nov 23 '18 at 20:52













  • from what i have seen, are you trying to suggesting to either change void Insert(..) to MyNode* insert(...) and return the new node... or the second method is to use void ( double pointer)... is that what your saying? i would like to go with double pointers as that seems harder but i don't really know how to do it..

    – EEECS
    Nov 23 '18 at 20:55











  • The comment I posted, in conjunction with the answer and prior question I linked, cannot possibly describe better what needs to be done. The second answer in the linked question shows precisely how to do this with pointer-to-pointer syntax.

    – WhozCraig
    Nov 23 '18 at 20:56











  • thanks. ill fix it now. i just saw ur second post so i didnt know you linked an example

    – EEECS
    Nov 23 '18 at 20:59
















  • 1





    head = (MyNode*) malloc(sizeof(MyNode)) in InsertStudent means nothing to the caller of that function. The head pointer is passed by value. Assigning to it as such just modifies a local variable; the caller's pointer remains unchanged. Either utilize the otherwise-unused return value of the function to communicate a possibly-updated head pointer, or pass the head pointer by address (so a pointer-to-pointer) and change the code in InsertStudent accordingly. There are at least a thousand duplicates of this problem on SO. I'll try and hunt one down.

    – WhozCraig
    Nov 23 '18 at 20:50













  • Found one here. The first and second answers actually demonstrate both methods I described above.

    – WhozCraig
    Nov 23 '18 at 20:52













  • from what i have seen, are you trying to suggesting to either change void Insert(..) to MyNode* insert(...) and return the new node... or the second method is to use void ( double pointer)... is that what your saying? i would like to go with double pointers as that seems harder but i don't really know how to do it..

    – EEECS
    Nov 23 '18 at 20:55











  • The comment I posted, in conjunction with the answer and prior question I linked, cannot possibly describe better what needs to be done. The second answer in the linked question shows precisely how to do this with pointer-to-pointer syntax.

    – WhozCraig
    Nov 23 '18 at 20:56











  • thanks. ill fix it now. i just saw ur second post so i didnt know you linked an example

    – EEECS
    Nov 23 '18 at 20:59










1




1





head = (MyNode*) malloc(sizeof(MyNode)) in InsertStudent means nothing to the caller of that function. The head pointer is passed by value. Assigning to it as such just modifies a local variable; the caller's pointer remains unchanged. Either utilize the otherwise-unused return value of the function to communicate a possibly-updated head pointer, or pass the head pointer by address (so a pointer-to-pointer) and change the code in InsertStudent accordingly. There are at least a thousand duplicates of this problem on SO. I'll try and hunt one down.

– WhozCraig
Nov 23 '18 at 20:50







head = (MyNode*) malloc(sizeof(MyNode)) in InsertStudent means nothing to the caller of that function. The head pointer is passed by value. Assigning to it as such just modifies a local variable; the caller's pointer remains unchanged. Either utilize the otherwise-unused return value of the function to communicate a possibly-updated head pointer, or pass the head pointer by address (so a pointer-to-pointer) and change the code in InsertStudent accordingly. There are at least a thousand duplicates of this problem on SO. I'll try and hunt one down.

– WhozCraig
Nov 23 '18 at 20:50















Found one here. The first and second answers actually demonstrate both methods I described above.

– WhozCraig
Nov 23 '18 at 20:52







Found one here. The first and second answers actually demonstrate both methods I described above.

– WhozCraig
Nov 23 '18 at 20:52















from what i have seen, are you trying to suggesting to either change void Insert(..) to MyNode* insert(...) and return the new node... or the second method is to use void ( double pointer)... is that what your saying? i would like to go with double pointers as that seems harder but i don't really know how to do it..

– EEECS
Nov 23 '18 at 20:55





from what i have seen, are you trying to suggesting to either change void Insert(..) to MyNode* insert(...) and return the new node... or the second method is to use void ( double pointer)... is that what your saying? i would like to go with double pointers as that seems harder but i don't really know how to do it..

– EEECS
Nov 23 '18 at 20:55













The comment I posted, in conjunction with the answer and prior question I linked, cannot possibly describe better what needs to be done. The second answer in the linked question shows precisely how to do this with pointer-to-pointer syntax.

– WhozCraig
Nov 23 '18 at 20:56





The comment I posted, in conjunction with the answer and prior question I linked, cannot possibly describe better what needs to be done. The second answer in the linked question shows precisely how to do this with pointer-to-pointer syntax.

– WhozCraig
Nov 23 '18 at 20:56













thanks. ill fix it now. i just saw ur second post so i didnt know you linked an example

– EEECS
Nov 23 '18 at 20:59







thanks. ill fix it now. i just saw ur second post so i didnt know you linked an example

– EEECS
Nov 23 '18 at 20:59














2 Answers
2






active

oldest

votes


















0














You're not setting the initial value of the head pointer to the first node, and since that is never done, the list remains empty and you leak memory like a sieve leaks rain water.



As you have communicated you want to use pointer-to-pointer syntax, the result should look like this . (sans error checking, which you shoudl probably consider adding):



void InsertStudent(char givenName[50], int age, MyNode** head)
{
while (*head)
head = &(*head)->next;

*head = malloc(sizeof **head);
strcpy((*head)->Name, givenName);
(*head)->studentAge = age;
(*head)->next = NULL;
}


Invoked from your main program using the address of the head pointer (do NOT confused that with the address held in the head pointer which you're initially setting to NULL correctly; think of the latter a value held by a pointer, the former as a residence where the head pointer itself is in memory).



InsertStudent(givenName,givenAge, &head); // NOTE THIS


I leave the task of removal and list cleanup.






share|improve this answer
























  • sorry to bother you.. i dont understand what these 3 lines do: while (*head) head = &(*head)->next; *head = malloc(sizeof **head);

    – EEECS
    Nov 23 '18 at 21:42













  • Remember, you're passing the address of a pointer. It's therefore coming in as a pointer-to-pointer. if head in this function is a pointer to a pointer, then *head is the value of the pointer being pointed to. That's the original pointer in main. I strongly suggest you hit up some google-fu with the topic of "C pointer to pointer". or consult your text if you have one. And, as always, single stepping though this code with a debugger, watching variables as it progresses, is extremely educational.

    – WhozCraig
    Nov 23 '18 at 21:45













  • in insert(**head) am i right in assuming head is actually address of head so is it ok to change head into addresshead in that function? then *addresshead will actually be head and **addresshead would be the actual first node, it doesnt really affect the code but its confusing like this.. i am just making sure my logic is correct

    – EEECS
    Nov 23 '18 at 22:38











  • It sounds like it's more or less accurate.

    – WhozCraig
    Nov 24 '18 at 5:52











  • i kind of followed what you said and thought about it and now its working i think i slightly understand double pointers now.. Thanks

    – EEECS
    Nov 24 '18 at 14:42





















0














You are passing head by value; which means that the line in InsertStudent:



head = (MyNode*) malloc(sizeof(MyNode))


which does not update the variable ‘head’ in main.
What you want is to pass &head to InsertStudent, but then InsertStudent has to deal with a MyNode **. The other option is have InsertStudent return head, so that its invocation is:



 head = InsertStudent(name, age, head);


It doesn’t matter much either way, some people prefer the latter because it looks more functional.



Inside of InsertStudent, you add the first element twice. This is almost certainly unwanted. By the time you get to the line:



if(head != NULL){


head is never NULL; if it were, you would have assigned it in the if statement above. You probably want this statement to be:



else {





share|improve this answer
























    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',
    autoActivateHeartbeat: false,
    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%2f53452734%2finserting-node-linked-list-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









    0














    You're not setting the initial value of the head pointer to the first node, and since that is never done, the list remains empty and you leak memory like a sieve leaks rain water.



    As you have communicated you want to use pointer-to-pointer syntax, the result should look like this . (sans error checking, which you shoudl probably consider adding):



    void InsertStudent(char givenName[50], int age, MyNode** head)
    {
    while (*head)
    head = &(*head)->next;

    *head = malloc(sizeof **head);
    strcpy((*head)->Name, givenName);
    (*head)->studentAge = age;
    (*head)->next = NULL;
    }


    Invoked from your main program using the address of the head pointer (do NOT confused that with the address held in the head pointer which you're initially setting to NULL correctly; think of the latter a value held by a pointer, the former as a residence where the head pointer itself is in memory).



    InsertStudent(givenName,givenAge, &head); // NOTE THIS


    I leave the task of removal and list cleanup.






    share|improve this answer
























    • sorry to bother you.. i dont understand what these 3 lines do: while (*head) head = &(*head)->next; *head = malloc(sizeof **head);

      – EEECS
      Nov 23 '18 at 21:42













    • Remember, you're passing the address of a pointer. It's therefore coming in as a pointer-to-pointer. if head in this function is a pointer to a pointer, then *head is the value of the pointer being pointed to. That's the original pointer in main. I strongly suggest you hit up some google-fu with the topic of "C pointer to pointer". or consult your text if you have one. And, as always, single stepping though this code with a debugger, watching variables as it progresses, is extremely educational.

      – WhozCraig
      Nov 23 '18 at 21:45













    • in insert(**head) am i right in assuming head is actually address of head so is it ok to change head into addresshead in that function? then *addresshead will actually be head and **addresshead would be the actual first node, it doesnt really affect the code but its confusing like this.. i am just making sure my logic is correct

      – EEECS
      Nov 23 '18 at 22:38











    • It sounds like it's more or less accurate.

      – WhozCraig
      Nov 24 '18 at 5:52











    • i kind of followed what you said and thought about it and now its working i think i slightly understand double pointers now.. Thanks

      – EEECS
      Nov 24 '18 at 14:42


















    0














    You're not setting the initial value of the head pointer to the first node, and since that is never done, the list remains empty and you leak memory like a sieve leaks rain water.



    As you have communicated you want to use pointer-to-pointer syntax, the result should look like this . (sans error checking, which you shoudl probably consider adding):



    void InsertStudent(char givenName[50], int age, MyNode** head)
    {
    while (*head)
    head = &(*head)->next;

    *head = malloc(sizeof **head);
    strcpy((*head)->Name, givenName);
    (*head)->studentAge = age;
    (*head)->next = NULL;
    }


    Invoked from your main program using the address of the head pointer (do NOT confused that with the address held in the head pointer which you're initially setting to NULL correctly; think of the latter a value held by a pointer, the former as a residence where the head pointer itself is in memory).



    InsertStudent(givenName,givenAge, &head); // NOTE THIS


    I leave the task of removal and list cleanup.






    share|improve this answer
























    • sorry to bother you.. i dont understand what these 3 lines do: while (*head) head = &(*head)->next; *head = malloc(sizeof **head);

      – EEECS
      Nov 23 '18 at 21:42













    • Remember, you're passing the address of a pointer. It's therefore coming in as a pointer-to-pointer. if head in this function is a pointer to a pointer, then *head is the value of the pointer being pointed to. That's the original pointer in main. I strongly suggest you hit up some google-fu with the topic of "C pointer to pointer". or consult your text if you have one. And, as always, single stepping though this code with a debugger, watching variables as it progresses, is extremely educational.

      – WhozCraig
      Nov 23 '18 at 21:45













    • in insert(**head) am i right in assuming head is actually address of head so is it ok to change head into addresshead in that function? then *addresshead will actually be head and **addresshead would be the actual first node, it doesnt really affect the code but its confusing like this.. i am just making sure my logic is correct

      – EEECS
      Nov 23 '18 at 22:38











    • It sounds like it's more or less accurate.

      – WhozCraig
      Nov 24 '18 at 5:52











    • i kind of followed what you said and thought about it and now its working i think i slightly understand double pointers now.. Thanks

      – EEECS
      Nov 24 '18 at 14:42
















    0












    0








    0







    You're not setting the initial value of the head pointer to the first node, and since that is never done, the list remains empty and you leak memory like a sieve leaks rain water.



    As you have communicated you want to use pointer-to-pointer syntax, the result should look like this . (sans error checking, which you shoudl probably consider adding):



    void InsertStudent(char givenName[50], int age, MyNode** head)
    {
    while (*head)
    head = &(*head)->next;

    *head = malloc(sizeof **head);
    strcpy((*head)->Name, givenName);
    (*head)->studentAge = age;
    (*head)->next = NULL;
    }


    Invoked from your main program using the address of the head pointer (do NOT confused that with the address held in the head pointer which you're initially setting to NULL correctly; think of the latter a value held by a pointer, the former as a residence where the head pointer itself is in memory).



    InsertStudent(givenName,givenAge, &head); // NOTE THIS


    I leave the task of removal and list cleanup.






    share|improve this answer













    You're not setting the initial value of the head pointer to the first node, and since that is never done, the list remains empty and you leak memory like a sieve leaks rain water.



    As you have communicated you want to use pointer-to-pointer syntax, the result should look like this . (sans error checking, which you shoudl probably consider adding):



    void InsertStudent(char givenName[50], int age, MyNode** head)
    {
    while (*head)
    head = &(*head)->next;

    *head = malloc(sizeof **head);
    strcpy((*head)->Name, givenName);
    (*head)->studentAge = age;
    (*head)->next = NULL;
    }


    Invoked from your main program using the address of the head pointer (do NOT confused that with the address held in the head pointer which you're initially setting to NULL correctly; think of the latter a value held by a pointer, the former as a residence where the head pointer itself is in memory).



    InsertStudent(givenName,givenAge, &head); // NOTE THIS


    I leave the task of removal and list cleanup.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 21:31









    WhozCraigWhozCraig

    51.8k959108




    51.8k959108













    • sorry to bother you.. i dont understand what these 3 lines do: while (*head) head = &(*head)->next; *head = malloc(sizeof **head);

      – EEECS
      Nov 23 '18 at 21:42













    • Remember, you're passing the address of a pointer. It's therefore coming in as a pointer-to-pointer. if head in this function is a pointer to a pointer, then *head is the value of the pointer being pointed to. That's the original pointer in main. I strongly suggest you hit up some google-fu with the topic of "C pointer to pointer". or consult your text if you have one. And, as always, single stepping though this code with a debugger, watching variables as it progresses, is extremely educational.

      – WhozCraig
      Nov 23 '18 at 21:45













    • in insert(**head) am i right in assuming head is actually address of head so is it ok to change head into addresshead in that function? then *addresshead will actually be head and **addresshead would be the actual first node, it doesnt really affect the code but its confusing like this.. i am just making sure my logic is correct

      – EEECS
      Nov 23 '18 at 22:38











    • It sounds like it's more or less accurate.

      – WhozCraig
      Nov 24 '18 at 5:52











    • i kind of followed what you said and thought about it and now its working i think i slightly understand double pointers now.. Thanks

      – EEECS
      Nov 24 '18 at 14:42





















    • sorry to bother you.. i dont understand what these 3 lines do: while (*head) head = &(*head)->next; *head = malloc(sizeof **head);

      – EEECS
      Nov 23 '18 at 21:42













    • Remember, you're passing the address of a pointer. It's therefore coming in as a pointer-to-pointer. if head in this function is a pointer to a pointer, then *head is the value of the pointer being pointed to. That's the original pointer in main. I strongly suggest you hit up some google-fu with the topic of "C pointer to pointer". or consult your text if you have one. And, as always, single stepping though this code with a debugger, watching variables as it progresses, is extremely educational.

      – WhozCraig
      Nov 23 '18 at 21:45













    • in insert(**head) am i right in assuming head is actually address of head so is it ok to change head into addresshead in that function? then *addresshead will actually be head and **addresshead would be the actual first node, it doesnt really affect the code but its confusing like this.. i am just making sure my logic is correct

      – EEECS
      Nov 23 '18 at 22:38











    • It sounds like it's more or less accurate.

      – WhozCraig
      Nov 24 '18 at 5:52











    • i kind of followed what you said and thought about it and now its working i think i slightly understand double pointers now.. Thanks

      – EEECS
      Nov 24 '18 at 14:42



















    sorry to bother you.. i dont understand what these 3 lines do: while (*head) head = &(*head)->next; *head = malloc(sizeof **head);

    – EEECS
    Nov 23 '18 at 21:42







    sorry to bother you.. i dont understand what these 3 lines do: while (*head) head = &(*head)->next; *head = malloc(sizeof **head);

    – EEECS
    Nov 23 '18 at 21:42















    Remember, you're passing the address of a pointer. It's therefore coming in as a pointer-to-pointer. if head in this function is a pointer to a pointer, then *head is the value of the pointer being pointed to. That's the original pointer in main. I strongly suggest you hit up some google-fu with the topic of "C pointer to pointer". or consult your text if you have one. And, as always, single stepping though this code with a debugger, watching variables as it progresses, is extremely educational.

    – WhozCraig
    Nov 23 '18 at 21:45







    Remember, you're passing the address of a pointer. It's therefore coming in as a pointer-to-pointer. if head in this function is a pointer to a pointer, then *head is the value of the pointer being pointed to. That's the original pointer in main. I strongly suggest you hit up some google-fu with the topic of "C pointer to pointer". or consult your text if you have one. And, as always, single stepping though this code with a debugger, watching variables as it progresses, is extremely educational.

    – WhozCraig
    Nov 23 '18 at 21:45















    in insert(**head) am i right in assuming head is actually address of head so is it ok to change head into addresshead in that function? then *addresshead will actually be head and **addresshead would be the actual first node, it doesnt really affect the code but its confusing like this.. i am just making sure my logic is correct

    – EEECS
    Nov 23 '18 at 22:38





    in insert(**head) am i right in assuming head is actually address of head so is it ok to change head into addresshead in that function? then *addresshead will actually be head and **addresshead would be the actual first node, it doesnt really affect the code but its confusing like this.. i am just making sure my logic is correct

    – EEECS
    Nov 23 '18 at 22:38













    It sounds like it's more or less accurate.

    – WhozCraig
    Nov 24 '18 at 5:52





    It sounds like it's more or less accurate.

    – WhozCraig
    Nov 24 '18 at 5:52













    i kind of followed what you said and thought about it and now its working i think i slightly understand double pointers now.. Thanks

    – EEECS
    Nov 24 '18 at 14:42







    i kind of followed what you said and thought about it and now its working i think i slightly understand double pointers now.. Thanks

    – EEECS
    Nov 24 '18 at 14:42















    0














    You are passing head by value; which means that the line in InsertStudent:



    head = (MyNode*) malloc(sizeof(MyNode))


    which does not update the variable ‘head’ in main.
    What you want is to pass &head to InsertStudent, but then InsertStudent has to deal with a MyNode **. The other option is have InsertStudent return head, so that its invocation is:



     head = InsertStudent(name, age, head);


    It doesn’t matter much either way, some people prefer the latter because it looks more functional.



    Inside of InsertStudent, you add the first element twice. This is almost certainly unwanted. By the time you get to the line:



    if(head != NULL){


    head is never NULL; if it were, you would have assigned it in the if statement above. You probably want this statement to be:



    else {





    share|improve this answer




























      0














      You are passing head by value; which means that the line in InsertStudent:



      head = (MyNode*) malloc(sizeof(MyNode))


      which does not update the variable ‘head’ in main.
      What you want is to pass &head to InsertStudent, but then InsertStudent has to deal with a MyNode **. The other option is have InsertStudent return head, so that its invocation is:



       head = InsertStudent(name, age, head);


      It doesn’t matter much either way, some people prefer the latter because it looks more functional.



      Inside of InsertStudent, you add the first element twice. This is almost certainly unwanted. By the time you get to the line:



      if(head != NULL){


      head is never NULL; if it were, you would have assigned it in the if statement above. You probably want this statement to be:



      else {





      share|improve this answer


























        0












        0








        0







        You are passing head by value; which means that the line in InsertStudent:



        head = (MyNode*) malloc(sizeof(MyNode))


        which does not update the variable ‘head’ in main.
        What you want is to pass &head to InsertStudent, but then InsertStudent has to deal with a MyNode **. The other option is have InsertStudent return head, so that its invocation is:



         head = InsertStudent(name, age, head);


        It doesn’t matter much either way, some people prefer the latter because it looks more functional.



        Inside of InsertStudent, you add the first element twice. This is almost certainly unwanted. By the time you get to the line:



        if(head != NULL){


        head is never NULL; if it were, you would have assigned it in the if statement above. You probably want this statement to be:



        else {





        share|improve this answer













        You are passing head by value; which means that the line in InsertStudent:



        head = (MyNode*) malloc(sizeof(MyNode))


        which does not update the variable ‘head’ in main.
        What you want is to pass &head to InsertStudent, but then InsertStudent has to deal with a MyNode **. The other option is have InsertStudent return head, so that its invocation is:



         head = InsertStudent(name, age, head);


        It doesn’t matter much either way, some people prefer the latter because it looks more functional.



        Inside of InsertStudent, you add the first element twice. This is almost certainly unwanted. By the time you get to the line:



        if(head != NULL){


        head is never NULL; if it were, you would have assigned it in the if statement above. You probably want this statement to be:



        else {






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 21:37









        mevetsmevets

        2,438919




        2,438919






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452734%2finserting-node-linked-list-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

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]