2

If I have:

int A[10][20];
printf("%p",A[3]);

it will print the address of A[3][0].

However, I'd like to know if this one dimensional array A[3] containing pointers really exists, or it is calculated in some way.

7
  • @MarcoBonelli the question is about "array of pointers". That wasn't declared. Commented Oct 6, 2019 at 10:32
  • @MarcoBonelli Technically, I think, with a 2D array declared like that, there is no actual array of int pointers; just a contiguous block of 10x20 integers. Commented Oct 6, 2019 at 10:34
  • Ah, yes that's right, no pointers in A, just a contiguous block of integers. Commented Oct 6, 2019 at 10:35
  • 1
    It's actually a really good question, when considering the more subtle aspects of the underlying memory arrangements. Commented Oct 6, 2019 at 10:48
  • 1
    @Revolver Yes, but the point is (pardon the pun) that it is a pointer whose value has to be calculated - there is no actual array of pointers to look through. Commented Oct 6, 2019 at 10:57

3 Answers 3

4

The way you have defined A means that the compiler will allocate for it a contiguous block of memory large enough to hold 10 x 20 (200) integers; see here (scroll down to "Multidimesional arrays"). As I'm sure you realize, if you were to do printf("%p", A); you would see the address of the beginning of that allocated block.

Now, when the compiler sees the expression A[3], it will add what it calculates as the necessary amount of "integer sizes" to the base address (that of A, or A[0][0]); in this case, it will add "3" (the index specified) multiplied by the combined size of all the other dimensions (in this case, there's only one, which is 20).

So, in your case, there is no actual array of pointers; just a memory block that the compiler can interpret according to how you described any part(s) of it.

However, in a more versatile approach, one can actually define a 2D array in terms of an actual array of pointers, like so:

int **A;
A = malloc(10 * sizeof(int*));
for (int n = 0; n < 10; ++n) A[n] = malloc(20 * sizeof(int));

In this case, using printf("%p",A[3]); would still be valid, but it would give a very different offset value from printf("%p",A); or printf("%p",A[0]);.

It's also, perhaps, worth noting that, even though these two different declarations for A can both resolve an individual element through an expression like A[i][j] (but the compiler would evaluate the addresses differently), there is here scope for major confusion! When, for example, passing such an array to a function: if the function expects data allocated in the second form, and you give it an array defined in the first form (and vice versa), you're gonna get major undefined behaviour .

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

5 Comments

Thank you, I knew that method, that's why I got confused. Perhaps I'm not smart enough to understand what you wrote, I mean, what does the compiler "understand"? If the compiler "thinks" that he should jump to the address 3*20*sizeof(int) (for A[3]) why does it print the address and not the value stored? Thank you so much, I apologize for the question
You can't pass a two-dimensional array to a function accepting int**, unless you do explicit cast, in which case you've aimed into your own foot.
@Ruslan - I think you're right. However, recently, on SO, I came across something like that: passing an int a[n][m] argument, which my compiler groaned at, so I changed it to int*[], which caused the implosion.
Dunno, gcc doesn't accept it any way: coliru.stacked-crooked.com/a/d2cd75c036d14b89
@AlessandroF In answer to your comment: the compiler cannot print an int value, because you've only provided one [] index. Thus, it has to interpret that as a pointer - the value of which it can (and must) calculate as described.
0

yes there is a way to calculate the position:

for A[i][j]
the position of the memory block will be

pos = A + i*(number_of_columns_in_each_row) + j

here A is the pointer to the first element of the array

1 Comment

That expression doesn't work because A is a pointer to the first sub-array (row), not &A[0][0]. So A + i, will increment by the size of the sub-array, not the type size.
0

However, I'd like to know if this one dimensional array A containing pointers really exists, or it is calculated in some way.

The way you defined the array A :

int A[10][20];

does not contain any pointers as elements of the array. it contains only integer elements.

if you want to make an array of pointers, which should be assigned to int-variables is defined like that:

int *A[10][20];

You also can set a pointer to the start of the array, which means element [0] [0] by using:

int *pointer;
int *A[10][20];

pointer = &A;

You also be able to set the pointer slightly forwards according to each element by increase the pointer.

pointer++;

5 Comments

I think you've misunderstood the OP's question. We all know the actual array contains integers (not pointers). The question is, what happens when one references a singly-indexed array value? Is there a list of pointers to each 'row' made?
But look again at the OP's question, which was about the 'pointer' nature/value of A[3] (which is a one-dimensional array).
@Adrian where is the relation to any pointer(s)? he did not defined a pointer at all nor get one as return.
The code printf("%p",A[3]); refers to a pointer (actually, a one-dimensional array of integers interpreted as a pointer). OP's question was whether or not such an array of pointers actually exits in this case.
@Adrian So you mean his question is about: if pointers are stored inside A[3] at the memory place of each according array element, which premise is that there are actual no int values stored at the according array elements?

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.