Hi @CodeVault , can you please explain is that right to assign value to array of pointer ? Int*p[2]={10,20}; And if its right can you please explain how it work please???
Well... not really. What you have there is an array of 2 pointers that point to the address 10 and 20 respectively. Those addresses are usually system reserved and will cause a segmentation fault if you ever want to access them. Although, you could do something like this: int x = 13; int y = 20; int* p[2] = {&x, &y}; And p would have a pointer to both x and y
So since an array decays to a pointer if i have for example this code am I saying to the compiler move 4 bytes using x? int arr[ ] = {0, 1}; int x; //arr [ 0 ] == *(arr + 0) for(x=0;x
Given an array: int arr[16]; &arr[0] is calculated as: address_of_array + 0 * (sizeof(arr)) = address_of_array + 0 * 64 = address_of_array &arr is calculated as: address_of_array BUT the type differs. This and the next video explains this concept in-depth: code-vault.net/course/ar67avx6hk:1610029043923/lesson/4rx6a7yjaz:1603820088963
10 line i had an error warning because format-specifier %d accepts integers whereas what you are supplying in your code is a pointer to a memory location. The correct way is to use specifier %p, which will print the address of the memory location in hexadecimal format.(printf("%p %p",(void*)&arr[0],(void*)&arr[4]);) ,but i dont know how you didnt get any error maybe you are a hacker
simple and neat explanation! Thank you!
Awesome channel great work so far.
Great explanation!
thank you for making it simple to graspe.
very neat explanation! Thank you!
great explanation, mate. thanks
Love the vids brother
Hi @CodeVault , can you please explain is that right to assign value to array of pointer ? Int*p[2]={10,20}; And if its right can you please explain how it work please???
Well... not really. What you have there is an array of 2 pointers that point to the address 10 and 20 respectively. Those addresses are usually system reserved and will cause a segmentation fault if you ever want to access them.
Although, you could do something like this:
int x = 13;
int y = 20;
int* p[2] = {&x, &y};
And p would have a pointer to both x and y
So since an array decays to a pointer if i have for example this code am I saying to the compiler move 4 bytes using x?
int arr[ ] = {0, 1};
int x;
//arr [ 0 ] == *(arr + 0)
for(x=0;x
Yes that's true in this case. For multi-dimensional arrays the compiler does a few extra calculations
@@CodeVault Thank you a lot for your answer ❤
Hi , thanks for sharing this , what if i want to get the index , how can i do it ?
If iterating using pointers... You can actually get it using subtraction:
int i = p - &arr[0];
Thanks
what if my array isnt integers? p = &arr[0] gives me Error (active) E0513 a value of type "char *" cannot be assigned to an entity of type "int *"
You'll have to also change the type of the pointer to the type of the array
@@CodeVault oh right cool thanks for the reply!
Just to be sure, "&arr[0]" is equivalent to "&arr" right ?
Given an array:
int arr[16];
&arr[0] is calculated as: address_of_array + 0 * (sizeof(arr)) = address_of_array + 0 * 64 = address_of_array
&arr is calculated as: address_of_array
BUT the type differs. This and the next video explains this concept in-depth: code-vault.net/course/ar67avx6hk:1610029043923/lesson/4rx6a7yjaz:1603820088963
@@CodeVault Okay thanks :D
Btw, add me on osu: osu.ppy.sh/users/2086138
10 line i had an error warning because format-specifier %d accepts integers whereas what you are supplying in your code is a pointer to a memory location. The correct way is to use specifier %p, which will print the address of the memory location in hexadecimal format.(printf("%p
%p",(void*)&arr[0],(void*)&arr[4]);) ,but i dont know how you didnt get any error maybe you are a hacker
I eh like you!