Your first problem is a matter of a type mismatch:
Code:
void modify(int *k)
versus:
Code:
int *k=NULL;
modify(&k);
&k is an int**, which does not match the int* parameter.
Your second problem is that k is a null pointer. This means that you cannot dereference it. What you probably want to do is to set k to point to something, and then pass k to modify so that what it points to can have 5 added to it. For example:
Code:
int *k = a + 1; // k points to a[1]
modify(k); // a[1] += 5
However, the fact that you are trying to use k to control the loop over the array a suggests that you actually want k to contain the value of 5 (i.e., the number of elements in the array a). This means that your entire setup is wrong. What you want would then be:
Code:
void set_array_size(int *array_size)
{
*array_size = 5;
}
int main(void)
{
int i;
int a[] = {3, 6, 2, 5, 1};
int k;
set_array_size(&k);
for (i = 0; i < k; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
That said, this is something of a Rube Goldberg machine. Normally, we would just write:
Code:
int main(void)
{
int a[] = {3, 6, 2, 5, 1};
const size_t k = sizeof(a) / sizeof(a[0]);
int i;
for (i = 0; i < k; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}

Originally Posted by
supernova_2706
why don't we need to dereference the pointer k inside the loop
You do, since k is a pointer. What's happening is that since k is a pointer, either i is getting implicitly converted to a pointer to int or k is getting implicitly converted to int (sorry, I don't remember the conversion rules for this unusual case), and the compiler happened to accept this (probably reluctantly, as you should be getting a warning). It doesn't mean your code is correct, even if it happens to "work". As you passed the address of k to modify() earlier, what might have happened is that your pointer to int was reinterpreted as an int with the value of 0, and then had 5 added to it. So, when you convert back again from a pointer to an int, or when you're comparing an int converted to a pointer, you might get a result similiar to just comparing say, an integer with the value 5. But this convoluted mess is not the way to go.
Oh, and you should compile at high warning levels and pay attention to the warnings.