C语言 指向数组中第一个元素的指针!(C)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6639256/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:06:02  来源:igfitidea点击:

Pointer to first element in array! (C)

carrayspointers

提问by Phil

I'm new to 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];

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.

该数组声明如下:const unsigned short myArray[1024]并且是 0x0000 等形式的十六进制数数组。

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?

关于出了什么问题的任何想法?

Thanks, Phil

谢谢,菲尔

回答by Dietrich Epp

My guess (you only show two lines) is that this code appears outside a function. This is a statement:

我的猜测(你只显示了两行)是这段代码出现在函数之外。这是一个声明:

pt = myArray[0];

Statements must go in functions. Also, if myArrayhas type unsigned short[], then you want to do one of these instead:

语句必须在函数中。此外,如果myArray有 type unsigned short[],那么您想要执行以下操作之一:

pt = myArray;
pt = &myArray[0]; // same thing

回答by catfish_deluxe_call_me_cd

&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'

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

当您使用时,myArray[0]您得到的不是地址而是值,这就是人们使用&myArray[0].

回答by Jonathan Wood

Yeah, you really should include a bit more code so we can see the context.

是的,您确实应该包含更多代码,以便我们可以查看上下文。

I don't quite get the error messages, but your code is not correct.

我不太明白错误消息,但您的代码不正确。

Try:

尝试:

pt = &myArray[0];

Or:

或者:

pt = myArray + 0;

Or just:

要不就:

pt = myArray;

Instead.

反而。

回答by David Grayson

You should not throw away the const qualifier; so the pointer should have a const modifier.

你不应该扔掉 const 限定符;所以指针应该有一个 const 修饰符。

const unsigned short myArray[1024];
const unsigned short * pointer = myArray;  // set pointer to first element