简体   繁体   中英

how to increment an array in c

I am trying to increment an int array using a variable as the increment but it throws an error.

int array[MAXSIZE];
int n;

//fill the array with some numbers
//some other code

The situation here is that once I analyze the first "n" numbers i will not need them again and it will be a waste of cycles to iterate the array from the starting so i want to increment the array by "n". NOTE: because of the type of the problem that I'm working on I cannot just save the position in a variable and start from that position later using array[position]; I have to increment the pointer permanently.

array += n;

and Throws this error: incompatible type in assignment.
I don't know in advance what "n" is going to be. I tried to use array += sizeof(int)*n; but it fails as well.

int array[MAXSIZE]; array is an array and not a pointer. You can not increment an array variable. You can do something like:

int *p = array;
p += whatever;

just make sure that you don't deference p when it is pointing to any element beyond the last element of the array.

The fact that printing out array and p will give you the same output (address) does not make them the same things.

an array name is a type * const array_name so you can't move it.(pay attention it is a const pointer) you can define a pointer yourself and then move it. for example:

int *arr = new int[siz];

or in c:

int *arr = (int *)malloc(n * sizeof(int));

if your source code is in a .c file, you do not need to do casting.

According to the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue . If the array object has register storage class, the behavior is undefined.

So you may not change an array such a way as

array += n;

So just use one more variable of the type int * . For example

int array[MAXSIZE];
int n;

//...

int *p = array;

p += n;
#include<stdio.h>
#include<stdlib.h>
#define MAX 100

int main()
{

    int array*,i,n;

    printf("Enter size of array:\n");
    scanf("%d",&n);

    array = malloc(n*sizeof(int));

    /* code to enter numbers in array */

    array += n;

    //remember to free pointers after you are done with them

    free(array);

    return 0;
}

This should do it.

If you are trying to fill the array with data, then you will need to index into the array.

int array[MAXSIZE];
int i;

for (i = 0; i < MAXSIZE; i++) {
    array[i] = rand();
}

If you genuinely want to make use of a pointer, and do 'pointer arithmetic' then you must be careful.

int array[MAXSIZE];
int *p;
int i;

p = &(array[0]);
for (i = 0; i < MAXSIZE; i++) {
    *p = rand();
    p += 1;
}

Pointer arithmetic may not work as you expect... Doing p += 1 does not move the pointer along one byte, or even one int , but it will move the address along the size of the variable's de-referenced type .

Do an experiment:

#include <stdio.h>

void main(void) {
    struct info {
        int a;
        int b;
    };

    struct info array[10];
    struct info *p;
    int n;

    p = &(array[0]);

    printf("sizeof(*p): %zu\n", sizeof(*p));

    for (n = 0; n < 10; n++) {
        printf("address: %p\n", p);
        p += 1;
    }
}

This will advance p 's value by sizeof(*p) each time around the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM