问题描述:

I'm new to C++ and I cannot figure out how pointers work in relation to arrays. I do cannot figure out how I am supposed to access an element in an array pointer. Here's what I mean:

int* array[10];

(*array)[5] = 4;// This works but I don't think that that is the correct way to do it

array->[5] = 4; // Is there a similar method such as what you would use for a normal pointer?

Also I was wondering if you could initialize an array pointer like you can initialize a regular array with an array in curly brackets.

int array[] = {0, 2, 45, 235};// Works

int* array[] = {0, 2, 45, 235};// does not work

int* array[] = &{0, 2, 45, 235};// does not work

EDIT:

Some of you are suggesting to use an array without a pointer. But if i do this, wont it make a copy whenever i pass it into a method like with normal variables?

网友答案:

This declaration:

int* array[10];

...might not be what you think it is. This is a declaration of an array of 10 pointers-to-int. It's not an array of 10 ints.

Since you haven't actually initialized each of those 10 pointers, trying to access one of them evoked Undefined Behavior. That's what you're doing here:

(*array)[5] = 4;

...and again here:

array->[5] = 4;

What you probably want is an array of 10 ints, which you declare like this:

int array[10];

Now you can access the items just like this:

array[5] = 4;

But, while we're on the subject of arrays, why not use a vector instead and avoid all this mess?

vector<int> vi;
vi.push_back(1);
/* ... */
网友答案:
int * array[10]

That's not an array pointer. It's an array of pointers. You put pointers in it, not integers.

An array pointer would look like this:

int (*ptr)[10] = &array;

But that also is not filled with integers. It is filled with arrays of 10 integers. More accurately, it points either to a single array of 10 integers, or to the first array in an array of arrays of 10 integers.

网友答案:

For an array of plain datatypes like int the -> operator is not definied. The * operator is the dereferencing operator, giving you the element where every pointer points to. In your case to an int*.

For the second part of your question, only method one is valid to initalize an array with constant data.

网友答案:

For number one, because array is an array of 10 int*s which point to some random memory location, you need to make the pointers point to an int (this should also answer your next question):

// 10 ints on the heap held in an array on the stack
int* array[] = {new int, new int, new int, new int, new int, new int, new int, new int, new int, new int};

*array[3] = 32; // [] has higher precedence than * so this is like *(array[3])

// remember to loop through and delete each one
for (int i = 0; i < 10; ++i)
    delete array[i];
网友答案:

In your examples, you're trying to initialize arrays of pointers. Basically, I think there's no way to initialize pointer to array, except for

int array[] = {0,2,3,4};
int *ptr = array;

But honestly, I'm not sure if you asked for that.

网友答案:

in general:

  • The symbolic name of an array (int arr[], the name being arr) generally works like a pointer to its head; so arr[3] == *(arr+3). (Note that C/C++ pointer arithmetic is in units of the pointer's size, not bytes.)
  • Creating an array using int* arr[] is an array of pointers to integers, not an array of integers.

In your example, you're trying to place integers into slots that are designated for pointers. It's extremely unlikely that you want to do that.

网友答案:

array itself is a pointer

it points to base address

this will help

http://pw1.netcom.com/~tjensen/ptr/ch2x.htm

网友答案:

No, because pointers have to point at an object, and you're pointing it at an initializer list, not an array object. You have to instantiate an array object with the initializer list for the pointer to point at.

Similarly, you can't have

int* = &7;
网友答案:

When you pass an array, you actually pass a pointer that points to the first element of the array. So you should probably stick to not mix arrays and pointers to arrays. As pointed out earlier, arrays are implicitly represented as pointers. If you are just starting out, first study basic arrays, then pointers and then move on to dynamic memory allocation. Pointers are very tricky, so you might wanna go slow and thoroughly with it. It will save you a lot of time later.

网友答案:

When you write

int* array[10]; 

You're not actually creating an array of integers, or for that matter, an array of pointers to integers. What you are doing is alloctaing 40 bytes of data (since an integer takes up 4 bytes).

相关阅读:
Top