# simple question in arrays

• 05-12-2013
abood1190
simple question in arrays
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 ?
• 05-12-2013
std10093
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!!!!
• 05-12-2013
grumpy
No, it is not "shifting".

Assuming array_name is an array of N elements, and i is an integral value,such as
Code:

```    float key[N];  /* float type picked at random */     int i;```
then the notation "array_name + i" is a pointer with the value equal to "&array_name[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).
• 05-12-2013
rcgldr
Quote:

Originally Posted by abood1190
What does it mean when passing an array in this way :
getArray(key + j, key + j + k, k, k);

It's not passing an array, it's passing two pointers to two locations in the array and then passing the same integer twice. As mentioned above, it's the same as:

getArray(&key[j], &key[j+k], k, k);
• 05-12-2013
abood1190

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?
• 05-12-2013
CodeMonkey
• 05-13-2013
abood1190
Quote:

Originally Posted by CodeMonkey

Code :

Code:

```#include <conio.h> #include <stdio.h> void getArray( int key[] ){   int i;    for ( i=0 ; i<6 ; i++){       printf(" %d ",key[i]);}            } main() {         int i;      int array[]={1,2,3,4,5,6}; getArray(array+1);       getch(); }```

Output :

2 3 4 5 6 2293616
• 05-13-2013
laserlight
You are accessing the array out of bounds because getArray accesses key[5], which corresponds to array[6], which does not exist.
• 05-13-2013
abood1190
Quote:

Originally Posted by laserlight
You are accessing the array out of bounds because getArray accesses key[5], which corresponds to array[6], which does not exist.

Sorry, i didn't understand :)
• 05-13-2013
laserlight
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; }```