C编程:'和;'在scanf函数阵列 [英] C programming: '&' with arrays in scanf

查看:104
本文介绍了C编程:'和;'在scanf函数阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的节目一个警告,它说:

I got a warning in my program, and it says:

格式'%C'型预期'字符*'的说法,但参数2的类型'字符(*)[10] - [-Wformat]

下面是我的程序:

#include<stdio.h>
int main()
{
  char array[10];
  scanf("%10c", &array);
  printf("%.10s", array);
  return 0;
}

当我删除,警告消失'和;'从scanf函数。
我知道,这是一个数组,并且不需要与功放;.但是,不要他们有同样的效果?
我的意思是这两个'和;阵'和'阵'给它的第一个元素的地址,对不对?
如果是这样,这里有什么区别呢?

The warning disappears when I remove '&' from scanf. I know, it's an array and doesn't require &. But don't they have same effect? I mean both '&array' and 'array' give address of its first element, right? If so, what's the difference here?

我在这里阅读一些相关的问题,并用Google搜索了很多。
有人说,'和;阵'是一个指向字符数组,如果数组是一个数组,而数组本身就是一个字符指针。
据它说什么,因为我使用的是C,一个指向的字符数组应传递,我想。
IDK,我会很感激,如果有人介绍了如何%[宽度] c ++工程。

I read some related questions here, and googled a lot. It has been said that '&array' is a pointer to an array of characters if 'array' is an array while 'array' itself is a pointer to char. According to what it says, since I'm using %c, a pointer to an array of characters should be passed, I think. Idk, I would very greatful if someone explains how %[width]c works.

我也验证了所有'阵','和;阵'和'和;数组[0]'给它的第一个元素的地址。
下面是我所做的:

I also verified that all 'array', '&array' and '&array[0]' give address of its first element. Here's what I did:

int main()
{
  char array[10];
  puts("ADDRESS:");
  printf(" %p \n %p \n %p", array, &array, &array[0]);
  return 0;
}

如果他们都给予相同的地址,它为什么给人这样的警告?
这也适用于%S。
他们都在大多数窗口编译器做工精细,没有任何警告。

If they all give same address, why is it giving such warnings? It also works for %s. They all work fine in most of windows compiler, without any warnings.

由于我是Windows用户,我从来没有使用过的gcc编译器。而我在想什么是它只是不强制写&安培;与函数指针。
你不一定必须写&安培;与函数指针,我读

Since I'm a windows user, I never used gcc compiler before. And what I was thinking was it's just not mandatory to write & as with function pointers. You don't necessarily have to write & with function pointers, I read.

我越来越糊涂了,请帮我把它。

I'm getting more and more confused, please help me get it.

感谢您。

推荐答案

阵列&放大器;阵列这两个收益率一个指向同一个地址,但具有不同的类型。前者是在大多数情况下,以&放等价;数组[0] ,一个的char * 你的情况。 &放大器;阵列,然而,这是数组本身,其类型的地址字符(*)[10] 在你的例子。

array and &array both yield a pointer to the same address, but with different types. The former is equivalent in most situations to &array[0], a char * in your case. &array, however, is the address of the array itself, which has type char (*)[10] in your example.

这篇关于C编程:'和;'在scanf函数阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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