我什么时候应该在 scanf() 中使用 & 号 [英] When should I use ampersand with scanf()

查看:57
本文介绍了我什么时候应该在 scanf() 中使用 & 号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

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

推荐答案

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

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

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

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

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

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

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

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

上面的语句说:将%s(字符数组)类型的数据读入从s开始的地址位置.

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

#include<stdio.h>
#define MAX 5

int main()
{
    int a[MAX];
    int i;
    printf("Enter Values of array
");
    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指向的地址.

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

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

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

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

指针的使用:

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

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. 通过移动指针位置读取数组

  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
");
    for(i=0;i<MAX;i++)
    {
        printf("Enter a[%d] =  ",i);
        scanf("%d",ptr);
        ptr++; //moving pointer 
    }
}

  • 在没有移动指针位置的情况下读取数组.

  • 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
    ");
        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) 位置.

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

    int a[5][5];
    

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

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

    #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 :
    ");
        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:
    
    ");
        for(i=0;i<MAX;i++)
        {
            for(j=0;j<MAX;j++)
            {
                printf("%d",*(pointer[i]+j));
                printf("		");
            }
            printf("
    
    ");
        }
    }
    

    直接扫描

    printf("Enter elements :
    ");
    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  
        }
    }
    

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

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

    这篇关于我什么时候应该在 scanf() 中使用 & 号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆