Jump to content

[SOLVED] C Question


Recommended Posts

When you 'send an array' into a function, the thing that is sent is just a pointer to the arrays first element.

 

Now I know you can access/change the array by incrementing the pointer (e.g. pointer++;) but when you do something like this:

 

pointer[4] = 6.5;

 

Does this  do the same thing as:

 

pointer + 5 = 6.5

 

So now the pointer in the function is pointing to the fifth element? Or is it still pointing to the first element when you use the 'pointer[4]' method?

Link to comment
https://forums.phpfreaks.com/topic/126853-solved-c-question/
Share on other sites

Let me reword my question...

 

If you send an array into a function, will:

 

array_pointer[4] = 6.5

 

do the same thing as:

 

array_pointer += 4;
array_pointer = 6.5;

 

Because in the 2nd example, array_pointer is now pointing at the fifth element of the array. Is this true in the 1st example as well?

Link to comment
https://forums.phpfreaks.com/topic/126853-solved-c-question/#findComment-656115
Share on other sites

You're right, but will the square brackets do the same thing as you posted?

 

Indeed.

#include <stdio.h>

int array_test(int* foo) {
printf("%d\n", foo[2]);
printf("%d\n", *(foo + 2));
return 1;
}

int main(int argc, char** argv)
{
int test[15] = {1, 15, 16, 7, 19, 8, 99, 15, 4, 8};
array_test(test);
return 0;	
}

Link to comment
https://forums.phpfreaks.com/topic/126853-solved-c-question/#findComment-656601
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.