Not at all. One time you declare an int and an int*, and then let the int* point to the int. The next time you declare 2 int* and 2 int arrays, and let each of the int* point to one of the arrays.
in your case they are both/all pointers-to-an-integer, it is just how they are assigned that is different. So yes they accomplish the same thing , that of assigning a value to a pointers-to-an-integer.
Same here:
1 2 3 4 5 6
int num;
int arr[10];
int * int_ptr;
int_ptr = # // points to num
int_ptr = arr; // points to the first int in the array
int arr[10];
int * int_ptr;
int_ptr = arr; // arr is automatically converted to a pointer
int_ptr = arr[1]; // Error: arr[1] is an int that can not be automatically converted to a pointer
int_ptr = &arr[1]; // OK: takes the address it the second element of the array
huh. so now that line DOES NOT assign the location of the first integer in arr[x] to pointer int_ptr? like it did in your previous example? sorry, i guess i'm just having a tough time with this.
It's just that in C/C++ an anything foo[ANY_SIZE]; Is always automatically cast to a anything *bar; whenever needed. And then bar points to the first element in the array (bar+1 to the second element, etc).
sorry, i guess i'm just having a tough time with this.
I don't blame you, pointers can be a pain to get your head around.
OK, try this
A pointer holds the address of an object. When you prefix an object with an ampersand (&object), you are effectively saying you want the address of the object not the object itself.
When you have an array of objects and you refer to it only by name you are given the address of the first element of the array. So with int_ptr = arr;, int_ptr wants an address and arr is converted to the address of the first element so all is good.
When you subscript the array (use []) you are asking for the object so you need to prefix it with the an ampersand to get the address of the element at that position. int_ptr = &arr[1];
Is that any clearer, or am I just muddying the water?
Also, when we say "converted" we don't mean that str itself is actually changed, but rather that the value that is copied from str to p is not a char array, but a char*.
i'm reading the pointer page of the C++ tutorial on THIS VERY SITE. in the "Pointer initialization" section, it says to initialize a pointer for a character constant like this:
char *terry="hello";
it claims this gives terry the value of the memory location of the 'h' in "hello".
not only is this not the case, but that line won't even compile in Dev C++. i get the error "invalid conversion from const char to char"... wtf?
it will only compile if i subscript terry with brackets, in which case the value of terry becomes "hello", not the memory address of the constant.
Pretty much, yes. cout interprets a char* always as "a array of chars" and will print the value the char* points to and all values after that until it hits a \0 character.