C语言 标量对象需要初始化器中的一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24905270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
scalar object requires one element in initializer
提问by Roxana Istrate
Why when I want to initialize the following vector of uint8_t
为什么当我想初始化以下向量时 uint8_t
uint8_t *mmac_source1 = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x01 };
I get this error
我收到这个错误
Error: scalar object 'mmac_source1' requires one element in initializer
But when I am using this :
但是当我使用这个时:
uint8_t mmac_source1[6] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x01 };
it's working fine.
它工作正常。
采纳答案by Ginu Jacob
uint8_t *mmac_source1 = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x01 };
Here you don't memory allocated to the pointer.
mmac_source1just acts as a place holder wherein you can store an address.
在这里,您没有分配给指针的内存。
mmac_source1只是作为一个占位符,您可以在其中存储地址。
uint8_t mmac_source1[6] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x01 };
Here you have an array where in your compiler allocates sizof(uint8_t)*6bytes.
在这里你有一个数组,在你的编译器中分配sizof(uint8_t)*6字节。
回答by Bathsheba
In many instances a pointer is interchangable with an array. More formally it can be said that the first element of an array decaysto a pointer.
在许多情况下,指针可以与数组互换。更正式地说,可以说数组的第一个元素衰减为指针。
But there are exceptions and the one you cite in your question is one of them: uint8_t *mmac_source1 = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x01 };is not syntatically valid. Conceptually you are allocating, in your second case, the array on the stack. Using a pointer in that instance doesn't make sense.
但是也有例外,您在问题中引用的就是其中之一:uint8_t *mmac_source1 = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x01 };在语法上无效。从概念上讲,在第二种情况下,您正在分配堆栈上的数组。在该实例中使用指针没有意义。
回答by sjsam
The rvalue expected for a pointer initialization is a memory address.
指针初始化所需的右值是一个内存地址。
As in the case of arrays, the compiler allows the special case that we want to initialize the content at which the pointer points with constants at the same moment the pointer is declared:
与数组的情况一样,编译器允许在声明指针的同时用常量初始化指针指向的内容的特殊情况:
char * str = "hello"; // what succeeds the equal to is the rvalue.
The above statement is similar to
上面的语句类似于
char str[] = "hello";
The next question is if we are allowed to do the following :
下一个问题是是否允许我们执行以下操作:
int* v = 2;
Yes we are but at our own risk. The compiler will initially come with an error:
是的,但我们要自担风险。编译器最初会出现错误:
error: invalid conversion from ‘int' to ‘int*' [-fpermissive]
Here the compiler considered the rvalue as merely an integer where what it expected was a memory location say for example &aor so. At the same time it provided an option fpermissiveto overrrideits assumption whereby we can force the rvalue to look like a memory address.fpermissiveflag downgrades some diagnostics about nonconformant code from errors to warnings. So when compiling, I did
在这里,编译器认为右值仅仅是一个整数,它期望的是一个内存位置,例如&a左右。同时,它提供了一个选项fpermissive到overrride其假设由此,我们可以强制右值看起来像一个内存地址。fpermissive标志将一些关于不符合代码的诊断从错误降级为警告。所以在编译时,我做了
g++ -fpermissive intpointerexample.cpp -o intpointerexample
g++ -fpermissive intpointerexample.cpp -o intpointerexample
Eventually on executing intpointerexample, I encountered a segmentation fault because I might have attempted to access memory the program does not have rights to.
最终在执行 intpointerexample 时,我遇到了分段错误,因为我可能试图访问程序无权访问的内存。

