Jack Jack - 4 months ago 22
C Question

Value changing when I pass array into function

I am passing an array of 64 ints into a function in c. It is not displaying the same value when I print p[1] inside the function versus when i print p[1] outside the function in the main. The output I am getting is "this is p[1]: 0" and "this is p[1] inside the function: 1"

below is the code in the main:

for (int i=0; i < 64; i++){
if (((i+1)%4) == 0){
int new = ((i+1)/4)-1;
printf("this is p[1]: %d\n", p[1]);
gg[new] = getgg(&g, &p, i-3);
gp[new] = getgp(&p, i-3);

here is my function:

int getgg(int (*g)[64], int (*p)[64], int i){
printf("this is p[1] inside the function: %d\n", *p[1]);
if((*g[i+3]) || (*p[i+3] && *g[i+2]) || (*p[i+3] && *g[i+2] && *g[i+1]) || (*p[i+3] && *p[i+2] && *p[i+1] && *g[i]) ){
return 1;
return 0;


There is a huge mess there, you need to get used to pointers!

Ok so your function gets g and p which are int *. What you give them is &g and &p, where g and p are also int *. So you pass to your function type int **.

Also g[i+3] is *(g+i+3) so lets see what *g[i+3] is.

g is the address of the address of the first element of your array. g[i+3] is i+3 spots next to the address of the address blah blah blah... so *g[i+3] is the content of that!

It's something you probably have no idea about!

Try to pass g and p instead of &g and &p and remove all the * from your function.