473,490 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointer to array and gcc warnings

Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

Can anyone explain (don't answer with a yes; actually do so :-) gcc's
behavior here?
Sep 23 '08 #1
7 2130
vi******@gmail.com writes:
Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

Can anyone explain (don't answer with a yes; actually do so :-) gcc's
behavior here?
"This is off topic in c.l.c. We do not know anything about gcc
here. please ask in a newsgroup more appropriate to your question. The
Gnu C newsgroup is down the corridor on the left."

Ah. That felt good.
Sep 23 '08 #2
In article <gb**********@registered.motzarella.org>,
Richard <rg****@gmail.comwrote:
>vi******@gmail.com writes:
>Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

Can anyone explain (don't answer with a yes; actually do so :-) gcc's
behavior here?

"This is off topic in c.l.c. We do not know anything about gcc
here. please ask in a newsgroup more appropriate to your question. The
Gnu C newsgroup is down the corridor on the left."

Ah. That felt good.
Bathe in the glory. There's hope for you (*) yet.

(*) To get accepted into the he-man CLC regs society.

Sep 23 '08 #3
vipps...@gmail.com wrote:
Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */

return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?

My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)
VLA normally refers to variable length arrays. You don't have any of
those in this program. I suspect you mean variable arguments. No such
promotion should occur.

As far as the C standard is concerned, the type of &p[0] is char(*)
[2]. Why gcc described it as char(*)[1u] is something you'll have to
take up with gcc. It's definitely version dependent. I have access to
gcc versions 3.2.3 and 3.3, both of which simply say:

warning: int format, pointer arg (arg 2)
Sep 23 '08 #4
On Sep 23, 7:13 pm, jameskuy...@verizon.net wrote:
vipps...@gmail.com wrote:
Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)
In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'
The code is
#include <stdio.h>
int main(void) {
char p[3][2];
printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */
return 0;
}
I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?
My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

VLA normally refers to variable length arrays. You don't have any of
those in this program. I suspect you mean variable arguments. No such
promotion should occur.
Whoops, I meant "variadic arguments".
As far as the C standard is concerned, the type of &p[0] is char(*)
[2]. Why gcc described it as char(*)[1u] is something you'll have to
take up with gcc. It's definitely version dependent. I have access to
gcc versions 3.2.3 and 3.3, both of which simply say:

warning: int format, pointer arg (arg 2)
Thanks. :-)
Sep 23 '08 #5
vi******@gmail.com wrote, On 23/09/08 17:16:
On Sep 23, 7:13 pm, jameskuy...@verizon.net wrote:
>vipps...@gmail.com wrote:
>>Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)
In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'
The code is
#include <stdio.h>
int main(void) {
char p[3][2];
printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */
return 0;
}
I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?
<snip>
>As far as the C standard is concerned, the type of &p[0] is char(*)
[2]. Why gcc described it as char(*)[1u] is something you'll have to
take up with gcc. It's definitely version dependent. I have access to
gcc versions 3.2.3 and 3.3, both of which simply say:

warning: int format, pointer arg (arg 2)

Thanks. :-)
I suspect it was a bug that has been fixed. I get a far more useful warning:
markg@brenda:~$ gcc -ansi -pedantic -Wall -Wextra -g3 t.c
t.c: In function ‘main’:
t.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type
‘char (*)[2]’
markg@brenda:~$
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Sep 23 '08 #6
On Sep 23, 10:06 pm, Flash Gordon <s...@spam.causeway.comwrote:
vipps...@gmail.com wrote:
Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)
<snip>
I suspect it was a bug that has been fixed. I get a far more useful warning:
markg@brenda:~$ gcc -ansi -pedantic -Wall -Wextra -g3 t.c
t.c: In function ‘main’:
t.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type
‘char (*)[2]’
markg@brenda:~$
Ah, thank you too.
For anyone interested, I'm using gcc 4.0.3
Sep 23 '08 #7
On Tue, 23 Sep 2008 08:40:58 -0700 (PDT), vi******@gmail.com wrote:
>Today I got a confusing message from gcc (I'm aware, those don't break
conformance ;-)

In function 'main':
7: warning: format '%d' expects type 'int', but argument 2 has type
'char (*)[1u]'

The code is

#include <stdio.h>

int main(void) {

char p[3][2];

printf("%d\n", &p[0]); /* this is errornous, but it's on purpose
there to generate the warning */
The & operator always results in a value of type pointer to something.
A pointer value is never the appropriate type of argument for %d. And
undefined behavior does break conformance.
>
return 0;
}

I'm curious, what is gcc talking about?
Why is &p[0] a char (*)[1u]?
p is an array of 3 array of 2 char.

p[0] is the first array of 2 char in the above object.

&p[0] is the address of p[0] with type pointer to array of 2 char. The
syntax for this type is char (*)[2].

The standard does not impose any quality requirements on the text of
diagnostics and this diagnostic is not even a required one. So what
you have is a useful message that happens to suffer from a slight
inaccuracy in terminology.
>
My first guess was that char (*)[N] pointers get promoted to char (*)
[1] pointers in VLA arguments. (it doesn't make much sense to me
though)

Can anyone explain (don't answer with a yes; actually do so :-) gcc's
behavior here?
--
Remove del for email
Sep 25 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7629
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
3
5050
by: Timo | last post by:
I am trying to pass a pointer to an array of structures to function, but I have some problems. I am using MS Visual C++ 6.0, but I think this is more of a C-problem than Windows programming...
11
2062
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
5
23087
by: ur8x | last post by:
I have a double pointer and a 2D array: int mat, **ptr; ptr = mat; mat = 3; Although mat and ptr are pointing at the same address, &ptr and &mat are not, why?
204
12882
by: Alexei A. Frounze | last post by:
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 = {0,1,2,4,9,16}; int aInt3 =...
10
2191
by: vb | last post by:
Hi all, I am a newbie in C and i want to know what all pointer conversions are "legal" according to ANSI C standard. For Example, int* to char*, some_struct* to char* and so on .. According to...
73
3750
by: Markus | last post by:
Hi, I can't understand why this code causes a "memory read exception" at int x=**a; void pass(int** a) { int x=**a; } void main()
7
528
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
17
3215
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
11
1711
by: Christopher Key | last post by:
Hello, Can anyone suggest why gcc (-W -Wall) complains, test.c:22: warning: passing arg 1 of `f' from incompatible pointer type when compiling the following code? Change the declation of f...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.