为什么scanf()的需要和放大器;操作者在某些情况下,而不是其他人? [英] Why does scanf() need & operator in some cases, and not others?

查看:111
本文介绍了为什么scanf()的需要和放大器;操作者在某些情况下,而不是其他人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们需要把&安培; 运营商scanf()的在一个整数数组存储值但不能同时存储在字符数组字符串?

Why do we need to put a & operator in scanf() for storing values in an integer array but not while storing a string in a char array?

int a[5];
for(i=0;i<5;i++)
scanf("%d",&a[i]);

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

我们需要在我们存储的值的地方的地址来传递,因为阵列是一个指向第一个元素。因此,在使用int /浮标阵的情况下,它的基本含义(A + I)

We need to pass in the address of the place we store the value, since array is a pointer to first element. So in the case with int/float arrays it basically means (a+i).

但什么用绳子的情况?

推荐答案

scanf函数接受一个指向无论你是把价值,在第一个实例,你在我的位置传入您的整数具体INT参考数组。在第二个实例要传递到scanf的整个数组。在C语言中,数组指针一个是同义的,可以互换使用的(在某种程度上)。变量s实际上是一个指向内存有连续空间的5个字符。

scanf accepts a pointer to whatever you are putting the value in. In the first instance, you are passing a reference to the specific int at position i in your integer array. In the second instance you are passing the entire array in to scanf. In C, arrays an pointers are synonymous and can be used interchangeably (sort of). The variable s is actually a pointer to memory that has contiguous space for 5 characters.

这篇关于为什么scanf()的需要和放大器;操作者在某些情况下,而不是其他人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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