What does it mean when passing an array in this way :
key[16];
j+=0;
k+=0;
getArray(key + j, key + j + k, k, k);
is that shifting or something similar ?
Printable View
What does it mean when passing an array in this way :
key[16];
j+=0;
k+=0;
getArray(key + j, key + j + k, k, k);
is that shifting or something similar ?
Try to print the first element of the 1st argument and of the second argument inside the function. Before calling the function initialize the key array to something like 0,1,2,3,...,16 or something like that. Then remember how pointers and arrays are connected.
And do not forget, always post your code in code tags!!!!
No, it is not "shifting".
Assuming array_name is an array of N elements, and i is an integral value,such as
then the notation "array_name + i" is a pointer with the value equal to "&array_name[i]".Code:float key[N]; /* float type picked at random */
int i;
Any attempt to use that pointer (e.g. inside your function) will yield undefined behaviour unless i is a valid index (for an array of N elements, i is only a valid index if it is between 0 and N-1).
your comments are very helpful guyz
I notice from the output that it changes the result of some elements
lets say this code :
Code:key[16];
j+=0;
k+=0;
getArray(key + j, key + j + k, k, k);
the + will change 1 value of the array key[] from the right when j is 1 to a weird value looks like an address and when it is -1 that will change 1 value from the left of the array !! why is that behavior?
Please provide a minimal complete program, its ouput, and your question.
You are accessing the array out of bounds because getArray accesses key[5], which corresponds to array[6], which does not exist.
Compile and run this program:
Code:#include <stdio.h>
void printArray(int numbers[], int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d ", numbers[i]);
}
}
int main(void)
{
int array[] = {1, 2, 3, 4, 5, 6, 7};
printArray(array + 1, 6);
return 0;
}