Thread: New Programmer, new Error

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ASURugger View Post
    Does that still have to do with my linked list even though every time I take in a new string I creat a new struct and allocate the memory for it?
    It has everything to do with the fact that you do nothing of the sort. Watch:
    Code:
    while (fgets(tempname, 40, p) != NULL) {
        tempstudent = malloc(sizeof(*tempstudent));
        strcpy(tempstudent->name, tempname);
        tempstudent->ave = 0.0f;
        tempstudent->next = head;
        head = tempstudent;
    }
    You have to malloc a node for every student. (This code adds the new student to the front of the line; you'll have to change it if you want the student at the back, or sorted, or whatever.)

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Code:
    fgets(curr->name, 40, p);
                                    curr->ave = 0.0;
    		curr->next = (Student *)malloc(sizeof(Student));
    		curr = curr->next;
    		curr->next=NULL;
    		count++;
    That seems really similar to what youre doing, where am I wrong in this code here so that I don't do it again? My understanding of my code is that i take in a string and put it in curr.name, i initialize curr.ave as 0.0 and then i make a new Student in curr.next, i move curr to that new student i created and set its next pointer to null. Is that not what is happening here?

    I looked at the code above again and I still think Im allocating memory for each string. Im sorry to ask a redundant question and not understand.
    Last edited by ASURugger; 11-06-2008 at 10:55 AM.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I was looking at the wrong updated code. The updated code in the very first post can't possibly compile, as there's no way you can do a malloc to initalize a global object, so I don't know what code you are actually running.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by ASURugger View Post
    Code:
    fgets(curr->name, 40, p);
                                    curr->ave = 0.0;
    		curr->next = (Student *)malloc(sizeof(Student));
    		curr = curr->next;
    		curr->next=NULL;
    		count++;
    That seems really similar to what youre doing, where am I wrong in this code here so that I don't do it again?
    Though they look similar yet the two approaches are poles part as one adds nodes to the tail of the list while the other adds them to the head of the list.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    I think I understand now. I was confused by the fact that if I assign head to element it means they are both pointing to the same address. I thought id be replacing a variable.

    Thank you for your patience and help. Ill probably have more questions though... lol.

  6. #21
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Code:
    for (x = 0; x<count; x++)
    {		printf("Enter the final average for the student: ");
    		scanf_s("&#37;f", i);
    		element->ave = i;
    		element = element->next;
    		
    }
    Ok there is a definite problem here as it produces an error saying cannot write to location 0x0000000.
    Also count is not incrementing as I think it should, I will link my new while loop, mostly borrowed from tab.
    Code:
    while(!feof(pread))
    {
    	fgets(tempname, 128, pread);
    	for(x = 0; x<128;x++)
    	{
    		if(tempname[x] == '\n')
    		{
    			tempname[x] = '\0';
    			break;
    		}
    	}
    	count++;
    	strcpy(element->name, tempname);
    	element->ave = i;
    	element->next = head;
    	head = element;
    	
    }
    I added the for loop to add a null terminator to the string, i think that would work. count shows up as only 1. Should be 3 from my input file which has 3 names separated by '\n'.
    Im assuming this is a pointer error in my linked list because i get the output "Enter student average" but when I input the double number the error occurs and the program stops working. Any suggestions?

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ASURugger View Post
    Code:
    for (x = 0; x<count; x++)
    {		printf("Enter the final average for the student: ");
    		scanf_s("%f", i);
    		element->ave = i;
    		element = element->next;
    		
    }
    Ok there is a definite problem here as it produces an error saying cannot write to location 0x0000000.
    Also count is not incrementing as I think it should, I will link my new while loop, mostly borrowed from tab.
    Code:
    while(!feof(pread))
    {
    	fgets(tempname, 128, pread);
    	for(x = 0; x<128;x++)
    	{
    		if(tempname[x] == '\n')
    		{
    			tempname[x] = '\0';
    			break;
    		}
    	}
    	count++;
    	strcpy(element->name, tempname);
    	element->ave = i;
    	element->next = head;
    	head = element;
    	
    }
    I added the for loop to add a null terminator to the string, i think that would work. count shows up as only 1. Should be 3 from my input file which has 3 names separated by '\n'.
    Im assuming this is a pointer error in my linked list because i get the output "Enter student average" but when I input the double number the error occurs and the program stops working. Any suggestions?
    Where do you allocate/assign element in the first loop?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    But you don't malloc() any storage before copying to element->name.

  9. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Programming makes me look so stupid currently lol.

    Code:
    while(!feof(pread))
    {
    	fgets(tempname, 128, pread);
    	element =(Student *)malloc(sizeof(Student));
    	for(x = 0; x<128;x++)
    	{
    		if(tempname[x] == '\n')
    		{
    			tempname[x] = '\0';
    			break;
    		}
    	}
    	count++;
    	strcpy(element->name, tempname);
    	element->ave = i;
    	element->next = head;
    	head = element;
    	
    }
    That looking better? I create element earlier in the main function, but im not allocating memory to it until this.

    While im debugging after I execute this loop
    Code:
    for (x = 0; x<count; x++)
    {		printf("Enter the final average for the student: ");
    		scanf_s("&#37;f", &i);
    		element->ave = i;
    		element = element->next;
    		
    }
    It shows i as an integer though ive defined it as a double and and its saying in the value of p <Bad Ptr>. So its a bad pointer im guessing? I dont understand whats happening under the hood too well, so I dont get these messages.
    Last edited by ASURugger; 11-06-2008 at 06:25 PM.

  10. #25
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm deja vu... I remember making similar corrections to your code last time but here goes...

    Code:
    while(fgets(tempname, 128, pread))
    {
    	element = malloc(sizeof(Student));
    	if(element)
    	{
    		strtok(tempname, "\n"); // include <string.h>
    		count++;
    		strcpy(element->name, tempname);
    		element->ave = i;
    		element->next = head;
    		head = element;
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM