const structure pointer compiler issue

R

rjones872

Consider the following structure:

typedef struct _Struct1
{
int n1;
int n2;
} Struct1, *PStruct1;

Now consider a function that takes a const pointer to this structure.
This allows only read only access to the structure within this Test1()
function.

void Test1(const PStruct1 pData)
{
}

Now let's say I have a global structure in memory that I've also
declared as const so that it cannot be modified as follows:

const Struct1 st = { 0, 0 };

I then issue this single line of code to pass the pointer to the "st"
structure to the Test1() API:

Test1(&st);

The compiler complains with:

error C2664: 'Test1' : cannot convert parameter 1 from 'const Struct1
*' to 'const PStruct1'
Conversion loses qualifiers

Since const Struct1* is equivalent to const PStruct1, I'm not sure
what qualifier is lost. If I remove the const from the "const Struct1
st = { 0, 0 }" it compiles properly but I want to leave that structure
declared as const. Likewise, if I declare the function as "void
Test1(const Struct1* pData)" it compiles as well.

My problem is that I like to use PStruct1 as the variable type instead
of Struct1* everywhere. Here is the code again which may be easier to
read:

typedef struct _Struct1
{
int n1;
int n2;
} Struct1, *PStruct1;

void Test1(const PStruct1 pData)
{
}

void Test2(void)
{
const Struct1 st = { 0, 0 };
Test1(&st); // Compiler complains here
}

What is the recommended way to handle this? Thank you!
 
F

Francesco S. Carta

Consider the following structure:

typedef struct _Struct1
{
int n1;
int n2;
} Struct1, *PStruct1;

Now consider a function that takes a const pointer to this structure.
This allows only read only access to the structure within this Test1()
function.

void Test1(const PStruct1 pData)

Here above you're not declaring an equivalent of "const Struct1*" -
which would be a "pointer to const Struct1" but the equivalent of
"Struct1* const" which has a completely different meaning.
{
}

Now let's say I have a global structure in memory that I've also
declared as const so that it cannot be modified as follows:

const Struct1 st = { 0, 0 };

I then issue this single line of code to pass the pointer to the "st"
structure to the Test1() API:

Test1(&st);

The compiler complains with:

error C2664: 'Test1' : cannot convert parameter 1 from 'const Struct1
*' to 'const PStruct1'
Conversion loses qualifiers

Hence the error. Modulo misunderstandings of mine, but I'm pretty sure I
got it right.
 
F

Francesco S. Carta

Here above you're not declaring an equivalent of "const Struct1*" -
which would be a "pointer to const Struct1" but the equivalent of
"Struct1* const" which has a completely different meaning.

Sorry, hit the send button by mistake and posted a non revised message.

Of course, "Struct1* const" means "const pointer to non-const Struct1",
which is different from what you want.

Since you're dealing with very important information (the fact that
something is const and the fact that something is a raw pointer) I would
suggest you not to hide it behind a typedef.

But if you really want it so, just do:

struct Struct {/*...*/};
typedef Struct* PStruct;
typedef const Struct* PCStruct;

// (or
// typedef Struct const * PCStruct;
// as you prefer)

And use them accordingly.
 
R

rjones872

Since you're dealing with very important information (the fact that
something is const and the fact that something is a raw pointer) I would
suggest you not to hide it behind a typedef.

But if you really want it so, just do:

struct Struct {/*...*/};
typedef Struct* PStruct;
typedef const Struct* PCStruct;

// (or
// typedef Struct const * PCStruct;
// as you prefer)

Thanks!
 
F

Francesco S. Carta


You're welcome. I hope you'll decide to not use those typedefs - just
for easing the life to the people who will eventually review your code ;-)
 
R

Rolf Magnus

Francesco said:
You're welcome. I hope you'll decide to not use those typedefs - just
for easing the life to the people who will eventually review your code ;-)

I'd generally advise to not hide plain pointers behind typedefs. This
particular exmple gives you no advantage whatsoever. It doesn't save you any
typing at all, and it doesn't make the code any more readable.
 
F

Francesco S. Carta

I'd generally advise to not hide plain pointers behind typedefs. This
particular exmple gives you no advantage whatsoever. It doesn't save you any
typing at all, and it doesn't make the code any more readable.

I perfectly agree with you - and in fact your post would have been
better addressed directly to the OP ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top