gcc:指向数组的指针 [英] gcc: pointer to array
问题描述
大家好,
我有关于gcc行为的问题(gcc版本3.3.4)。
在以下测试中程序发出警告:
#include< stdio.h>
int aInt2 [6] = {0,1,2,4,9 ,16};
int aInt3 [5] = {0,1,2,4,9};
void print1(const int * p, size_t cnt)
{
while(cnt--)
printf("%d \ n",* p ++); < br $>
}
void print2(const int(* p)[5])
{
size_t cnt;
#if 0
//禁止:
(* p)[0] = 0;
#endif
for(cnt = 0; cnt< 5; cnt ++)
printf("%d \ n",(* p)[cnt] );
}
int main()
{
printf(" test begin \ n");
print1(aInt2,sizeof(aInt2)/ sizeof(aInt2 [0]));
print2(& aInt3) ; //< - 在这里警告
printf(" test end\\\
);
返回0;
}
警告是:
MOD.c:函数`main'':
MOD.c ::警告:从不兼容的指针类型传递'print2'的arg 1
为什么会这样?这与使用print1(aInt2,...)有什么不同?我想要做的就是明确地显示指针是一个5
条目的数组,并确保它在print2()内部没有被修改。然而
print2()内的#if 0'的赋值运算符由
gcc正确处理 - 生成错误:
MOD.c:在函数`print2'':
MOD.c ::错误:只读位置的分配
gcc有什么问题?
指向数组的另一个问题是...编译器
如果我改变print2(),
不会产生警告
for(cnt = 0; cnt< 5; cnt ++)
to
for(cnt = 0; cnt< 6; cnt ++)
并让我访问(* p)[5],虽然我认为至少应该在这里生成一个警告
。
它没有'如果我把
aInt3 [6] = 0;
放到main()中,请警告我。但在这两种情况下,编译器都知道。
数组的实际大小,仍然没有警告...
这样可以吗???
谢谢,
Alex
PS我将其编译为gcc MOD.c -Wall -oMOD.exe
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2[6] = {0,1,2,4,9,16};
int aInt3[5] = {0,1,2,4,9};
void print1 (const int* p, size_t cnt)
{
while (cnt--)
printf ("%d\n", *p++);
}
void print2 (const int (*p)[5])
{
size_t cnt;
#if 0
// prohibited:
(*p)[0]=0;
#endif
for (cnt=0; cnt<5; cnt++)
printf ("%d\n", (*p)[cnt]);
}
int main()
{
printf ("test begin\n");
print1 (aInt2, sizeof(aInt2)/sizeof(aInt2[0]));
print2 (&aInt3); // <-- warns here
printf ("test end\n");
return 0;
}
The warning is:
MOD.c: In function `main'':
MOD.c:: warning: passing arg 1 of `print2'' from incompatible pointer type
Why is that? How is this different from using print1 (aInt2, ...)? All I
want to do is to explicitly show that the pointer is to an array of 5
entries and ensure that it''s not modified in anyway inside print2(). However
the #if 0''d assignment operator inside print2() is correctly handled by
gcc -- an error is generated:
MOD.c: In function `print2'':
MOD.c:: error: assignment of read-only location
What''s wrong with gcc?
There''s another thing about the pointer to the array is... The compiler
doesn''t generate a warning if I change in print2()
for (cnt=0; cnt<5; cnt++)
to
for (cnt=0; cnt<6; cnt++)
and lets me access (*p)[5], though I think at least a warning should be
generated here.
It doesn''t warn me if I put
aInt3[6] = 0;
into main(). But in both cases the compiler "knows" the real size of the
array, still no warning...
Is this OK???
Thanks,
Alex
P.S. I compile it as gcc MOD.c -Wall -oMOD.exe
推荐答案
当然。
" ;&安培; alnt3"与alnt3相同,因为alnt3是一个数组。
因此,你将int *传递给print2,它需要一个int *数组 - 即int **。
-
Maxim Shatskih,Windows DDK MVP
StorageCraft Corporation
ma *** @ storagecraft.com
http://www.storagecraft.com
" Alexei A. Frounze" <人***** @ chat.ru>在消息中写道
新闻:3j ************ @ individual.net ...
Surely.
"&alnt3" is the same as "alnt3", since alnt3 is an array.
So, you pass int* to print2, and it expects an array of int* - i.e. int**.
--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
ma***@storagecraft.com
http://www.storagecraft.com
"Alexei A. Frounze" <al*****@chat.ru> wrote in message
news:3j************@individual.net...
大家好,
在下面的测试程序中它会发出警告:
#include< stdio.h>
int aInt2 [6] = {0,1,2,4,9,16};
int aInt3 [5] = {0,1,2,4,9};
void print1(const int * p,size_t cnt)
{
while(cnt--)
printf("%d \ n",* p ++) ;
void print2(const int(* p)[5])
{
size_t cnt;
#if 0
//禁止:
(* p)[0] = 0;
#endif
for(cnt = 0; cnt< 5; cnt ++)
printf("%d \ n",(* p)[cnt]);
}
int main()
{/> printf(" test begin\\\
) );
print1(aInt2,sizeof(aInt2)/ sizeof(aInt2 [0]));
print2(& aInt3); //< - 在此警告
printf(" test end\\\
);
返回0;
}
警告是:
MOD.c:在函数`main'':MOD.c :: warning:从不兼容的指针类型传递'print2'的arg 1
为什么是那?这与使用print1(aInt2,...)有什么不同?我想要做的就是明确地显示指针是一个包含5个条目的数组,并确保它在print2()内部没有被修改。然而,
gcc正确处理了print2()中#if 0'的赋值运算符 - 生成错误:
MOD.c:函数`print2'':
MOD.c ::错误:只读位置的分配
gcc有什么问题?
指向数组的另一个问题是。 ..编译器
如果我将(cnt = 0; cnt< 5; cnt ++)
更改为(cnt),则不会生成警告= 0; cnt< 6; cnt ++)
让我访问(* p)[5],虽然我认为至少应该在这里生成一个警告。
它不会警告我,如果我把
aInt3 [6] = 0;
放到main()中。但在这两种情况下,编译器都知道。
阵列的实际大小,仍然没有警告......
这样可以吗?
谢谢,
Alex
P.S。我将其编译为gcc MOD.c -Wall -oMOD.exe
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2[6] = {0,1,2,4,9,16};
int aInt3[5] = {0,1,2,4,9};
void print1 (const int* p, size_t cnt)
{
while (cnt--)
printf ("%d\n", *p++);
}
void print2 (const int (*p)[5])
{
size_t cnt;
#if 0
// prohibited:
(*p)[0]=0;
#endif
for (cnt=0; cnt<5; cnt++)
printf ("%d\n", (*p)[cnt]);
}
int main()
{
printf ("test begin\n");
print1 (aInt2, sizeof(aInt2)/sizeof(aInt2[0]));
print2 (&aInt3); // <-- warns here
printf ("test end\n");
return 0;
}
The warning is:
MOD.c: In function `main'':
MOD.c:: warning: passing arg 1 of `print2'' from incompatible pointer type
Why is that? How is this different from using print1 (aInt2, ...)? All I
want to do is to explicitly show that the pointer is to an array of 5
entries and ensure that it''s not modified in anyway inside print2(). However
the #if 0''d assignment operator inside print2() is correctly handled by
gcc -- an error is generated:
MOD.c: In function `print2'':
MOD.c:: error: assignment of read-only location
What''s wrong with gcc?
There''s another thing about the pointer to the array is... The compiler
doesn''t generate a warning if I change in print2()
for (cnt=0; cnt<5; cnt++)
to
for (cnt=0; cnt<6; cnt++)
and lets me access (*p)[5], though I think at least a warning should be
generated here.
It doesn''t warn me if I put
aInt3[6] = 0;
into main(). But in both cases the compiler "knows" the real size of the
array, still no warning...
Is this OK???
Thanks,
Alex
P.S. I compile it as gcc MOD.c -Wall -oMOD.exe
" Maxim S. Shatskih" <毫安*** @ storagecraft.com>在留言中写道
news:da *********** @ gavrilo.mtu.ru ...
"Maxim S. Shatskih" <ma***@storagecraft.com> wrote in message
news:da***********@gavrilo.mtu.ru...
当然。
"& alnt3"因为alnt3是一个数组,所以与alnt3相同。
不是。 :)值是相同的,但类型不是,严格说来话。如果你将& aInt2传递给print1(),你就会遇到问题。你不会,如果你通过aInt2或& aInt2 [0],你将获得
。这很清楚。并且它不是问题。
所以,你将int *传递给print2,并且它需要一个int *数组 - 即
int **。
int(* p)[5]将p声明为指向int类型的5个元素的数组的指针。它是
不是一个5指针的数组,也不是指向这样的数组的指针!
Alex
- < Windows XPK MVP Maxim Shatskih
StorageCraft公司
ma***@storagecraft.com
http://www.storagecraft.com
; Alexei A. Frounze <人***** @ chat.ru>在消息中写道
新闻:3j ************ @ individual.net ...
Surely.
"&alnt3" is the same as "alnt3", since alnt3 is an array.
It''s not. :) The values are identical, but the types aren''t, strictly
speaking. If you pass &aInt2 to print1(), you''ll have a problem. You won''t,
if you pass aInt2 or &aInt2[0]. This is clear. And it''s not the problem.
So, you pass int* to print2, and it expects an array of int* - i.e. int**.
int (*p)[5] declares p as a pointer to array of 5 elements of type int. It''s
not an array of 5 pointers, nor a pointer to such array!
Alex
--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
ma***@storagecraft.com
http://www.storagecraft.com
"Alexei A. Frounze" <al*****@chat.ru> wrote in message
news:3j************@individual.net...
大家好,
我有关于gcc行为的问题(gcc版本3.3.4)。
在下面的测试程序中,它会发出警告:
#include< stdio.h>
int aInt2 [6] = {0,1,2,4,9,16};
int aInt3 [5] = {0,1,2,4,9};
void print1(const int * p,size_t cnt)
{
while(cnt--)
printf("%d \ n",* p ++);
}
void print2(const int(* p)[5])
{
size_t cnt;
#if 0
//禁止:
(* p)[0] = 0;
#endif
for(cnt = 0; cnt< 5; cnt ++)
printf("%d \ n" ,(* p)[cnt]);
}
int main()
{
printf(" test begin\\\
);
print1(aInt2,sizeof(aInt2)/ sizeof(aInt2 [0]));
print2(& aInt3); //< - 在此警告
printf(" test end\\\
);
返回0;
}
警告是:
MOD.c:在函数`main'':MOD.c :: warning:从不兼容的指针传递'print2'的arg 1
类型
为什么是那?这与使用print1(aInt2,...)有什么不同?我想要做的就是明确地显示指针是一个包含5个条目的数组,并确保它在print2()内部没有被修改。
然而,在print2()中#if 0'的赋值运算符由
gcc正确处理 - 生成错误:
MOD.c:在函数`print2'':
MOD.c ::错误:只读位置的分配
gcc有什么问题?
指向数组的另一个问题是。 ..编译器
如果我将(cnt = 0; cnt< 5; cnt ++)
更改为(cnt),则不会生成警告= 0; cnt< 6; cnt ++)
让我访问(* p)[5],虽然我认为至少应该在这里生成一个警告。
它不会警告我,如果我把
aInt3 [6] = 0;
放到main()中。但在这两种情况下,编译器都知道。
阵列的实际大小,仍然没有警告......
这样可以吗?
谢谢,
Alex
P.S。我将其编译为gcc MOD.c -Wall -oMOD.exe
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2[6] = {0,1,2,4,9,16};
int aInt3[5] = {0,1,2,4,9};
void print1 (const int* p, size_t cnt)
{
while (cnt--)
printf ("%d\n", *p++);
}
void print2 (const int (*p)[5])
{
size_t cnt;
#if 0
// prohibited:
(*p)[0]=0;
#endif
for (cnt=0; cnt<5; cnt++)
printf ("%d\n", (*p)[cnt]);
}
int main()
{
printf ("test begin\n");
print1 (aInt2, sizeof(aInt2)/sizeof(aInt2[0]));
print2 (&aInt3); // <-- warns here
printf ("test end\n");
return 0;
}
The warning is:
MOD.c: In function `main'':
MOD.c:: warning: passing arg 1 of `print2'' from incompatible pointer type
Why is that? How is this different from using print1 (aInt2, ...)? All I
want to do is to explicitly show that the pointer is to an array of 5
entries and ensure that it''s not modified in anyway inside print2(). However the #if 0''d assignment operator inside print2() is correctly handled by
gcc -- an error is generated:
MOD.c: In function `print2'':
MOD.c:: error: assignment of read-only location
What''s wrong with gcc?
There''s another thing about the pointer to the array is... The compiler
doesn''t generate a warning if I change in print2()
for (cnt=0; cnt<5; cnt++)
to
for (cnt=0; cnt<6; cnt++)
and lets me access (*p)[5], though I think at least a warning should be
generated here.
It doesn''t warn me if I put
aInt3[6] = 0;
into main(). But in both cases the compiler "knows" the real size of the
array, still no warning...
Is this OK???
Thanks,
Alex
P.S. I compile it as gcc MOD.c -Wall -oMOD.exe
Alexei A. Frounze写道:
Alexei A. Frounze wrote:
void print2(const int(* p)[5])
void print2 (const int (*p)[5])
arg1 of print2似乎是指向a的指针功能......不是吗?
-
daniele_athome
arg1 of print2 seems a pointer to a function... isn''t it?
--
daniele_athome
这篇关于gcc:指向数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!