我什么时候应该使用带有scanf(&)的&符号

问题描述 投票:15回答:3

使用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我必须使用它?

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

c string scanf
3个回答
25
投票

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

int var;

这里var是值,&var是地址

scanf("%d",&var);

上面的语句说==>读取%d(整数)类型的数据到&var地址


char s[20];

这里的地址不是价值。因为这里是一个字符数组(我们称之为字符串)

数组名称本身表示其地址。 s ==&s [0],这两者都是一样的。

scanf("%s",s);

上面的语句说==>读取%s(字符数组)数据的类型,从s开始到地址位置。


int a[20];

请看这个代码

#include<stdio.h>
#define MAX 5

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

     }

在C中我们没有任何单个格式说明符一次扫描整数组,就像在%s的帮助下扫描字符组一样。

这里a =&a [0];

您可以直接将单个整数值扫描到a指向的地址。

scanf("%d",a);

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

如果输入10则打印一个[0] = 10。

指针的用法:

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

您可以移动指针位置以读取数组。你可以读出没有移动指针位置的数组。

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

    #include<stdio.h>
    #define MAX 5

      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

  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

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

你将在下面的书中找到上述大部分内容。

please goto this link : read the wiki and read
The C Programming Language (Second edition) - Brian W. Kernighan and Dennis M. Ritchie


7
投票

因为C字符串有一种char []。数组名称具有它的地址值,但int变量没有,你需要使用&

编写void main是错误的,你应该总是使用int main


1
投票

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

数组名称返回数组的第一个元素的指针(存储数组的内存位置),标量变量的名称返回标量的值,因此您需要使用&运算符来获取标量的内存位置,其中你需要写下这个值。

© www.soinside.com 2019 - 2024. All rights reserved.