Like Tree7Likes
  • 1 Post By CommonTater
  • 1 Post By CommonTater
  • 1 Post By TheBigH
  • 2 Post By CommonTater
  • 1 Post By TheBigH
  • 1 Post By CommonTater

Array of pointers to a struct

This is a discussion on Array of pointers to a struct within the C Programming forums, part of the General Programming Boards category; Could someone try to explain to me how to make an array of pointers to a structure? I am trying ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Array of pointers to a struct

    Could someone try to explain to me how to make an array of pointers to a structure?

    I am trying to create a bunch of structures and store my access to them by storing the pointer to them in an array.

    Really confused as to how to do this.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It would probably be easier to just make an array of structs... something like

    Code:
    struct tStuff
     { int x;
        int y;
        char z[100];
      }
    
    struct tStuff MyStuff[500];
    Magical likes this.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Normally I would (Which is why I don't really understand this lol), but I need to move the pointer around. I hit a brick wall in not knowing how to move forward

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magical View Post
    Normally I would (Which is why I don't really understand this lol), but I need to move the pointer around. I hit a brick wall in not knowing how to move forward
    Ok do some searching for tutorials on linked lists... You'll also find tons of threads on them here so you may want to do a little local snooping as well.
    Magical likes this.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    I know linked lists already :/ Not sure how they relate to making an array hold pointers.

    I feel like I'm missing something basic lol.

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Try something like

    Code:
    struct tStuff
     { int x;
        int y;
        char z[100];
      }
    
    struct tStuff *MyStuff[500];
    This declares MyStuff to be an array of pointers to tStuff structs, with enough room for 500 such pointers. Thus MyStuff[0] is a pointer to a tStruct struct, as is MyStuff[1], etc.
    Magical likes this.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TheBigH View Post
    Try something like

    Code:
    struct tStuff
     { int x;
        int y;
        char z[100];
      }
    
    struct tStuff *MyStuff[500];
    This declares MyStuff to be an array of pointers to tStuff structs, with enough room for 500 such pointers. Thus MyStuff[0] is a pointer to a tStruct struct, as is MyStuff[1], etc.
    And... don't forget, each one will have to be created with malloc() and destroyed with free().
    TheBigH and Magical like this.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by TheBigH View Post
    Try something like

    Code:
    struct tStuff
     { int x;
        int y;
        char z[100];
      }
    
    struct tStuff *MyStuff[500];
    This declares MyStuff to be an array of pointers to tStuff structs, with enough room for 500 such pointers. Thus MyStuff[0] is a pointer to a tStruct struct, as is MyStuff[1], etc.
    Code:
    MyStuff[i] = (tStuff *) malloc(sizeof(tStuff));
    1 = MyStuff[i]->x;
    2 = MyStuff[i]->y;
    3 = MyStuff[i+1]->x;
    Is this close to how I would do it? It feels like this is more of a Struct of Arrays that I could of just declared as

    Code:
    struct tStuff MyStuff[500];
    Trying to figure out how to move the pointer to the structs on and off queues.

  9. #9
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Yes, that's more or less right. Except, do
    Code:
    MyStuff[i+1]->x = 3
    rather than the other way round. Also, you have to remember to malloc each element of the array:
    Code:
    for( i=0;i<500;i++) {
       MyStuff[i] = (tStuff *) malloc(sizeof(tStuff));
    }
    You are also right in saying that you could just put the structs in the array rather than pointers to the structs. This may well be simpler. However, there are times when an array of pointers to structs is useful. For example, suppose the struct had 100 elements, that one of the elements was a person's surname and that you wanted to sort the structs in alphabetical order of surnames. Sorting involves a lot of swapping, and moving structs around is a lot slower than moving pointers around because pointers are a lot smaller. So the sorting will go faster if your array contains pointers rather than the whole thing.
    Magical likes this.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magical View Post
    Code:
    MyStuff[i] = (tStuff *) malloc(sizeof(tStuff));
    1 = MyStuff[i]->x;
    2 = MyStuff[i]->y;
    3 = MyStuff[i+1]->x;
    I'm pretty sure the general principles of math don't allow you to redefine constants abd I'm certain C isn't going to let you tell it 3 doesn't equal 3.

    Also there is no reason to typecast the return value of malloc...
    Code:
    Mystuf[i] = malloc(sizeof(tStuff));
    is perfectly adequate.
    Magical likes this.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    I'm pretty sure the general principles of math don't allow you to redefine constants abd I'm certain C isn't going to let you tell it 3 doesn't equal 3.

    Also there is no reason to typecast the return value of malloc...
    Code:
    Mystuf[i] = malloc(sizeof(tStuff));
    is perfectly adequate.
    Heehee I typed that fast, meant it to be other way
    MyStuff[i+1]->x = 3;


    Thanks for the help you two !

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magical View Post
    Is this close to how I would do it? It feels like this is more of a Struct of Arrays that I could of just declared as

    Code:
    struct tStuff MyStuff[500];
    It's an array of structs... not the other way around.
    Yer a real "Wrongway Feldman" aren't ya ...

    And yes it will probably be easier to just declare an array of structs as I showed you right up front. The only restriction being that if you are declaring the array inside a function (such as main() ) you have to be careful how big it gets since it's created on the stack and might cause problems if the stack overflows. Of course the answer is to declare it globally (outside any function) in which case it can be any size you need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically Allocated Arrays- my pointers are a mess
    By Soulzityr in forum C Programming
    Replies: 16
    Last Post: 01-26-2010, 10:02 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 10:50 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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