Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

c - 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?
");
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
", 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.

question from:https://stackoverflow.com/questions/65920284/how-to-add-a-pointer-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
", i + 1, *(input + i));
}

free(input);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...