0

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.

2
  • If an answer has addressed your question, you should accept it rather than putting "closed" or "solved" in the title. Also "closed" means something specific on SO which differs from how you're using it. Commented Jan 27, 2021 at 13:48
  • Use stdlib.h for malloc(), not malloc.h. The latter is an obsolete, non-standard one. Commented Jan 27, 2021 at 13:50

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

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.