c - 我什么时候应该在 scanf() 中使用&符号

标签 c string scanf

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

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));
}

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

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

最佳答案

scanf 将特定类型的数据读入地址,这些地址作为第二、第三、第四等参数传递。

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

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

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

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

上面的语句是说:读取%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.
    }
}

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

在这里a=&a[0];你可以直接扫描单个整数值到a指向的地址。

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

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

指针的使用:

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

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

  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];

这是一个二维数组,所以你需要 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");
    }
}

直接扫描

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  
    }
}

您会在 The C Programming Language (Second edition) by Brian W. Kernighan and Dennis M. Ritchie 中找到上述大部分内容.

关于c - 我什么时候应该在 scanf() 中使用&符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18403154/

相关文章:

string - Excel VBA - 在单元格之间复制文本并修改文本

java - 如何从字符串中找出出现次数最多的数字?

c - c中的“计算电路的面积和周长”程序......奇怪的输出

c - 如何使用多线程丢弃 scanf 错误并从 4 个输入整数中识别最高素数

c - 两种scanf使用方式的区别

c - 从线程中定义的函数返回结构

c - 在 typename 中插入注释会报错

c - OpenSSL C Cryptopals 挑战7

c - 将标量和数组(任意维度)从 Fortran 传递到 C

c - 我想打印用户输入的名称和名称前后的一些星星(也输入)