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?
p[i]and*(p + i)are exactly identical and interchangeable with each other; whatever alignment concerns there are work the same way either way.intthenpointer[1]issizeof(int)passed the first index. Likewise*(pointer+1)is alsosizeof(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.