codelab

Allocate an array of 100 integer pointers and assign the resulting pointer to the appropriately declared variable , ip_arr. Allocate 100 integers and assign the resulting pointers to the elements of ip_arr. Initialize each integer value to -1.

My Answer:

ip_arr = new int [100];
for (int i = 0; i <100; i++)
{
ip_arr[i]= -1;
}

CodeLab states that the above is incorrect;
Any help would be appreciated. Thank you.
Last edited on
ip_arr = new int [100];
That's an array of a hundred integers. You're meant to be making an array of a hundred integer pointers.
so would it be

ip_arr = new int * [100];
for (int i = 0; i < 100; i++)
{
**ip_arr[i] = -1;
}
Still no.

You do now have an array of a hundred int-pointers. Good. Where are they all pointing? Are any of them pointing at an int? No, none of them point at an int. They are all pointing at random memory somewhere.

It is up to you to make sure each of them points to an int. That's what this is talking about:
Allocate 100 integers and assign the resulting pointers to the elements of ip_arr.


**ip_arr[i] = -1;

This is also wrong.ip_arr[i] is the ith element in the array, so *ip_arr[i] is dereferencing the ith element, so **ip_arr[i] is trying to dereference an int - nonsense.

I think you don't understand pointers. You should learn what a pointer is before you try to use them. They're not difficult. http://www.cplusplus.com/articles/EN3hAqkS/
yea I have idea as to wtf I'm doing.. Thanks for the help bro. cheers.
Topic archived. No new replies allowed.