Thread: pointer to array of structs

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

    pointer to array of structs

    I have a pointer to an array of two structures. The structure is:

    Code:
    ==in some header.h==
    typedef struct 
    {
    
      float distX;
      float distY;
      float veloX;
      float veloY;
    
    } ObjList_t;
    and the array is defined as:

    Code:
    ==in some file.c==
    static MeasObjList_t Objects_g[MAX]; //MAX = 2
    In my function, I have the pointer defined as:
    Code:
    extern MeasObjList_t Objects_g[MAX];
    
    void foo(void){
    MeasObjList_t (* ptrMeasObjs)[MAX] = &measObjects_g;
    
    ...
    This works out perfectly fine when I have to access ptrMeasObjs[0]->distX but the second structure in the array is not being initialized when I access ptrMeasObjs[1]->distX. Im using MSVC++ and when I look in the Autos window while debugging I see the following when I init ptrMeasObjs[1]->distX to 10

    Code:
    void foo(void){
       MeasObjList_t (* ptrMeasObjs)[MAX] = &measObjects_g;
       ptrMeasObjs[1]->distX = 10.0f;
    
       if (ptrMeasObjs[1]->distX >=5.0f){
          count++; //never gets here
       }
    Code:
    - ptrMeasObjs
      +[0]
      -[1]
            distX = 10.000
            distY = 0.000
            veloX = 0.000
            veloY = 0.000
          
    - ptrMeasObjs[1]
      +[0]
      -[1]
            distX = 0.000
            distY = 0.000
            veloX = 0.000
            veloY = 0.000
    
    ptrMeasObjs[1]->distX = 0
    ptrMeasObjs[1]->distY = 0
    ptrMeasObjs[1]->veloX = 0
    ptrMeasObjs[1]->veloY = 0

    When I just access ptrMeasObjs[0]->distX, it looks like:
    Code:
    void foo(void){
       MeasObjList_t (* ptrMeasObjs)[MAX] = &measObjects_g;
       ptrMeasObjs[0]->distX = 10.0f;
    
       if (ptrMeasObjs[0]->distX >=5.0f){
          count++; //gets here this time
       }
    Looks good in the debugger and it gets to the count inside the loop
    Code:
    - ptrMeasObjs
      -[0]
            distX = 10.000
            distY = 0.000
            veloX = 0.000
            veloY = 0.000
    
      -[1]
            distX = 0.000
            distY = 0.000
            veloX = 0.000
            veloY = 0.000
          
    - ptrMeasObjs[0]
      -[0]
            distX = 10.000
            distY = 0.000
            veloX = 0.000
            veloY = 0.000
    
      -[1]
            distX = 0.000
            distY = 0.000
            veloX = 0.000
            veloY = 0.000
    
    ptrMeasObjs[0]->distX = 10
    ptrMeasObjs[0]->distY = 0
    ptrMeasObjs[0]->veloX = 0
    ptrMeasObjs[0]->veloY = 0
    Why is ptrMeasObjs being listed twice in the debugger? And why with the change in elements of the array does it not work as anticipated for the second half of my array? According to the debugger, it looks like its close, but not quite. What am I doing wrong here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How can it be 'static' and 'extern' at the same time?
    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.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Luken8r View Post

    Code:
    ==in some file.c==
    static MeasObjList_t Objects_g[MAX]; //MAX = 2
    You have an array of structs. There are two structs in the array.

    If you want to use a pointer to access the structs, then you could do something like

    Code:
    MeasObjList_t * pMeasObjList = Objects_g;
    Note that the name of an array is a (constant) pointer whose value is the address of the first element of the array, so this declares a pointer to your kind of struct and sets its value to the address of the first element in your array.

    Then pMeasObjList[0] would access the first struct in the array and pMeasObjList[1] would access the second struct in the array.


    Individual elements of the structs would be accessed by
    pMeasObjList[0].distX
    pMeasObjList[0].distY
    etc.
    pMeasObjList[1].distX
    etc.

    As you indicate in the title of the thread, the thing that you have defined is a pointer to an array of two structs. So, as far as the compiler is concerned, your ptrMeasObjs[0] would point to the first array of two structs, ptrMeasObjs[1] would point to the next array of two structs, etc.

    (So your program produces undefined behavior, since dereferencing ptrMeasObs[1] is accessing memory beyond the amount taken by the two structs of your declaration.)

    D.
    Last edited by Dave Evans; 01-08-2008 at 02:35 PM.

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, 10: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