1

I have a generic struct declared and an array of these structs as given below:

struct A
{
    int x,y,z;
    char a,b,c;
};

struct A *str_arr[5];

From my understanding str_arr is a pointer to a block of memory which stores pointers to the 5 structs in sequential order and therefore these pointers can be accessed via pointer arithmetic or array indexing as:

struct A *str_a = str_arr[1];                    // points to 2nd struct?
struct A *str_b = str_arr + 2*sizeof(struct A*); // points to 3rd struct?

However, these 5 structs might not be in sequential memory?

printf("%p\n", str_arr);              // prints memory location of start of str_arr pointers?
printf("%p\n", str_arr[1])            // prints memory location of 2nd struct?
printf("%d\n" str_arr == &str_arr[0]) // prints 1?

I would just like clarification that my understanding is correct with all of the points I have raised.

2
  • str_arr is array of pointer to struct A (five pointer to struct A). Commented May 28, 2015 at 5:45
  • Just like the integers in int a[42] do not have to be in sequential order. They are whatever you put in there. Commented May 28, 2015 at 5:57

1 Answer 1

4

All is correct except one:

struct A **str_b = str_arr + 2 /* *sizeof(struct A*) */;
/*       ^^                    ^^^^^^^^^^^^^^^^^^^^^^ */
/* Not need to multiply with size. Dereference with * if your type is struct A * */

or

struct A *str_b = *(str_arr + 2);

You give the offset in terms of number of elements and not the size in bytes.

str_arr + 2*sizeof(struct A*) is equivalent to &str_arr[2*sizeof(struct A*)]

             +0  +1  +2  +3  +4
           +---+---+---+---+---+
           | A | B | C | D | E |
           +---+---+---+---+---+
str_arr    ^^^^^^^^^^^^^^^^^^^^^
&str_arr[0]^^^^   
str_arr[1] = B
  • str_arr is the address of array start
  • str_arr[1] is the contents at offset +1 i.e. B which is an address pointing to object of type struct A.
  • str_arr == &str_arr[0] are same address with different type

As suggested by @Gopi, last point can be proven by printing the following:

  1. sizeof str_arr v/s &str_arr[0]
  2. Following address, &str_arr + 1 v/s str_arr + 1
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it does, only with the different type. That means address generated from &str_arr and str_arr + 0 are same but addresses generated from expressions &str_arr + 1 and str_arr + 1 are different
Yeah I know I meant that also can be added to the answer
@Gopi Thanks for the input. I thought it is implied by the figure. Length of ^^^is different for str_arr and &str_arr[0]. But still feel free to edit the answer if you can add more helpful or easier to read info for OP. I will try to add your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.