0

I am new to C++. Please see the following code:

//sample1
int arr[2]  = { 11, 22 };
int(*t)[2];
t = &arr;
cout << *t[0] << "\n";
cout << *t[1] << "\n";


//sample2
int arr2[2];
arr2[0] = { 33 };
arr2[1] = { 44 };
int(*t2)[2];
t2 = &arr2;
cout << *t2[0] << "\n";
cout << *t2[1] << "\n";


//sample3
int arr3[2];
arr3[0] = { 55 };
arr3[2] = { 66 };
int(*t3)[2];
t3 = &arr3;
cout << *t3[0] << "\n";
cout << *t3[1] << "\n";

// output

11
-858993460
33
-858993460
55
66

Can anyone tell me how to initializing a pointer to array?Sample3 is out of my understanding.

2
  • 2
    This is wrong: arr3[2] = { 66 }; Commented Oct 18, 2014 at 14:45
  • 1
    There's nothing wrong with the initialization of arr. You should read up on the precedence of the * and [] operators. (hint: it should be (*arr)[0] etc.) Commented Oct 18, 2014 at 14:45

3 Answers 3

1

There are two issues with your code:

1) [] has more precedence than * so you need parenthesis in all those cases (cfr. operator precedence). If you don't use them, you're going to do pointer-arithmetic on arrays of 2 integers each (thus immediately getting out-of-range), e.g.

int(*t)[2]; // A pointer to an array of 2 integers
cout << t[0]; // The address of the array

[first_integer][second_integer] ... garbage memory ...
^

cout << t[1]; // The address of the array + sizeof(int[2])

[first_integer][second_integer] ... garbage memory ...
                               ^
cout << *t[0]; // Dereference at the address of the array
cout << *t[1]; // Dereference past the end of the array

//  ---- correct -----

cout << (*t)[0]; // Dereference the pointer to the array and get the element there

[first_integer][second_integer] ... garbage memory ...
^

cout << (*t)[1]; // Dereference the pointer to the array and get the second element there

[first_integer][second_integer] ... garbage memory ...
               ^    

2) You have an out-of-range access at line

arr3[2] = { 66 };

This is how you should proceed:

//sample1
int arr[2] = { 11, 22 };
int(*t)[2];
t = &arr;
cout << (*t)[0] << "\n";
cout << (*t)[1] << "\n";


//sample2
int arr2[2];
arr2[0] = { 33 };
arr2[1] = { 44 };
int(*t2)[2];
t2 = &arr2;
cout << (*t2)[0] << "\n";
cout << (*t2)[1] << "\n";


//sample3
int arr3[2];
arr3[0] = { 55 };
arr3[1] = { 66 };
int(*t3)[2];
t3 = &arr3;
cout << (*t3)[0] << "\n";
cout << (*t3)[1] << "\n";

The initialization is just fine.

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

Comments

1

An array is almost the same thing as a pointer:

int arr[2]  = { 11, 22 };
int * t;
t = arr;
cout << t[0] << "\n";
cout << t[1] << "\n";

Comments

0

In this code snippet

//sample3
int arr3[2];
arr3[0] = { 55 };
arr3[2] = { 66 };
int(*t3)[2];
t3 = &arr3;
cout << *t3[0] << "\n";
cout << *t3[1] << "\n";

there is defined an array with two elements. The valid range of indices for elements of this array is 0, 1 In this statement

arr3[2] = { 66 };

there is an attempt to write data beyond the array because index 2 is invalid. So you have

arr3: | 55 | uninitilaized | 66 |
index:   0        1          beyond the array

After these statements

int(*t3)[2];
t3 = &arr3;

pointer t3 points to array arr3,

Expression

t3[0]

gives array arr3

Expression

*t3[0] 

gives the first element of the array. Thus statement

cout << *t3[0] << "\n";

outputs 55.

Expression

t3[1]

points to memory after (beyond) array arr3. You wrote at this address value 66. So this value is outputed by statement

cout << *t3[1] << "\n";

Of course this code snippet is invalid because its overwrite memory that was not reserved for objects of the code.

Comments

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.