> in the warning programm, we create a pointer called arr, by saying int*arr;
Yes.
> then we give the actuall arr value(which isnt initialized yet) to the function f, since the argument &arr moves us from *arr to arr?
No, remember & takes you further away from the end type, so &arr is of type int**
> Or is it the address of arr since the deklaration of int* arr didnt store it as a pointer, but just told the compiler to see the arr variable as a pointer/address variable?
Yes, f expects the address of arr, and yes arr is a pointer to some address.
> function f expects a pointer variable (is a pointer then same thing as an address in the memory?)
Yes.
> so i guess what we gave was the adress of arr, which is now cast to *ptr. *ptr is set to the array we want to have in it, so *ptr must be the pointer to *arr.
If you change 'cast' to 'dereference', yes.
> So if i see this correctly, f(&arr) gave him the address of the pointer to arr, which is the same as a pointer to a pointer to arr?
Yes, but the "which...." should read as "which has the type pointer to pointer to int".
> Then you set only the pointer from the pointer pointer variable to the actual ar. So i guess it is like saying the actual value of the pointer pointer is ar
ptr is dereferenced, to update the value of arr in main, with the start of the array called ar.
> But then, in the main, you can still access arr with the [index], not needing to address its actual pointer? Why is that?
Because saying arr[i] is the equivalent of saying *(arr + i)
Question 6.3
then
Arrays and Pointers
You can hide one of the stars using a typedef.
Code:
#include <stdio.h>
typedef int* T;
void f(T *);
void f(T *ptr)
{
static int ar[] = { 1, 2, 3, 4 };
*ptr = ar;
}
int main()
{
T arr;
f(&arr);
for (int i = 0; i < 4; i++) {
int d = arr[i];
printf("The %d-th element is %d.\n", i, d);
}
return 0;
}
Then compare with say just an int.
Code:
#include <stdio.h>
typedef int T;
void f(T *);
void f(T *ptr)
{
*ptr = 42;
}
int main()
{
T val;
f(&val);
printf("The answer is %d\n",val);
return 0;
}