Thread: Accessing arrays using pointers

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    19

    Accessing arrays using pointers

    I came across the below code snippet in a project and was not sure how value of variable "response" is computed. Here as we can see, pic_data holds two one dimensional arrays but "response" access both the single dimensional array as two dimensional array. Can anyone please explain how this works?

    Code:
    #define MAX 100
    #define MAXBUF 100
    
    u32 response;
    u32 index;
    
    typedef struct {  
          u16         flag;  
          u16         status;  
    } __attribute__ ((packed)) register;
    
    typedef struct{  
            register      *rq[MAX];  
           u64            buf[MAXBUF];
    }Data;
    
    Data *pic_data;
    
    void getres(Data *pic_data) {   
     response = *((u32*)&(pic_data->rq[index][pic_data->buf[index]]));
    }
    Last edited by pkumarn; 08-30-2013 at 11:28 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    rq is array of pointers

    You do not provide the initialization of pointer but suppose it is pointing to the array of structs reqister then
    Code:
    &(pic_data->rq[index][pic_data->buf[index]])
    will be pointer to some struct in this external array pointed by rq[index]

    This struct contains 2 16 bit unsigned integers, so casting pointer to u32 and then dereferencing pointer will return u32 combined from 2 u16 integers.

    Of course this solution is not portable. So this code is written for one specific architecture in mind. And one specific compiler (based on usage of __attribute__)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    Thanks for the quick reply.

    To dig more into it. I understand the part of pic_data->rq[index] typecasts to u32 gives me value of both 'flag' and 'status' but what would this fetch pic_data->buf[index]?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    As I said pq[index] is pointing not to the single struct but to arrays of structs
    So it looks like pic_data->buf[index] is used as index in this external arrays
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing elements in an array of pointers to arrays
    By fxtdr79 in forum C Programming
    Replies: 5
    Last Post: 06-05-2010, 09:29 PM
  2. Not accessing my pointers correctly?
    By MoonGuy in forum C Programming
    Replies: 6
    Last Post: 09-12-2009, 01:06 PM
  3. Accessing Arrays
    By perkins in forum C++ Programming
    Replies: 7
    Last Post: 12-30-2005, 06:06 PM
  4. Accessing vector pointers
    By blue8173 in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2003, 07:32 PM
  5. accessing arrays with pointers
    By Nutka in forum C Programming
    Replies: 9
    Last Post: 09-30-2002, 08:19 PM

Tags for this Thread