C语言 我什么时候应该在 scanf() 中使用 & 号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18403154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:14:22  来源:igfitidea点击:

When should I use ampersand with scanf()

cstringscanf

提问by Dkova

What are the rules for using ampersand in c while using scanf()?

使用时在c中使用&符号的规则是什么scanf()

struct Student 
{
  char name[20];
  int id;
};

int main(void)
{
  struct Student std1;
  printf("enter name and id of std1\n");
  scanf("%s %d", std1.name, &(std1.id));
}

Why for String do I not need to use the the ampersand and for intI have to use it?

为什么对于 String 我不需要使用&符号而int我必须使用它?

Is there a rule on when to use the ampersand sign?

是否有关于何时使用&符号的规则?

回答by Gangadhar

scanf reads particular types of data into addresses which are passed as second, third, fourth and so on arguments.

scanf 将特定类型的数据读取到作为第二、第三、第四等参数传递的地址中。

int var;
scanf("%d",&var);

Here varis value and &varis address. The above statement says: read %d(integer) type of data into &varaddress.

这里var是值,&var是地址。上面的语句说:将%d(整数)类型的数据读入&var地址。

char s[20];
scanf("%s",s);

Here sis address not the value because sis a character array (string). An array name itself indicates its address. s == &s[0], these are both the same.

这里s是地址而不是值,因为它s是一个字符数组(字符串)。数组名称本身指示其地址。s == &s[0],这些都是一样的。

The above statement says: read %s(array of characters) type of data into address location starting from s.

上面的语句说:将%s(字符数组)类型的数据读入从s.

#include<stdio.h>
#define MAX 5

int main()
{
    int a[MAX];
    int i;
    printf("Enter Values of array\n");
    for(i=0;i<MAX;i++)
    {
        printf("Enter a[%d] =  ",i);
        scanf("%d",&a[i]); // reading each time single integer value starting index with 0 and ending index MAX-1.
    }
}

There is no single format specifier to scan group of integers at a time as like scanning group of characters at time with the help of %s.

没有单一的格式说明符可以一次扫描一组整数,就像在 %s 的帮助下一次扫描一组字符一样。

And here a=&a[0];you can scan single integer value directly to the address which is pointed by a.

在这里,a=&a[0];您可以将单个整数值直接扫描到a.

scanf("%d",a);
printf("a[0]=%d\n",a[0]);

if you enter 10then prints a[0]=10.

如果你输入10然后打印a[0]=10

Usage of Pointers:

指针的使用:

if you use pointers as shown below, then you will come to know how to increment the pointer and get the values into different locations of array.

如果您使用如下所示的指针,那么您将知道如何增加指针并将值放入数组的不同位置。

You can move the pointer location to read arrays. You can read arrays without moving pointer location.

您可以移动指针位置来读取数组。您可以在不移动指针位置的情况下读取数组。

  1. Reading arrays by moving pointer locations

    #include<stdio.h>
    #define MAX 5
    
    int main()
    {
        int a[MAX];
        int i;
        int *ptr;
        ptr = &a[0];
        printf("Enter Values of array\n");
        for(i=0;i<MAX;i++)
        {
            printf("Enter a[%d] =  ",i);
            scanf("%d",ptr);
            ptr++; //moving pointer 
        }
    }
    
  2. Reading arrays with out moving pointer locations.

    #include<stdio.h>
    #define MAX 5
    
    int main()
    {
        int a[MAX];
        int i;
        int *ptr;
        ptr = &a[0];
        printf("Enter Values of array\n");
        for(i=0;i<MAX;i++)
        {
            printf("Enter a[%d] =  ",i);
            scanf("%d",ptr+i); //we are not moving ptr position we scaning each time into next location by incrementing i 
        }
    }
    

    When a pointer is incremented then the increment is dependent on the type of pointer. Here ptris an integer pointer so ptr+1will increment ptr+sizeof(int)locations.

  1. 通过移动指针位置读取数组

    #include<stdio.h>
    #define MAX 5
    
    int main()
    {
        int a[MAX];
        int i;
        int *ptr;
        ptr = &a[0];
        printf("Enter Values of array\n");
        for(i=0;i<MAX;i++)
        {
            printf("Enter a[%d] =  ",i);
            scanf("%d",ptr);
            ptr++; //moving pointer 
        }
    }
    
  2. 读取没有移动指针位置的数组。

    #include<stdio.h>
    #define MAX 5
    
    int main()
    {
        int a[MAX];
        int i;
        int *ptr;
        ptr = &a[0];
        printf("Enter Values of array\n");
        for(i=0;i<MAX;i++)
        {
            printf("Enter a[%d] =  ",i);
            scanf("%d",ptr+i); //we are not moving ptr position we scaning each time into next location by incrementing i 
        }
    }
    

    当指针递增时,增量取决于指针的类型。这ptr是一个整数指针,因此ptr+1将增加ptr+sizeof(int)位置。

int a[5][5];

This is two dimensional array so you require 5 pointers to scan so I was declared pointer array.

这是二维数组,所以你需要 5 个指针来扫描,所以我被声明为指针数组。

#include<stdio.h>
#define MAX 5

int main()
{
    int a[MAX][MAX],i,j;
    int *pointer[MAX];

    for(i=0;i<MAX;i++)
        pointer[i]=&a[i][0]; //initializes the pointers 

    printf("Enter elements :\n");
    for(i=0;i< MAX;i++)
    {   
        for(j=0;j<MAX;j++)
        {
            printf("Enter the a[%d][%d] element: ",i,j);
            scanf("%d",pointer[i]+j); //each time you will move like 00 01 02 03 04 and second time 10 11 12 13 14 and so on...
            //printf("%u or %x",pointer[i]+j,pointer[i]+j);//un comment this line and see the addresses how the address incrementing for each element
        }
    }

    printf("The Given Matrix:\n\n");
    for(i=0;i<MAX;i++)
    {
        for(j=0;j<MAX;j++)
        {
            printf("%d",*(pointer[i]+j));
            printf("\t\t");
        }
        printf("\n\n");
    }
}

Direct scanning

直接扫描

printf("Enter elements :\n");
for(i=0;i< MAX;i++)
{   
    for(j=0;j<MAX;j++)
    {
        printf("Enter the a[%d][%d] element: ",i,j);
        scanf("%d",&a[i][j]); //we can't do like this a++ or ++a or a+i this is illegal in C. for that purpose we are using pointers  
    }
}

You will found most of the above stuff in The C Programming Language (Second edition) by Brian W. Kernighan and Dennis M. Ritchie.

您可以在Brian W. Kernighan 和 Dennis M. Ritchie 的 The C Programming Language (Second edition) 中找到上述大部分内容。

回答by Yu Hao

Because C strings have a type of char []. The array name has the value of it's address, but an intvariable doesn't, you need to use &

因为 C 字符串的类型为char []. 数组名称具有其地址的值,但int变量没有,您需要使用&

And it's wrong to write void main, you should always use int main.

而且写错了void main,应该一直用int main

回答by Nemanja Boric

In C, strings are arrays of characters (terminated with \0character).

在 C 中,字符串是字符数组(以\0字符结尾)。

Array name returns the pointer of the first element of a array (memory location where array is stored), and name of the scalar variable returns the value of the scalar, so you need to use &operator to get the memory location of scalar in which you need to write the value.

数组名返回数组第一个元素的指针(数组存放的内存位置),标量变量名返回标量的值,所以需要使用&运算符来获取标量所在的内存位置需要写入值。