我是C的新手.
我知道这有多种形式的问题,但我的有点独特……我想.我有一个无符号的短指针.
6 unsigned short *pt; 7 pt = myArray[0];
数组声明如下: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
什么出错的想法?
谢谢,
菲尔
解决方法
&安培;是参考运算符.它返回它前面的变量的内存地址.指针存储内存地址.如果要“将某些内容存储在指针中”,请使用*运算符取消引用它.当您这样做时,计算机将查看指针包含的内存地址,这适用于存储您的值.
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'
当你使用myArray [0]时,你没有得到一个地址而是一个值,这就是人们使用& myArray [0]的原因.