pointer to array of structs

This is a discussion on pointer to array of structs within the C Programming forums, part of the General Programming Boards category; What is the proper syntax for initializing a pointer to an array of structures? I have the following definitions: Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    pointer to array of structs

    What is the proper syntax for initializing a pointer to an array of structures?

    I have the following definitions:
    Code:
    typedef struct
    {
       int Targets;
       Target_t targets[TARGETS_MAX];
    } TargetList_t;
    
    typedef struct  
    {
      int id;                           
      float distX;                        
      float distY;                       
    
    }Target_t;
    
    TargetList_t foo[SENS_MAX];
    TargetList_t* ptrfoo = &foo;
    TargetList_t TL;
    TargetList_t* ptrTL = &TL;
    If I try to initialize the pointer ptrTL it works fine
    Code:
    ptrTL->targets[0].distX = 4;
    However, in initializing the pointer to foo
    Code:
    ptrfoo[0]->targets[0].distX = 2;
    I get an error saying:
    error C2232: '->targets' : left operand has 'struct' type, use '.'
    what gives? How can I initialize a pointer to an array of structures?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    For one, this is wrong:

    Code:
    TargetList_t foo[SENS_MAX];
    TargetList_t* ptrfoo = &foo;
    Since foo is an array, "foo" by itself is a pointer. (aka, foo == &foo[0]).

    Todd

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > TargetList_t* ptrfoo = &foo;
    You don't need the & in this assignment.

    > ptrfoo[0]->targets[0].distX = 2;
    A pointer to an array, and an array share the same notation, so in this case it would be
    ptrfoo[0].targets[0].distX = 2;

    Being the first element of the array, you could have done this as well
    ptrfoo->targets[0].distX = 2;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    Quote Originally Posted by Todd Burch View Post
    For one, this is wrong:
    Since foo is an array, "foo" by itself is a pointer. (aka, foo == &foo[0]).

    Todd
    Ahh, I did not know that. Thanks for the tip.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    You should have gotten a warning on that.
    Conversion from TargetList_t** to TargetList_t*.
    Do you have warnings enabled?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  2. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 09:29 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM
  5. Replies: 41
    Last Post: 07-04-2004, 03:23 PM

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