Problem to understand casting

Why that to ptr1 point to the address of the array I need to do this (int*)&array? what does it means?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main(){
    int arr[10];
    for (int i = 0; i < 10; i++){
	cin >> arr[i];
    }
    
    int* ptr1 = (int*)&arr;//what does this line means?
    int* ptr2 = arr;
    
    cout<<arr<<endl;
    cout<<&arr[0]<<endl;
    cout<<ptr1<<endl;
    cout<<ptr2<<endl;

    return 0;
}
You don't actually — this is one of those cases where there is really only one valid meaning, so every way people write it is considered OK.

arr is an array. What that means is that it degrades into a pointer to its element type at every opportunity.

In fact, arr[index] is syntactic sugar for *(arr + index).

The way to do it properly is:

 
  int* ptr1 = &(arr[0]);

...which is exactly the same as saying:
 
  int* ptr1 = &(*(arr + 0));

...which is exactly the same as saying:
 
  int* ptr1 = arr;  // 'arr' automagically degrades to pointer to int 


Saying &arr really doesn't mean anything, since 'arr' is not a pointer. So the compiler takes basically ignores the nonsense in that syntax.

(If 'arr' actually were a pointer, then &arr would get the address of the pointer itself, and not of the content of the array.)

If I have not expressed that clearly let me know.
Got it. I taught that there was only one way to do this. But the exact syntax of (int*)&arr means what? I mean, how it works? And thanks anyhow! Good explanation!
Last edited on
> But the exact syntax of (int*)&arr means what? I mean, how it works?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

int main()
{
    using array_type = int[10] ; // type array_type is 'array of 10 int'

    int arr[10] { 12, -18, 22 } ; // the type of object 'arr' is 'array of 10 int'
    array_type arr2 { 12, -18, 22 } ; // the type of object 'arr2' is 'array of 10 int'
    // arr and arr2 are of the same type

    auto ptr = &arr ; // address of object 'arr'
                      // the type of ptr is 'pointer to array of 10 int'
    array_type* ptr2 = &arr ; // ptr and ptr2 are of the same type
    ptr = ptr2 = &arr2 ; // fine

    auto& x = *ptr ; // '*ptr' dereferences 'pointer to array of 10 int'
                     // therefore, type of '*ptr' is 'array of 10 int'
    array_type& y = *ptr ;
    x[3] = y[4] = 7 ; // access the element of the array through x and y
    std::cout << sizeof(ptr[0]) << ' ' << sizeof(x) << '\n' ; // prints size of 'array of 10 int'

    // the array is somewhere in memory and ptr contains the address of the array
    // the array contains 10 integers, the first one is at the beginning of the array
    int* pi = (int*) ptr ; // cast the pointer to array to a pointer to int
    std::cout << sizeof(pi[0]) << ' ' << sizeof(ptr[0][0]) << '\n' ; // prints size of 'int'

    // print the first two elements of the array
    std::cout << pi[0] << ' ' << pi[1] << '\n' ;
    // print the first two elements of the array
    std::cout << ptr[0][0] << ' ' << ptr[0][1] << '\n' ;

    // print the address of the first two elements of the array
    std::cout << pi << ' ' << pi+1 << '\n' ;

    // print the addresses of the array and a hypothetical (non-existent)
    // second array of the same type which is immediately after the array
    std::cout << ptr << ' ' << ptr+1 << '\n' ;
}

http://coliru.stacked-crooked.com/a/86c779d79d357f79
Topic archived. No new replies allowed.