473,562 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2099

"x-pander" <ng***@pitek.eu .org> wrote in message
news:ct******** ***@mamut1.aste r.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******@mkwah ler.net> wrote in message
news:yJ******** *********@newsr ead3.news.pas.e arthlink.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******@mkwah ler.net> wrote in message
news:yJ******** *********@newsr ead3.news.pas.e arthlink.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******@mkwah ler.net> wrote in message
news:NX******** *********@newsr ead3.news.pas.e arthlink.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************ *********@nospa m.com> wrote in message
news:aj******** *************** *********@4ax.c om...
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************ *********@nospa m.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

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

Similar topics

22
12712
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 exactly this, and similarly generic functions seem to have useful applications. In a few posts I read, it was suggested to avoid void pointer...
4
2797
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
12937
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 = {0,1,2,4,9};
2
1636
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 what I mean: /* 2 classes, one holds the int number, other is a table which uses pointer to array of objects */ #include <iostream>
10
407
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}; int **ip = evenPtr+3;
29
3627
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
13
2130
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 for future use. So far, I'm just causing crashes, psuedo code below: double block; foo = MyClass(block);
5
3625
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 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer...
50
3447
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
7652
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8095
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6219
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5476
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5193
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2073
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 we have to send another system
1
1187
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.