指针指向第一个元素的数组! (C) [英] Pointer to first element in array! (C)

查看:236
本文介绍了指针指向第一个元素的数组! (C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C

我知道这已被要求在许多形式,但我的是一点点独特的...我猜。我有一个无符号短指针。

I know this has been asked in many forms but mine is a little unique...I guess. I have an unsigned short pointer.

6 unsigned short *pt;  
7 pt = myArray[0];

该数组被声明为这样的: const的无符号短myArray的[1024] ,其格式为0x0000等十六进制数字数组

The array is declared as such: const unsigned short myArray[1024] and is an array of hex numbers of the form 0x0000 and so on.

我尝试编译,它抛出这些错误:

I try to compile, it throws these errors:

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

什么错的任何想法?

any ideas of what's going wrong?

谢谢,
菲尔

推荐答案

&安培; 是引用操作符。它返回它的变量precedes的内存地址。指针店的内存地址即可。如果你想囤东西的指针取消引用它与 * 运营商。当你做计算机将考虑内存地址的指针包含,这是适合存放你的价值。

& is the reference operator. It returns the memory address of the variable it precedes. Pointers store memory addresses. If you want to "store something in a pointer" you dereference it with the * operator. When you do that the computer will look into the memory address your pointer contains, which is suitable for storing your value.

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]

When you use myArray[0] you don't get an address but a value, that's why people used &myArray[0].

这篇关于指针指向第一个元素的数组! (C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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