thomashw Posted October 3, 2008 Share Posted October 3, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/126853-solved-c-question/ Share on other sites More sharing options...
thomashw Posted October 3, 2008 Author Share Posted October 3, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/126853-solved-c-question/#findComment-656115 Share on other sites More sharing options...
DarkWater Posted October 3, 2008 Share Posted October 3, 2008 I'm pretty sure you'd need: *(array_pointer + 5) = 6.5; Quote Link to comment https://forums.phpfreaks.com/topic/126853-solved-c-question/#findComment-656248 Share on other sites More sharing options...
thomashw Posted October 3, 2008 Author Share Posted October 3, 2008 You're right, but will the square brackets do the same thing as you posted? Quote Link to comment https://forums.phpfreaks.com/topic/126853-solved-c-question/#findComment-656264 Share on other sites More sharing options...
DarkWater Posted October 3, 2008 Share Posted October 3, 2008 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; } Quote Link to comment https://forums.phpfreaks.com/topic/126853-solved-c-question/#findComment-656601 Share on other sites More sharing options...
thomashw Posted October 4, 2008 Author Share Posted October 4, 2008 Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/126853-solved-c-question/#findComment-657177 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.