Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我应该在什么时候将安培与scanf()一起使用_C_String_Scanf - Fatal编程技术网

我应该在什么时候将安培与scanf()一起使用

我应该在什么时候将安培与scanf()一起使用,c,string,scanf,C,String,Scanf,在使用scanf()时,在c中使用符号AND的规则是什么 为什么对于字符串我不需要使用符号and,而对于int我必须使用它 有没有关于何时使用符号的规定 在C语言中,字符串是字符数组(以\0字符结尾) 数组名返回数组第一个元素的指针(存储数组的内存位置),标量变量名返回标量的值,因此需要使用&运算符获取标量的内存位置,您需要在其中写入值。因为C字符串的类型为char[]。数组名有其地址的值,但int变量没有,您需要使用& 写入void main是错误的,您应该始终使用int mainscanf将

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

为什么对于字符串我不需要使用符号and,而对于
int
我必须使用它


有没有关于何时使用符号的规定

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


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

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


写入
void main
是错误的,您应该始终使用
int main

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.
    }
}
如果输入
10
,则打印
a[0]=10

指针的使用:

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

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

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

    #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 
        }
    }
    
    这是二维数组,所以需要扫描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");
        }
    }
    
    #包括
    #定义最大值5
    int main()
    {
    int a[MAX][MAX],i,j;
    int*指针[MAX];
    
    对于(i=0;iRead@PP在他的评论中可能听起来有点居高临下,但他绝对正确:先读一本好的C编程书。Kernighan&Ritchie的《C编程语言》是一本很好的资源。@PP绝对正确。在你明白为什么一个变量需要&而不是另一个变量之前,你不能开始编写C。这就像试图开车时不知道油门和刹车的区别。
    #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 
        }
    }
    
    #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 
        }
    }
    
    int a[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  
        }
    }