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
817 views
in Technique[技术] by (71.8m points)

calculator - Printing an array in C with pointer

I am writing a calcutalor which takes multiple inputs. I seperated numbers and operations to 2 different arrays. But I can't print the arrays because I am not familier with pointers and it is giving me their position.

#include <stdio.h>
int main(void){
    printf("You will enter your operations in number - operator order.
");
    printf("How many numbers do you have to calculate?");
    int how_many; //how many numbers the user has in his/her whole operation
    scanf("%d",&how_many);
    int number_entry = how_many; //for example 1+2*9/5 has 4 numbers
    int operator_entry = how_many - 1; //and 3 operators
    int *number_in; //for every number as 1, 2 ,9 ,5 according to example
    char *operator_in; // and for every operator as +, * , / according to example
    int numbers[number_entry];
    int ops[operator_entry];
    int j;
    printf("
");
    for(j=0;j<operator_entry;j++){
        printf("Enter the number : ");
        scanf("%d",&number_in);
        numbers[j] = number_in;
        printf("Enter the operation as + for add, - for substract, * for multiply and / for division: ");
        scanf(" %c", &operator_in);
        ops[j] = operator_in;
    };
    printf("Enter the number : ");
    scanf("%d",&number_in); 
    numbers[number_entry+1] = number_in;
        
    
    int loop;

    for(loop = 0; loop < number_entry; loop++)
        printf("%d ", &numbers[loop]);
    
    for(loop = 0; loop < operator_entry; loop++)
        printf(" %c", ops[loop]);
    
    return 0;
}

'''

here you can see the output

output

question from:https://stackoverflow.com/questions/65834994/printing-an-array-in-c-with-pointer

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

1 Answer

0 votes
by (71.8m points)

Your first printing loop should have

printf("%d ", numbers[loop]);

instead of

printf("%d ", &numbers[loop]);

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

...