数组名称作为指针 [英] Array name as pointers

查看:78
本文介绍了数组名称作为指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在int arr [10]中,整数数组的名称

" arr"

相当于指向数组第一个元素的指针。但是

" arr"

实际上是一个指针,与int * arr;相同?


我试图修改这个arr以下代码中的东西。海湾合作委员会警告我不相容的指针,我不知道。


---------------- -----------------------------------------------

#include< stdio.h>


void incrementPtr(int **);


int main(int argc, char * argv []){

int i;

int a [3] = {10,20,30};


incrementPtr(& a);


for(i = 0; i< 3; i ++){

printf(" a [% d] =%d \ n",i,*(a + i));

}


返回0;

}


void incrementPtr(int ** a){

* a ++;

}

------------------------------------------------ -


任何人都有任何意见我为什么收到警告?并且

如果a在这段代码中可以修改吗?


谢谢。

解决方案

newgoat写道:< blockquote class =post_quotes>据我所知,在int arr [10]中,整数数组的名称
arr
等同于指向第一个的指针数组的元素。但是
arr
确实是一个指针,与int * arr;相同。 ?


没有。这个问题太死了,让人觉得鹦鹉看起来很活泼。你是否认为你是第一个有这种想法的人?这是为了避免多次回答我们有这样的问题,我们有一个常见问题解答

< http://c-faq.com/>。请在发布之前检查它。

其中,您可能会觉得有趣:


< http://c-faq.com/aryptr/aryptr2。 HTML>但我听说char a []是与char * a相同的



< http://c-faq.com/aryptr/aryptrequiv html的>那么什么是指C中指针和数组的等价?$ b / b

< http:// c-faq。 COM / aryptr / aryptrparam.html>如果它们如此不同,那么

为什么数组和指针声明可以互换为函数

正式参数?


[ ...]任何人都有任何意见我为什么得到警告?并且
如果a在这段代码中可以修改吗?




一旦你检查了常见问题解答,就像你应该已经完成​​的那样,你就会知道答案是否为





" Martin Ambuhl" <毫安***** @ earthlink.net>在留言中写道

新闻:Nf ****************** @ newsread2.news.atl.earth link.net ...

newgoat写道:

据我所知,在int arr [10]中,整数数组的名称
arr
等效于指向数组第一个元素的指针。但是
arr
确实是一个指针,与int * arr;相同。 ?
没有。




公平声明。

这个问题已经死了,很无聊让鹦鹉看起来很好看
活泼。


?我承认我现在睡着了,但WTF你忘记了吗?

你认为你是第一个想到这个吗?


敌意。

这是为了避免反复回答我们有这样的事情我们有一个常见问题
< http:// c -faq.com/> ;.请在发布之前检查。
其中,您可能会觉得有趣:

< http://c-faq.com/aryptr/aryptr2.html>但我听说char a []与char * a相同。

< http://c-faq.com/aryptr/aryptrequiv.html>那么C中的指针和数组的等价是什么意思?

< http://c-faq.com/aryptr/aryptrparam.html>如果它们如此不同,那么为什么数组和指针声明可以作为函数
形式参数互换?


公平。公平。公平公正。

[...]

任何人都有任何意见我为什么收到警告?并且
如果a在这段代码中可以修改吗?
一旦你查看常见问题解答......你就会知道答案。




公平。

检查常见问题后,您应该已经完成​​了,




在您告诉他之前,他怎么能知道常见问题?他有先见之明吗?我认为这只是为了上帝。并且,我认为它违反了许多关于

通信理论的法律。

Rod Pemberton


Rod Pemberton写道:

" Martin Ambuhl" <毫安***** @ earthlink.net>在消息中写道
新闻:Nf ****************** @ newsread2.news.atl.earth link.net ...

newgoat写道:

据我所知,在int arr [10]中,整数数组的名称
arr
等同于指向数组第一个元素的指针。但是
arr
确实是一个指针,与int * arr;相同。 ?


没有。



公平的陈述。

这个问题太死了,让人看起来很无聊
热闹。



?我承认我现在睡着了,但WTF你忘记了吗?



他假设一个共同的文化遗产极客,每个人都知道

Monty Python的死鹦鹉素描。毫无疑问,

维基百科上有一篇完整的文章: http://en.wikipedia.org/wiki/Dead_Parrot


我必须说我看过阅读常见问题解答的更友好的用语。 。


S.


As far as I understand, in "int arr[10]", the name of the integer array
"arr"
is equivalent to the pointer to the first element of the array. But is
"arr"
really a pointer, identical as "int *arr;" ?

I tried to modify this "arr" thing in the following code. GCC warns
me of incompatible pointers, which I have no idea of.

---------------------------------------------------------------
#include<stdio.h>

void incrementPtr(int **);

int main(int argc, char *argv[]){
int i;
int a[3] = {10, 20, 30};

incrementPtr(&a);

for(i = 0; i < 3; i++){
printf("a[%d] = %d\n", i, *(a + i));
}

return 0;
}

void incrementPtr(int **a){
*a++;
}
-------------------------------------------------

Anyone has any opinion why I got the warning? And
if "a" in this code can be modified?

Thanks.

解决方案

newgoat wrote:

As far as I understand, in "int arr[10]", the name of the integer array
"arr"
is equivalent to the pointer to the first element of the array. But is
"arr"
really a pointer, identical as "int *arr;" ?
No. This question is so dead and boring it makes the parrot look
lively. Do you imagine you are the first with this thought? It is to
avoid repeatedly answering such things that we have a FAQ
<http://c-faq.com/>. Please check it before posting.
Among others, you might find interesting:

<http://c-faq.com/aryptr/aryptr2.html> But I heard that char a[] was
identical to char *a.

<http://c-faq.com/aryptr/aryptrequiv.html> So what is meant by the
``equivalence of pointers and arrays'''' in C?

<http://c-faq.com/aryptr/aryptrparam.html> If they''re so different, then
why are array and pointer declarations interchangeable as function
formal parameters?

[...] Anyone has any opinion why I got the warning? And
if "a" in this code can be modified?



Once you check the FAQ, as you should have already done, you will know
the answer.



"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:Nf******************@newsread2.news.atl.earth link.net...

newgoat wrote:

As far as I understand, in "int arr[10]", the name of the integer array
"arr"
is equivalent to the pointer to the first element of the array. But is
"arr"
really a pointer, identical as "int *arr;" ?
No.



Fair statement.
This question is so dead and boring it makes the parrot look
lively.
? I admit I''m asleep right now, but WTF did you forget?
Do you imagine you are the first with this thought?
Hostility.
It is to
avoid repeatedly answering such things that we have a FAQ
<http://c-faq.com/>. Please check it before posting.
Among others, you might find interesting:

<http://c-faq.com/aryptr/aryptr2.html> But I heard that char a[] was
identical to char *a.

<http://c-faq.com/aryptr/aryptrequiv.html> So what is meant by the
``equivalence of pointers and arrays'''' in C?

<http://c-faq.com/aryptr/aryptrparam.html> If they''re so different, then
why are array and pointer declarations interchangeable as function
formal parameters?

Fair. Fair. Fair and Fair.
[...]

Anyone has any opinion why I got the warning? And
if "a" in this code can be modified?
Once you check the FAQ ... you will know
the answer.



Fair.
Once you check the FAQ, as you should have already done,



How could he know about the FAQ before you told him? Is he prescient? I
thought that was only for God. And, I think it violates a number of laws of
communication theory too.
Rod Pemberton


Rod Pemberton wrote:

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:Nf******************@newsread2.news.atl.earth link.net...

newgoat wrote:

As far as I understand, in "int arr[10]", the name of the integer array
"arr"
is equivalent to the pointer to the first element of the array. But is
"arr"
really a pointer, identical as "int *arr;" ?


No.



Fair statement.

This question is so dead and boring it makes the parrot look
lively.



? I admit I''m asleep right now, but WTF did you forget?


He is assuming a shared cultural heritage of geek, where everybody knows
Monty Python''s dead parrot sketch. It should come as no surprise that
Wikipedia has a full article on it: http://en.wikipedia.org/wiki/Dead_Parrot.

I must say I''ve seen kinder wordings of "read the FAQ".

S.


这篇关于数组名称作为指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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