Jean
Jean

Reputation: 49

How to add a Pointer array?

I have to add in this file a pointer that goes thru the array to add a worth. Does someone know how to make/change a array in this.

#include <malloc.h>

int main() {

int nummers;


printf("Hoeveel nummers wilt u gaan invoeren?\n");
scanf("%i", &nummers);


int* input = (int*) malloc (sizeof(int)*nummers);


for (int i = 0; i < nummers; ++i) {
    printf("Nummer %d:", i+1);
    scanf("%d", &input[i]);
}

for (int i = 0; i < nummers; ++i) {
    printf("Nummer %d is: %d\n", i + 1, input[i]);
}

free(input);
}

Above here is my current code that already works only pointer arrays must be added or changed. enter image description here

This picture above has to be the end result Apreciate help.

Upvotes: 0

Views: 43

Answers (1)

0___________
0___________

Reputation: 67516

Is hard to understand what you mean but probably you ask about pointer arithmetic.

In c language *(pointer + i) === pointer[i] and pointer + i === &pointer[i]so your code using the pointer arithmetic instead of indexes:

int* input = malloc (sizeof(*input)*nummers);

for (int i = 0; i < nummers; ++i) {
    printf("Nummer %d:", i+1);
    scanf("%d", input + i);
}

for (int i = 0; i < nummers; ++i) {
    printf("Nummer %d is: %d\n", i + 1, *(input + i));
}

free(input);

Upvotes: 1

Related Questions