C++ array and pointer *This is a question related to arithmetic

Asked 2 years ago, Updated 2 years ago, 43 views

#include <stdio.h>
#include <conio.h>

int main()
{
    int a[3][5] = {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15}
    };
    printf("%d", (a + 1));
    printf("\n%d", *(a + 1));
    _getch();

}
The result of the first printf and the result of the second printf are the same in the above code 
I don't understand why it looks the same. 

*(a + 1) means "Read the value in the street number pointed to by the pointer (a + 1) 
The result (as a result of reading the value contained in the address) was a[1]. 
That is, the value included in the address indicated by the pointer a+1 is the address value of a[1]. 
In other words, (a + 1) means that it has a street value of 'something with a street value of a[1]'. 
Doesn't that make sense? A +1 to A means that it went to A by 4*5 bytes  
What do you mean there's a street value of something with a street value of a[1]. 
I'm sure I misunderstood something, but I can't tell even if I read the blog.
(Sorry for confusing the sentence. But I think I can't help expressing my question myself.)

array pointer indirect-referencing

2022-09-21 19:34

1 Answers

A one-dimensional array would have a different value. However, a above is a two-dimensional array.

So if you look at the next one,

#include <stdio.h>

int main(){
    int a[3][5] = {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15}
    };

    printf("%d", a[1][2]); // output as follows:
    a[1] //So next is the pointer.
    The a[1]=*(a+1) //[] operator is as follows:
    This is also a pointer.
    So this is also a pointer. 
}

And since both values point to 1*3 away from the beginning, they have the same value.

In a two-dimensional array, you have to write two *operators.

I think I'm stammering. You understand, right?


2022-09-21 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.