我是C的新手.
我知道这有多种形式的问题,但我的有点独特......我想.我有一个无符号的短指针.
6 unsigned short *pt;
7 pt = myArray[0];
Run Code Online (Sandbox Code Playgroud)
该数组声明为:const unsigned short myArray[1024]并且是一个十六进制数的数组,形式为0x0000,依此类推.
我尝试编译,它抛出这些错误:
myLib.c:7: error: data definition has no type or storage class
myLib.c:7: error: type defaults to 'int' in declaration of 'pt'
myLib.c:7: error: conflicting types for 'pt'
myLib.c:6: note: previous declaration of 'pt' was here
myLib.c:7: error: initialization makes integer from pointer without a cast
Run Code Online (Sandbox Code Playgroud)
什么出错的想法?
谢谢,菲尔
Die*_*Epp 10
我的猜测(你只显示两行)是这个代码出现在一个函数之外.这是一个声明:
pt = myArray[0];
Run Code Online (Sandbox Code Playgroud)
声明必须有功能.此外,如果myArray有类型unsigned short[],那么你想要做其中一个:
pt = myArray;
pt = &myArray[0]; // same thing
Run Code Online (Sandbox Code Playgroud)
&是参考运算符.它返回它前面的变量的内存地址.指针存储内存地址.如果要"将某些内容存储在指针中",请将其与*操作员取消引用.当您这样做时,计算机将查看指针包含的内存地址,这适用于存储您的值.
char *pc; // pointer to a type char, in this context * means pointer declaration
char letter = 'a'; // a variable and its value
pc = &letter; // get address of letter
// you MUST be sure your pointer "pc" is valid
*pc = 'B'; // change the value at address contained in "pc"
printf("%c\n", letter); // surprise, "letter" is no longer 'a' but 'B'
Run Code Online (Sandbox Code Playgroud)
当你使用时,myArray[0]你不会得到一个地址而是一个价值,这就是人们使用的原因&myArray[0].