3

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.

12
  • 4
    Array is not a pointer. Commented Mar 24, 2017 at 16:41
  • array is not a pointer. it's type is int[MAXSIZE] Commented Mar 24, 2017 at 16:41
  • 7
    Just make a pointer and use that int* p_toArray = array; Commented Mar 24, 2017 at 16:41
  • 3
    An array is not a pointer. You cannot increment an array. (you could increment a pointer) Commented Mar 24, 2017 at 16:41
  • 1
    @Ryan What would someone expect p_toArray++; to do that it doesn’t? Point to the next element of the array, perhaps, instead of the address immediately past the end of the array? Or, depending on context, the reverse. Commented Mar 24, 2017 at 16:56

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I thought an array an a pointer was the same haha. However many times in my code im using the syntax array[x]. If I do what you are saying can I do p[x]; ?
@Ric Yes, you can :)
Sure you can and again make sure x is within a proper limit.
0

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;

Comments

0

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.

2 Comments

Have you noticed the language?
Even in C++ , int *arr = new int[siz]; is usually pretty poor form.
0

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.

Comments

0
#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.

5 Comments

...and poorly indented. And #DEFINE should be in lower case.
Could you please explain how is it leaking memory? I'm willing to improve.
malloc a memory, then loose the original pointer making it impossible to free it afterward (not that this code is trying to..)
You mean to free() the pointer after it has been used? I assumed OP would know how to do that.
mallocing memory just adds an unnecessary layer of complexity, that some people might see and think is necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.