简体   繁体   English

为什么动态分配的int数组在scanf,c语言中使用&?

[英]why dynamic allocated int array use & in scanf, c language?

I learned char array don't use & in scanf because char store point, not string itself.我了解到 char 数组不要在 scanf 中使用 & 因为 char 存储点,而不是字符串本身。

char s1[10];
scanf("%s", s1);

But, why dynamic allocated int array use & in scanf although it use pointer?但是,为什么动态分配的 int 数组在 scanf 中使用 & ,尽管它使用指针?

int *arr = (int *)malloc(sizeof(int)*3);
scanf("%d", &arr[1]);

This is completely wrong:这是完全错误的:

int arr[3] = (int *)malloc(sizeof(int)*3);

because you are trying to allocate memory to an array which is not allowed.因为您试图将内存分配给不允许的数组。
It should be:它应该是:

int * arr = malloc(sizeof(int)*3);

When you do this, the in-memory view would be something like this (assuming malloc is success):当你这样做时,内存中的视图将是这样的(假设malloc成功):

    +-----------+
    |   |   |   |
    +-----------+
    ^
    |
   arr

arr is a pointer pointing to the base address of the memory. arr是指向内存基地址的指针。
arr[1] is the first element of array (a single element of integer array arr ) and arr[1]是数组的第一个元素(整数数组arr的单个元素)和
&arr[1] is the address of first element of array arr . &arr[1]是数组arr的第一个元素的地址。

    +-----------+
    |   |   |   |
    +-----------+
        ^
        |
       &arr[1]

When you take input in first element of array arr you have to pass the address of first element of array to scanf() which is &arr[1] .当您在数组arr第一个元素中输入时,您必须将数组的第一个元素的地址传递给scanf() ,即&arr[1]

& used with scanf() arguments has nothing to do with dynamic allocation. &scanf()参数一起使用与动态分配无关。 If you want to take input in an element of fixed length array (size determined at compile time), the syntax will be same:如果要在固定长度数组(大小在编译时确定)的元素中输入,语法将相同:

int arr[3];

scanf("%d", &arr[1]);

Please let me know if you have any further questions.如果您有任何其他问题,请告诉我。

s1 is a char array. s1是一个字符数组。 The name of the array (s1) is a pointer to char, and so you don't need & with scanf - because %s expects to read the character array.数组的名称 (s1) 是指向 char 的指针,因此您不需要&和 scanf - 因为 %s 期望读取字符数组。

char s1[10];
scanf("%s", s1);

Note that in this case you can also specify the address of s1 in scanf as well because in terms of address, s1, &s1[0], &s1 all point to the same address.请注意,在这种情况下,您也可以在 scanf 中指定s1的地址,因为就地址而言, s1, &s1[0], &s1都指向相同的地址。

char s1[10];
scanf("%s", &s1); // work as well

arr is array of integer. arr是整数数组。 It's correct that the name of the array (ie arr ) is also a pointer (to int).数组的名称(即arr )也是一个指针(指向 int )是正确的。 But for scanf %d, which expect to read an integer, you have to provide the address of the integer you are reading.但是对于期望读取整数的 scanf %d,您必须提供您正在读取的整数的地址。 It's not an array, it's individual integer inside the array that scanf tries to read to - and you have to specify the address of it accordingly: &arr[1]它不是一个数组,它是 scanf 尝试读取的数组的单个整数- 您必须相应地指定它的地址: &arr[1]

int arr[3] = (int *)malloc(sizeof(int)*3);
scanf("%d", &arr[1]);

When you scanf the array of chars (string), in your case s1, you don't need & because s1 stores a pointer to the first element (the element with index 0) of this array.当您扫描字符(字符串)数组时,在您的情况下 s1,您不需要 & 因为 s1 存储指向该数组的第一个元素(索引为 0 的元素)的指针。 With regard to dynamic allocation, malloc return value is a pointer to someplace in memory where your array will be stored.关于动态分配, malloc 返回值是指向内存中将存储数组的某个位置的指针。 So your declaration should look like this:所以你的声明应该是这样的:

int* arr = malloc(sizeof(int) * 3); 
scanf("%d", &add[1]); 
/* here you scanf only the second element of your massive,
 it is int so you have to put & there to pass a pointer */

If you want to initialize the whole massive you can write easy cycle for it.如果你想初始化整个海量,你可以为它编写简单的循环。 Iwould also recommend you to read some documentation about malloc and an article "calloc vs malloc".我还建议您阅读一些关于 malloc 的文档和一篇文章“calloc vs malloc”。

Good luck :)祝你好运 :)

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

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