Quick pointer question

What's the difference between
int *i;
and
int i[];
???
closed account (zb0S216C)
In the context of function parameters, there's no difference. However, the sub-script form is used to declare an array, whereas the asterisk form is used to declare a pointer to a T.

So basically, the two forms are equivalent in most, if not all, contexts.

Wazzak
Last edited on
Right now they are equivalent, but it's good to have background understanding and not just treat them equally. Difference start emerging when array assignment is done. For example. Dynamic memory allocation and unallocation is different in both cases. Pointer increment/decrement behaves differently, etc.
Because there are semicolons at the end of these lines, I doubt this is a question about function parameters.

The first line creates an object of type pointer to int, and leaves it uninitialized, that is, it's not pointing at any int in particular.

The second line is an error. But if you put a constant expression between those brackets, e.g. int i[10];, it will create an array containing the specified number of ints.

And no, arays are not pointers.
closed account (zb0S216C)
@Cubbi: If arrays are not pointers, why can you dereference them?

Wazzak
Wazzak: same reason you can take a square root of 2 in C. When you're attempting an invalid operation, the compiler will check if there exist implicit conversions from the argument you've supplied to some overload of operator* or sqrt(). The existence of a conversion doesn't mean that arrays are pointers anymore than that ints are doubles.
closed account (zb0S216C)
You have a point there, Cubbi.

"if there exist implicit conversions from the argument you've supplied to some overload of operator*" ...got me thinking. Maybe dereferencing an array yields a pointer holding the address of an array element, giving the impression an array was implemented with pointers? Anyway, I'm not going to hijack this thread.

Wazzak
Last edited on
@Cubbi: Why is the second line an error?

And I'm still not sure what the difference between the two is
closed account (zb0S216C)
The first declaration is a pointer to a int. A pointer, if you don't already know, points to the location of another int. With that said, it's safe to say a pointer holds the address of another int object.

The second declaration is an array of ints (a group).

And when I said, "In the context of function parameters, there's no difference", I mean the two forms are pointers if they were parameters.

Wazzak
Last edited on
Ok, cool! Thanks a lot =D
Why is the second line an error?


Because it is not valid C++. You must put a constant expression between the brackets.
Well, before I've seen some people do this:
int main(int argc, char *argv[])
Is that also an error?
That's in a function parameter, which was mentioned earlier as valid and equivalent.
Ok, so it would be the same as?
int main(int argc, char **argv)
Correct.
Ok, cool! Thanks! =D
Topic archived. No new replies allowed.