简体   繁体   English

在C语言中将整数值输入到数组

[英]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 您还应该考虑检查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. 这意味着*z不会被初始化。 For that matter, z itself is never initialized, so you're accessing garbage memory. 因此, z本身从未初始化,因此您正在访问垃圾内存。

Your error probably occurs when you try to write an integer ( "%d" ) into a pointer ( z ). 当您尝试将整数( "%d" )写入指针( 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 . 请记住, scanf一个指向您要将输入写入的位置的指针,因此,如果您的输入是int ,则需要将一个指针传递给int You're passing a pointer to a pointer to an int . 您正在传递一个指向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. arrayPrimary[x][y]是一个整数,在内存中的地址与任何普通变量一样。
  • scanf needs to know this location in order to write the value into your array. scanf需要知道该位置,以便将值写入数组。
  • It looks like you're trying to use another variable, z , to serve as an argument to scanf . 似乎您正在尝试使用另一个变量z作为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. 但是,即使将arrayPrimary[x][y]的值复制到z指向的地址,z仍然是与数组不同的变量。
  • The memory address of z ( &z ) has no relation whatsoever to your array. z( &z )的内存地址&z数组没有任何关系。 When you pass &z to scanf , scanf will look at this address and write the input to it. 当将&z传递给scanfscanf将查看该地址并将输入内容写入该地址。 Therefore, you are directing the input into z , not to your array. 因此,您要将输入定向到z ,而不是数组。

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] ). 您创建一个指针z ,该指针指向要更改的数据(即arrayPrimary[x][y] )。
  • You pass this data yo scanf , which writes input to where z points to-- namely, arrayPrimary[x][y] . 您传递此数据yo scanf ,它将输入写入z指向的位置,即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; 如果z已经是指向所需内存位置的指针,则在scanf;中使用&z没有意义。 just use z . 只需使用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 . 您只需设置一个指向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'. 您首先取消引用该指针( *z ),所以您说“我现在要使用该指针所指向的值”。 Next you compare that value with arrayPrimary[x][y] and the result of that comparison is discarded. 接下来,您将该值与arrayPrimary[x][y]进行比较,并且该比较的结果将被丢弃。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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