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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.