Another interesting fact about arrays vs pointers is that the array symbol's location is the location of the first element of the array. I.E. printf("%p", &arr), printf("%p", &arr[0]) both print the same address. Whereas with a pointer, the pointer p lives somewhere and contains the address of somewhere else. So p[i] finds where p is, gets the address that is contained there, add i times the size of the type, and gives you the thing at that final location. Doing arr[i] finds where arr is, add i times the size of the type and gives you what is there (one less indirection). One thing you can do is declare a gobal array `int glb_arr[3] = {1,2,3};` in one file, and in a different file, do `extern int *glb_arr` and in that file try to print `glb_arr[1]` with the most likely result being a segfault.
Before you go for the segfault, you can do array.c: int glb_arr[2] = {0xdeadbeef, 0xbaadc0de}; main.c: #include extern int *glb_arr; int main(void) { printf("the value of the pointer glb_arr is %p ", glb_arr); } $ gcc main.c array.c -o main && ./main The value of the pointer glb_arr is 0xbaadc0dedeadbeef
It's relativly simple if you understand that arrays are purely a compile time datastructure. Sizeof(array) is calculated at compile time as well. A function that takes an array as an argument could be called multiple times with different arrays, thats why the compiler can't precalculate the size inside the function.
happy to see no one noticed the function definition takes an int array but I pass in a char array instead
😂
this language is driving me crazy
@@clymbep3390 Read a book :D
The C Programming language; 2nd ed.
Another interesting fact about arrays vs pointers is that the array symbol's location is the location of the first element of the array. I.E. printf("%p", &arr), printf("%p", &arr[0]) both print the same address.
Whereas with a pointer, the pointer p lives somewhere and contains the address of somewhere else. So p[i] finds where p is, gets the address that is contained there, add i times the size of the type, and gives you the thing at that final location. Doing arr[i] finds where arr is, add i times the size of the type and gives you what is there (one less indirection).
One thing you can do is declare a gobal array `int glb_arr[3] = {1,2,3};` in one file, and in a different file, do `extern int *glb_arr` and in that file try to print `glb_arr[1]` with the most likely result being a segfault.
Before you go for the segfault, you can do
array.c:
int glb_arr[2] = {0xdeadbeef, 0xbaadc0de};
main.c:
#include
extern int *glb_arr;
int main(void) {
printf("the value of the pointer glb_arr is %p
", glb_arr);
}
$ gcc main.c array.c -o main && ./main
The value of the pointer glb_arr is 0xbaadc0dedeadbeef
It's relativly simple if you understand that arrays are purely a compile time datastructure. Sizeof(array) is calculated at compile time as well.
A function that takes an array as an argument could be called multiple times with different arrays, thats why the compiler can't precalculate the size inside the function.
Also pretty sure an array lives in the stack not on the heap (like the bytes the pointer points to that you've malloc'd)
if you use malloc that memory is allocated on the heap which is why you need to free it yourself
thanks mate, very helpful!!!
No problem 👍
You are the man
Thanks a lot
Glad it helped
i dont understand
help
-dx12 or -d3d12 command line argument.
what it mean
it was interesting that because of pointer arithmetic (or other reason) a[3] and 3[a] are the same in C.
not first
accurate statement
agreed