0

Pointer to acess array .. i wanna acess to 2nd place in the array with ++ operator it doesn't work??

#include <iostream> using namespace std; int main() { int numbers[4]={10,20,30,40}; cout<<numbers[3]<<endl; cout<<*numbers<<endl; numbers ++; cout<<*numbers<<endl; }

19th Jul 2020, 9:37 AM
Mostafa Ibrahim
Mostafa Ibrahim - avatar
8 Respostas
+ 2
In that case, msg and ptr both points to the same memory location (beginning of the "hello" string). However, ptr is a pointer data-type, and msg is an array-datatype, and that makes all the difference : you cannot increment an array, since it has to be a constant value. On the contrary, a pointer doesn't have to be constant, and you can increment it.
19th Jul 2020, 10:15 AM
Théophile
Théophile - avatar
19th Jul 2020, 9:53 AM
Gabriel Ilie
Gabriel Ilie - avatar
+ 2
Ah. Nevermind my question Is there any difference between i++ and i+=1 or i=i+1 in its deeper function?
19th Jul 2020, 9:57 AM
LosT
+ 2
Even if an array is basically a pointer, it does not behave totally like pointers : you cannot perform increment / decrement on it. However, you can do : *(number + n) [Edit] number[n] // or [n]number Is syntax sugar for *(number + n)
19th Jul 2020, 10:03 AM
Théophile
Théophile - avatar
0
Why should this work? i=1 i++ numbers[i]
19th Jul 2020, 9:49 AM
LosT
0
Why increment work in charcter array like this Char msg[ ]="hello"; Ptr=msg; Ptr++; *ptr='a'; It will be hallo
19th Jul 2020, 10:10 AM
Mostafa Ibrahim
Mostafa Ibrahim - avatar
0
Nivid , i am beginner so i think no difference or what?
19th Jul 2020, 10:12 AM
Mostafa Ibrahim
Mostafa Ibrahim - avatar
0
Oh sorry i misunderstand increment done to pointer "ptr"which is firstly assigned to array Thank you all 💚
19th Jul 2020, 10:17 AM
Mostafa Ibrahim
Mostafa Ibrahim - avatar