Issue with arrays, pointers, and structs.

This is a discussion on Issue with arrays, pointers, and structs. within the C Programming forums, part of the General Programming Boards category; Code: #include "stdio.h" struct person{ int personID; //more stuff here later on }; struct person *population[10]; int main(int argc, char ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Issue with arrays, pointers, and structs.

    Code:
    #include "stdio.h"
    
    struct person{
    	int personID;
            //more stuff here later on
    };
    struct person *population[10];
    
    int main(int argc, char *argv[]){
    	int i;
    	
    	for(i = 0; i < 10; i++){
    		struct person p;
    		population[i] = &p;
    		population[i]->personID = i;
    	}
    	
    	for(i = 0; i < 10; i++){
    		printf("Person Address: %d \n",*(population[i]));
    		printf("Person ID: %d \n",population[i]->personID);
    	}
    }
    First off, I'm fairly new to C and still trying to figure things out. I'm trying to create an array of pointers to person structs. I expected each iteration of the first for loop to create a new person struct with a new memory address. However it just seems to be giving me the same one every time. I've been reading up on structs, arrays, and pointers on google and I can't figure out why my code isn't working.

    Output from second loop:
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9
    Person Address: 1606416904
    Person ID: 9

    Could someone tell me how to get this working?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since your variable p is declared inside the for loop, at the end of each iteration of your for loop, your variable p goes away. Your pointer is still a valid variable, but it is pointing at something that no longer exists.

    Since nothing exciting happened between one iteration of the for loop and the next one, when the next variable p was created, it used the same, recently deallocated, piece of memory as before (this is why all your pointers point to the same place).

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Did you get this working? You are about 97% there already!

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Try allocating memory for each of your assignments...

    Code:
    /* for instance */
    ...  
       population[i]= malloc (sizeof (struct person));
    ...
    What tabstop pointed out, should have been a big clue!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I'd say he's 50% there. Need to malloc each struct. Or stop using an array of pointers to structs and just use an array of 10 structs. population[10] (without the *)

    [ never mind. slingerland3g has already suggested it... while I was posting. ]
    Last edited by nonoob; 03-29-2010 at 03:08 PM.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Thanks a lot. I have it working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs and arrays - overview and help neede
    By pistacchio in forum C Programming
    Replies: 7
    Last Post: 01-11-2010, 01:11 AM
  2. Arrays, Structs, Pointers, and unexpected crashes
    By KairosDrasis in forum C Programming
    Replies: 2
    Last Post: 04-13-2009, 12:37 AM
  3. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 12:27 AM
  4. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 10:25 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21