0

I have come across an opinion that in case we have a pointer to a pointer, we can dereference it via [][] in case the allocated memory is adjacent. In case its not, only dereferencing via pointers arithmetic can be used. But I cannot quite reproduce that situation, i.e. here is an example code:

#include <stdio.h> #include <stdlib.h> #define ITEMS_NO 3 #define LETTERS_NO 4 int main(void) { char* theArray[ITEMS_NO]; char* dummyArr[ITEMS_NO]; for (int i = 0; i < ITEMS_NO; ++i) { theArray[i] = malloc(LETTERS_NO); dummyArr[i] = malloc(LETTERS_NO); // make sure mem is not adjecent for (int k = 0; k < LETTERS_NO - 1; ++k) { theArray[i][k] = '0' + i; dummyArr[i][k] = 'z'; } theArray[i][LETTERS_NO - 1] = 0; dummyArr[i][LETTERS_NO - 1] = 0; } for (int i = 0; i < ITEMS_NO; ++i) { printf("item by square bracket %i = %c \n", i, theArray[i][1]); printf("item by ptr %i = %c \n", i, *((*(theArray + i)) + 1)); free(theArray[i]); free(dummyArr[i]); } } 

In this example i allocate the dummyArr in order to make sure that the allocated memory for the theArray is not adjacentnext to each other. Both printf calls give the same results. Am I not getting something here or do both dereferencing methods work exactly the same way?

4
  • 1
    The C language itself defines that p[i] and *(p + i) are exactly identical and interchangeable with each other; whatever alignment concerns there are work the same way either way. Commented Dec 11, 2019 at 14:53
  • * and [] is the same depending on the type of pointer it is. For instance if the pointer is of type int then pointer[1] is sizeof(int) passed the first index. Likewise *(pointer+1) is also sizeof(int) passed the first index. And I'm not exactly sure how you're checking the memory isn't alligned. If you want to target a specific byte after index 0 you can cast it to char then add your offset. Commented Dec 11, 2019 at 14:54
  • 1
    When you say "aligned" do you really mean "adjacent"? Commented Dec 11, 2019 at 15:07
  • @Someprogrammerdude indeed. Fixed, thank you. Commented Dec 11, 2019 at 15:25

1 Answer 1

3

The two methods are exactly equivalent. Section 6.5.2.1p2 of the C standard regarding array subscripting states:

A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

The fact that you have a pointer-to-pointer, where each pointer points to a distinct array, as opposed to a true 2D array doesn't change this.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.