How do you change the starting index of an array in C?

I am trying to switch the starting index of an ints array to C. More precisely, I have the following array:

{ int array[4] = {1,2,3,4};}

I want to change the {array} to an array of 3 elements containing {2,3,4}. I was wondering if there is an easy way to do this with pointers and without offsetting all the elements in the array. I am also glad if {array} remains an array of 4 elements, if the first 3 elements are {2,3,4}. I tried the following:

{array = &(array[1])}

However, this does not work, and I get an error talking about {incompatible types when assigning type 'int [4] from type' int *}.

I got the impression that the C array is a reference to the first element of the array.

Can anyone help me with this?

+4
source share
3

:

,

:

int array[4] = { 1, 2, 3, 4 };

/ n n-1:

:

int i;
for (i = 1; i < n; ++i)
  array[i-1] = array[i];

memmove, .

,

memmove(array, array+1, sizeof array - sizeof *array);

, .

,

, , .

,

int array1[4] = { 1, 2, 3, 4 };
int array2[3];
int i;

for (i = 0, i < SIZE_OF_SMALLER_ARRAY; ++i)
  array2[i] = array1[i+1]

[1] [0]

, / :

int array[4] = { 1, 2, 3, 4 };
int *pArray = &array[1];

pArray , [1] pArray [0]

int

int array[4] = { 1, 2, 3, 4 };
int *parray[3];

int i;
for (i = 0; i < SIZE_OF_SMALLER_ARRAY; ++i)
  parray[i] = &array[i+1];

, , ...

int array1[4] = { 1, 2, 3, 4 };
array1 = &array1[1]; // !! Compiler Error !!

array1 - non-modifiable l-value. .

...

int array1[4] = { 1, 2, 3, 4 };
int array2[4] = array[1] // !! Compiler error !!

, .

+9

, p array. array .

#include <stdio.h>

int main(void) {
        int array[4] = {1,2,3,4};
        int *p = array + 1;
        printf("%d %d %d\n", p[0], p[1], p[2]);
        return 0;
}

"2 3 4"

memmove, . memcopy, .

, .

+1

"" "", .. , C/++.

, , - , .

0
source

All Articles