1

In this code i want to user to input numbers in the function then print the array in the main method, but when i try to print in the main method it gives me random numbers.

for eg: 1 2 3 gives 1 3 1895586112

void arrayInput(int *arr) {
    int x;
    int y;
    int z;
    printf("enter 3 numbers =");
    scanf("%d %d %d", &x,&y,&z);
    arr[0]=x;
    arr[1]=y;
    arr[2]=z;
}

int main(int argc, char **argv){
    int *arr[3];
    arrayInput(&arr);
    int i;
    for(i=0; i<3; i++)  
    {  
        printf("%d ", arr[i]);  
    } 
}

I want to change the array values without changing the method or param type

2
  • 3
    int *arr[3]; -> int arr[3]; Commented Apr 7, 2021 at 6:53
  • Just pay attention to your compiler. The code you have posted isn't valid C and the compiler told you as much. If you are a beginner and using gcc then compile with something like -Wall -Wextra -Werror. Commented Apr 7, 2021 at 7:30

2 Answers 2

1

int *arr[3] is an array of pointers to int. But you want an array of int.

Simply change:

int *arr[3];
arrayInput(&arr);

to

int arr[3];
arrayInput(arr);

This should be covered in your C text book.

Bonus:

You can remplace this convoluted code:

void arrayInput(int *arr) {
    int x;
    int y;
    int z;
    printf("enter 3 numbers =");
    scanf("%d %d %d", &x,&y,&z);
    arr[0]=x;
    arr[1]=y;
    arr[2]=z;
}

with this:

void arrayInput(int *arr) {
    printf("enter 3 numbers =");
    scanf("%d %d %d", &arr[0],&arr[1], &arr[2]); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Better int arrayInput(int *arr) { ... return scanf("%d %d %d", &arr[0],&arr[1], &arr[2]) == 3; }
@0___________ scanf isn't fit for user input anyway, but using it without error checks for toy programs is OK IMO.
Thank you, this seems to work perfectly. Also the bonus was helpful too :)
1

There are multiple issues with your code. First you have declared int *arr[3]; which would mean pointer arrays. For your task a simple array is needed. The array variable contains the address of the first element, so you don't need &. A simple corrected code is as follows:

#include <stdio.h>

void arrayInput(int arr[]) {
    int x;
    int y;
    int z;
    printf("enter 3 numbers =");
    scanf("%d %d %d", &x,&y,&z);
    arr[0]=x;
    arr[1]=y;
    arr[2]=z;
}

int main(int argc, char **argv){
    int arr[3];
    arrayInput(arr);
    int i;
    for(i=0; i<3; i++)  
    {  
        printf("%d ", arr[i]);  
    } 
}

3 Comments

"which would mean pointer to arrays" NO it means an array of 3 pointers to int. A pointer to array of int[3] would be int (*arr)[3]; Much much different. An array of pointers is just that, an array where the elements are pointers. A pointer to array is a single-pointer to an array type. (but I knew what you meant and your code correction is correct, but the language may leave the OP with the wrong impression of what a pointer to array is)
Yes, you are right, I had a 'to' as mistake.
Much better. Not like I've never had a stray "to" or other misnomer in my answers :)

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.