473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

pointer to array of const objects

given the code:

<file: c.c>
typedef int quad_t[4];

void w0(int *r, const quad_t *p)
{
*r = (*p)[0];
}

int main()
{
quad_t m = {0};
int r;
w0(&r, &m);
return 0;
}
</file>

why does the cc produce:

<quoting respectful cc>
c.c: In function `main':
c.c:12: warning: passing arg 2 of `w0' from incompatible pointer type
</quoting>

how do i write function's w0 prototype correctly, to make it say that arg 2
of w0 is a pointer to an array, content of which is not modified by the
function?

--
x-pander
Nov 14 '05 #1
11 2079

"x-pander" <ng***@pitek.eu.org> wrote in message
news:ct***********@mamut1.aster.pl...
given the code:

<file: c.c>
typedef int quad_t[4];

void w0(int *r, const quad_t *p)
{
*r = (*p)[0];
}

int main()
{
quad_t m = {0};
int r;
w0(&r, &m);
return 0;
}
</file>

why does the cc produce:

<quoting respectful cc>
c.c: In function `main':
c.c:12: warning: passing arg 2 of `w0' from incompatible pointer type
</quoting>

how do i write function's w0 prototype correctly, to make it say that arg 2 of w0 is a pointer to an array, content of which is not modified by the
function?


What you have looks correct to me. Are you sure that's
the exact code giving the error?

-Mike
Nov 14 '05 #2
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:yJ*****************@newsread3.news.pas.earthl ink.net...
[...]
What you have looks correct to me. Are you sure that's
the exact code giving the error?


i am.
checked with gcc 2.95.4 and 4.0.0 (experimental)

of course it works ok, if i modify the prototype from:
void w0(int *r, const quad_t *p);
to:
void w0(int *r, quad_t *p);

--
x-pander
Nov 14 '05 #3

"x-pander" <ng***@pitek.eu.org> wrote in message
news:ct**********@mamut1.aster.pl...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:yJ*****************@newsread3.news.pas.earthl ink.net...
[...]
What you have looks correct to me. Are you sure that's
the exact code giving the error?


i am.
checked with gcc 2.95.4 and 4.0.0 (experimental)

of course it works ok, if i modify the prototype from:
void w0(int *r, const quad_t *p);
to:
void w0(int *r, quad_t *p);


A parameter defined as a pointer to a const object
should accept a pointer to a nonconst object, but
not the other way around. Unless there's something
I'm missing (or you're not telling), it seems to
me the compiler is wrong.

-Mike
Nov 14 '05 #4
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:NX*****************@newsread3.news.pas.earthl ink.net...
of course it works ok, if i modify the prototype from:
void w0(int *r, const quad_t *p);
to:
void w0(int *r, quad_t *p);


A parameter defined as a pointer to a const object
should accept a pointer to a nonconst object, but
not the other way around. Unless there's something
I'm missing (or you're not telling), it seems to
me the compiler is wrong.


well i do strongly doubt that gcc is wrong
note, that what we have here is not a pointer to const object, but a pointer
to array of const objects, which (it seems) somehow breaks the normal rules
of const qualifiied type compatibility

i (kindly) demand explanation

--
x-pander
Nov 14 '05 #5
x-pander wrote:
Given the code:

<file: c.c>
typedef int quad_t[4];

void w0(int *r, const quad_t *p) {
*r = (*p)[0];
}

int main() {
quad_t m = {0};
int r;
w0(&r, &m);
return 0;
}
</file>

why does the cc produce:

<quoting respectful cc>
c.c: In function `main':
c.c:12: warning: passing arg 2 of `w0' from incompatible pointer type
</quoting>

How do I write function's w0 prototype correctly
to make it say that arg 2 of w0 is a pointer to an array
[the] content of which is not modified by the function? cat c.c #include <stdio.h>

typedef int quad_t[4];

void w0(int *r, const quad_t p) {
*r = p[0];
}

int main(int argc, char* argv[]) {
quad_t m = {0, 1, 2, 3};
int r;
fprintf(stdout, "m = (");
for (size_t j = 0; j < 4; ++j)
fprintf(stdout, " %d", m[j]);
fprintf(stdout, ")\n");
w0(&r, m);
fprintf(stdout, "r = %d\n", r);
return 0;
}
gcc -Wall -std=c99 -pedantic -o c c.c
./c

m = ( 0 1 2 3)
r = 0

`const quad_t p' is a synonym for `const int p[4]' and
`quad_t m' is a synonym for `int m[4]'.

Nov 14 '05 #6
Mike Wahler wrote:
[...]
> What you have looks correct to me. Are you sure that's
> the exact code giving the error?


i am.
checked with gcc 2.95.4 and 4.0.0 (experimental)

of course it works ok, if i modify the prototype from:
void w0(int *r, const quad_t *p);
to:
void w0(int *r, quad_t *p);


A parameter defined as a pointer to a const object
should accept a pointer to a nonconst object, but
not the other way around. Unless there's something
I'm missing (or you're not telling), it seems to
me the compiler is wrong.
...


Unfortunately, the compiler is right. That's one of the issues caused by
arrays being "second-rate citizens" in C.

In C 'const' qualifier applied to array type 'quad_t' "falls through" to
actual array elements, i.e. 'const quad_t' is 'const int[4]'. It is
simply impossible to apply 'const' qualifier to array type itself. Any
attempts to do so will lead to the above "fall through" behavior and the
qualifier will be applied to the type of array elements, not to the
array type itself. For this reason, type 'const quad_t' is not
considered to be a const-qualified version of type 'quad_t' and the
conversion from 'quad_t*' to 'const quad_t*' is not allowed.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #7
On Wed, 26 Jan 2005 02:29:33 +0100, "x-pander" <ng***@pitek.eu.org>
wrote:
typedef int quad_t[4];

void w0(int *r, const quad_t *p)
{
*r = (*p)[0];
}

int main()
{
quad_t m = {0};
int r;
w0(&r, &m);
gcc accepts this code if you write:
w0(&r, (void *)&m);

I'm not sure why.

Nick.
return 0;
}


Nov 14 '05 #8

"Nick Austin" <se*********************@nospam.com> wrote in message
news:aj********************************@4ax.com...
On Wed, 26 Jan 2005 02:29:33 +0100, "x-pander" <ng***@pitek.eu.org>
wrote:
typedef int quad_t[4];

void w0(int *r, const quad_t *p)
{
*r = (*p)[0];
}

int main()
{
quad_t m = {0};
int r;
w0(&r, &m);


gcc accepts this code if you write:
w0(&r, (void *)&m);

I'm not sure why.


Because the cast discards the const qualifier.

Btw thanks, Andrey, I learned something new today
(I've never had cause to try writing anything like
'x-pander' did, so his issue has never arisen
for me.)

-Mike
Nov 14 '05 #9

In article <aj********************************@4ax.com>, Nick Austin <se*********************@nospam.com> writes:
On Wed, 26 Jan 2005 02:29:33 +0100, "x-pander" <ng***@pitek.eu.org>
wrote:
typedef int quad_t[4];

void w0(int *r, const quad_t *p)
{
*r = (*p)[0];
}

int main()
{
quad_t m = {0};
int r;
w0(&r, &m);


gcc accepts this code if you write:
w0(&r, (void *)&m);

I'm not sure why.


Because void * can be converted to any object pointer type, including
const int[4] *, which is what const quad_t * is an alias for.

One way to avoid the const "fall-through" Andrey described, which is
what's giving x-pander trouble, would be to make quad_t a structure
rather than an array. However, in this case, I think the fall-through
is actually what x-pander wants:

"arg 2 ... is a pointer to an array, content of which is not
modified by the function"

seems to me to say that, indeed, pointer-to-array-of-four-const-int
is the right type for the parameter. The real issue isn't that the
array "itself" can't be const-qualified; it's that such a type isn't
compatible with pointer-to-array-of-four-int, which is what he wants
to pass.

In this case, it's probably best just to drop the const qualifier
from the function declaration. The alternative is to cast the
actual argument, which seems to me to cost more than it's worth.

--
Michael Wojcik mi************@microfocus.com

I will shoue the world one of the grate Wonders of the world in 15
months if Now man mourders me in Dors or out Dors
-- "Lord" Timothy Dexter, _A Pickle for the Knowing Ones_
Nov 14 '05 #10
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:ct********@news1.newsguy.com...

In article <aj********************************@4ax.com>, Nick Austin
<se*********************@nospam.com> writes:
On Wed, 26 Jan 2005 02:29:33 +0100, "x-pander" <ng***@pitek.eu.org>
wrote:
>typedef int quad_t[4];
>
>void w0(int *r, const quad_t *p)
[...]

One way to avoid the const "fall-through" Andrey described, which is
what's giving x-pander trouble, would be to make quad_t a structure
rather than an array. However, in this case, I think the fall-through
is actually what x-pander wants:


i was also thinking of making this type:
typedef struct {
int word[4];
} quad2_t;
which would work as excpected, but it felt rather artificial to me to
encapsulate an array inside a struct.
however I think i'm going to use that, one question i have:
am i guaranteed that quad2_t type has exactly same memory representation as
quad_t, so that:
sizeof(quad2_t) == sizeof(quad_t) == 4*sizeof(int)
and the alignment is the same:
quad2_t m;
(*(quad_t *)&m)[0] = 0; /* is this valid ? */
seems to me to say that, indeed, pointer-to-array-of-four-const-int
is the right type for the parameter. The real issue isn't that the
array "itself" can't be const-qualified; it's that such a type isn't
compatible with pointer-to-array-of-four-int, which is what he wants
to pass.


that is exactly the problem.
it's a real shame that in C "pointer-to-array-of-four-int" is an not
compatible with "pointer-to-array-of-four-const-int".
could someone explain me the rationale behind this incompatibility?
also how exactly does the c99 standard explicitly states that?

--
x-pander

Nov 14 '05 #11

In article <ct***********@mamut1.aster.pl>, "x-pander" <ng***@pitek.eu.org> writes:
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:ct********@news1.newsguy.com...
In article <aj********************************@4ax.com>, Nick Austin
<se*********************@nospam.com> writes:
On Wed, 26 Jan 2005 02:29:33 +0100, "x-pander" <ng***@pitek.eu.org>
wrote:
One way to avoid the const "fall-through" Andrey described, which is
what's giving x-pander trouble, would be to make quad_t a structure
rather than an array.


i was also thinking of making this type:
typedef struct {
int word[4];
} quad2_t;
which would work as excpected, but it felt rather artificial to me to
encapsulate an array inside a struct.


It may feel artificial to you, but remember that "struct" is the C
keyword for creating new types; "typedef" only creates an alias. If
you want a type that behaves like a type, use struct. (Personally,
I'd use struct and skip the typedef; I think typedef is rarely
actually useful. But opinions differ on this question.)

You can always set an int* to the .word member inside each function
if you don't want to qualify every access:

void foo(const quad2_t *quad)
{
int *qdata;

if (! quad) return;
qdata = quad->word;
...
}
however I think i'm going to use that, one question i have:
am i guaranteed that quad2_t type has exactly same memory representation as
quad_t, so that:
sizeof(quad2_t) == sizeof(quad_t) == 4*sizeof(int)
and the alignment is the same:
quad2_t m;
(*(quad_t *)&m)[0] = 0; /* is this valid ? */
Hmm. There can't be padding before the first member of a structure,
and you only have one member here. There *can* be padding at the end
of a structure, and in fact there must be if any would be needed
between items in an array of that structure. In this case, though,
any reasonable implementation ought to be aligning an array of quad2_t
just as it would align an array of int, since that's all that quad2_t
contains.
The real issue isn't that the
array "itself" can't be const-qualified; it's that such a type isn't
compatible with pointer-to-array-of-four-int, which is what he wants
to pass.


that is exactly the problem.
it's a real shame that in C "pointer-to-array-of-four-int" is an not
compatible with "pointer-to-array-of-four-const-int".
could someone explain me the rationale behind this incompatibility?


I suspect the committee felt that it was an unreasonable burden to
impose more complex const-conversion rules on implementors. (This is
actually a conversion rule, not a compatibility rule.) The one we
have - pointer to T can be converted to pointer to const T - is
pretty simple and easy for implementors to get right.

The conversion you want - from pointer-to-array-of-T to pointer-to-
array-of-const-T - requires "pushing" the const conversion one step
further. If they allowed that, they'd probably have to allow things
like pointer-to-struct-containing-T to pointer-to-struct-containing-
const-T, and clearly things are getting much more complicated when
the implementation has to figure out if any consts are being added
anywhere in the conversion process.
also how exactly does the c99 standard explicitly states that?


I don't have a copy of C99 (must remember to get it from the ANSI
store...), just a copy of the final draft. C90 says:

[#2] For any qualifier q, a pointer to a non-q-qualified
type may be converted to a pointer to the q-qualified
version of the type; the values stored in the original and
converted pointers shall compare equal. (n869 6.3.2.3)

That says that the conversion is allowed. What makes it happen
automatically is:

-- both operands are pointers to qualified or unqualified
versions of compatible types, and the type pointed to
by the left has all the qualifiers of the type pointed
to by the right; (n869 6.5.16.1 #1)

This is part of the "simple assignment" rules, which (AIUI) apply in
this case. Note that the destination must have all the qualifiers of
the source, but not vice versa.

And 6.5.4 says that any pointer conversion not covered by 6.5.16.1
requires an explicit cast.

--
Michael Wojcik mi************@microfocus.com

This is a "rubbering action game," a 2D platformer where you control a
girl equipped with an elastic rope with a fishing hook at the end.
-- review of _Umihara Kawase Shun_ for the Sony Playstation
Nov 14 '05 #12

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

Similar topics

22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
204
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 =...
2
by: brzozo2 | last post by:
Hello, anyone knows if there is a way to use pointer notation instead of array when it comes to copying one object to another via a pointer of array to objects? Here is a very simple code to show...
10
by: Michael | last post by:
Hi, I'm trying to get my head around the relationship between pointers and arrays. If I have the following: int even = {2,4,6,8,10}; int *evenPtr = {even+4, even+3, even+2, even+1, even};...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
13
by: stephen b | last post by:
(apologies for cross posting from the moderated group..i'm sure you understand) Hello, I'm passing an array into a Constructor and hoping to use it as a pointer and store it as a class member...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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,...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.