简体   繁体   中英

Inputting integer values into an Array in C

I was trying to figure out arrays. I'm having the same problem with single dimensional arrays and multidimensional arrays, when I input the value and try and return the value later in the code it return the wrong numbers.

#include <stdio.h>

main ()
{
    int arrayPrimary[2][2];
    int x,y,a,b;

    for(x=0; x<2; x++)
    {
        for (y=0; y<2; y++)
        {
            int* z;
            *z==arrayPrimary[x][y];
            printf("please enter a value for [%d][%d]:",x,y);
            scanf("%d", &z);    
        }
    }

    for(a=0; a<2; a++)
    {
        for(b=0; b<2;b++)
        {
            printf ("The current value of [%d][%d] is:%d\n",a,b,arrayPrimary[a][b]);
        }

    }

    return 0;
}

This part of code

  int* z;
    *z==arrayPrimary[x][y];
    printf("please enter a value for [%d][%d]:",x,y);
    scanf("%d", &z);

Should read

    printf("please enter a value for [%d][%d]:",x,y);
    scanf("%d", &arrayPrimary[x][y]);

You should also consider checking the return value of scanf

There are several things wrong here.

First of all, you use == (comparison) where you meant to use = (assignment) (This is rather ironic, considering how often people the reverse of that mistake). This means that *z is never initialized. For that matter, z itself is never initialized, so you're accessing garbage memory.

Your error probably occurs when you try to write an integer ( "%d" ) into a pointer ( z ). Remember, scanf takes a pointer to where you want the input to be written to, so if your input is an int , you'll want to pass an pointer to an int . You're passing a pointer to a pointer to an int .

The pointer logic here is probably what has you the most confused, so let's go through that in detail:

  • arrayPrimary[x][y] is an integer that has an address in memory like any normal variable.
  • scanf needs to know this location in order to write the value into your array.
  • It looks like you're trying to use another variable, z , to serve as an argument to scanf . However, even if you copy the value of arrayPrimary[x][y] to the address z points to, z is still a different variable from your array.
  • The memory address of z ( &z ) has no relation whatsoever to your array. When you pass &z to scanf , scanf will look at this address and write the input to it. Therefore, you are directing the input into z , not to your array.

Try this:

for (y=0; y<2; y++)
{
    int *z = &arrayPrimary[x][y];
    printf("please enter a value for [%d][%d]:",x,y);
    scanf("%d", z);

}

This way, it goes like this:

  • You create a pointer, z , that points to the data you want to change (namely arrayPrimary[x][y] ).
  • You pass this data yo scanf , which writes input to where z points to-- namely, arrayPrimary[x][y] .

However, there's no need for a separate pointer. You can just write:

for (y=0; y<2; y++)
{
    printf("please enter a value for [%d][%d]:",x,y);
    scanf("%d", &arrayPrimary[x][y]);
}

If z already is a pointer to your desired memory location, there's no point in using &z in your scanf; just use z .

(EDIT: Assuming you get your = straightened out as pointed out in the comments.)

With your line:

int *z;

you just set up a pointer to an int . In your next line:

*z==arrayPrimary[x][y];

you first dereference that pointer ( *z ), so you say 'I now want to use the value, the pointer points to'. Next you compare that value with arrayPrimary[x][y] and the result of that comparison is discarded. Here you should get a warning by your compiler, something like 'statement has no effect' or so. So this line effectively does nothing. Try:

int *z = &arrayPrimary[x][y];

instead of the two lines I talked about the last few lines.

#include<stdio.h>
#include<iostream>
int main ()
{
    int arrayPrimary[2][2];
    for(int i=0; i<2; i++)
    {
        for (int j=0; j<2; j++)
        {
            printf("Please Enter value for %d%d",i,j);
            scanf("%d",&arrayPrimary[i][j]);
        }
    }
    for(int i=0; i<2; i++){
        for(int j=0; j<2;j++){
            printf("%d",arrayPrimary[i][j]);
            printf(" ");
        }
        printf("\n");
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM