typedef is failing in const char*

D

dwaach

Hi,

I am trying to compile the following program,

#include <iostream>

using namespace std;

typedef char* CHAR;
typedef const CHAR CCHAR;

void test(CCHAR pp)
{
cout<<"hello"<<endl;
}




void main()

{
const char* myChar = "tt";
test(myChar);
}


Output:

error: 'test' : cannot convert parameter 1 from 'const char *' to 'char
*const '
Conversion loses qualifiers


Why ?
How to resolve this ?

Regards,
Abhishek
 
K

Kai-Uwe Bux

dwaach said:
Hi,

I am trying to compile the following program,

#include <iostream>

using namespace std;

typedef char* CHAR;

I would suggest:

typedef char* char_ptr;


typedef const CHAR CCHAR;

Ok, now CCHAR is a const CHAR, i.e., a constant pointer to a mutable
character. Is that what you want, or do you want CCHAR to be a mutable
pointer to a const char?
void test(CCHAR pp)
{
cout<<"hello"<<endl;

Why "hello"? Do you mean:

cout << pp << '\n';
}




void main()

int main ()
{
const char* myChar = "tt";

Now, this is a mutable pointer to a const char and not a const pointer to a
char.
test(myChar);

This should fail to compile because test() requires a different argument.
}


Output:

error: 'test' : cannot convert parameter 1 from 'const char *' to 'char
*const '
Conversion loses qualifiers

As expected!

See above.
How to resolve this ?

typedef char const * CCHAR;


Best

Kai-Uwe Bux
 
I

Ian Collins

dwaach said:
Hi,

I am trying to compile the following program,

#include <iostream>

using namespace std;

typedef char* CHAR;
typedef const CHAR CCHAR;
Why bother?

This creates a type 'const pointer to char', not 'pointer to const char'.
void test(CCHAR pp)
{
cout<<"hello"<<endl;
}

void main()
main returns int.
{
const char* myChar = "tt";
test(myChar);
}


Why ?
How to resolve this ?
Do away with the silly typedefs.
 
D

Daniel T.

"dwaach said:
Hi,

I am trying to compile the following program,

#include <iostream>

using namespace std;

typedef char* CHAR;
typedef const CHAR CCHAR;

void test(CCHAR pp)
{
cout<<"hello"<<endl;
}




void main()

{
const char* myChar = "tt";
test(myChar);
}


Output:

error: 'test' : cannot convert parameter 1 from 'const char *' to 'char
*const '
Conversion loses qualifiers


Why ?
How to resolve this ?

Welcome to the crazy world of typedefs. You are making the CHAR const
(ie the char*) not the value that CHAR points to. You fix it by:

typedef const char* CCHAR;
 
M

mlimber

dwaach said:
Hi,

I am trying to compile the following program,

#include <iostream>

using namespace std;

typedef char* CHAR;
typedef const CHAR CCHAR;

void test(CCHAR pp)
{
cout<<"hello"<<endl;
}




void main()

See http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
{
const char* myChar = "tt";
test(myChar);
}


Output:

error: 'test' : cannot convert parameter 1 from 'const char *' to 'char
*const '
Conversion loses qualifiers


Why ?

When you add const to CCHAR it makes the *pointer* const, not the
pointee. Doing substitution back in for your typedefs yields:

void test(CCHAR) = void test(const CHAR)
= void test(CHAR const)
= void test(char* const) != void test(const char*)

Compare this example for more on this distinction:

struct S
{
char *p;
};

void Foo( S& s, const S& cs )
{
char c = 'c';
*s.p = 'a'; // Ok: non-const
s.p = &c; // Ok: non-const
*cs.p = 'a'; // Ok: pointee is non-const
cs.p = &c; // Error: pointer is const!
}

The reason for the error is that constness is not deep. It is applied
to the pointer in [cs], not the pointee of that pointer. Your example
is similar since it applies const to the pointer, not the pointee.
How to resolve this ?

First, call your pointer something other than the name of the type in
all caps. Try this:

#include <iostream>

using namespace std;

typedef char* PCHAR;
typedef const char* PCCHAR;

void test( PCCHAR pp)
{
cout<<"hello"<<endl;
}

int main()
{
const char* const s = "tt";
test(s);
return 0;
}

Cheers! --M
 
M

Markus Moll

Hi
Welcome to the crazy world of typedefs.

Crazy? I think it would be crazy if it was the other way around.
If CHAR denotes the type "pointer to char", then it is reasonable to
expect "const CHAR" to be the type "const pointer to char", not "pointer to
const char". Macros are crazy... typedefs are sane.
You are making the CHAR const
(ie the char*) not the value that CHAR points to.

CHAR does not point to any value, as it is a type.

Markus
 
E

Earl Purple

Markus said:
Crazy? I think it would be crazy if it was the other way around.
If CHAR denotes the type "pointer to char", then it is reasonable to
expect "const CHAR" to be the type "const pointer to char", not "pointer to
const char". Macros are crazy... typedefs are sane.

as a macro:

#define CHAR char *
#define CCHAR const CHAR

Now the OPs code would work because of the textual substitution.

(Of course that doesn't mean he should use them).
 
D

dwaach

Hi,

Thanks for the explanation.

Its understood now that the typedef need to be changed particularly
this one
typedef char* CHAR;
as
typedef const char* CHAR;

But here are the some bottlenecks,
1. Cannot modify 'typedef char* CHAR;' as it comes from a 3rd party
library and is use d internally in that.
2. Cannot change the datatype in main program as to
const char* const s = "tt";
3. I have created another library which requires CHAR to be const to
pointee. Here lies the [Bug]

So its like i get a typdefined value say '_VAL_' and i need to const
qualify this, in another set of programs,
thats why i tried with typdef and ended up in entirely different set of
usages.

Any suggstions on this,

:)

Regards,
Abhishek
dwaach said:
Hi,

I am trying to compile the following program,

#include <iostream>

using namespace std;

typedef char* CHAR;
typedef const CHAR CCHAR;

void test(CCHAR pp)
{
cout<<"hello"<<endl;
}




void main()

See http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
{
const char* myChar = "tt";
test(myChar);
}


Output:

error: 'test' : cannot convert parameter 1 from 'const char *' to 'char
*const '
Conversion loses qualifiers


Why ?

When you add const to CCHAR it makes the *pointer* const, not the
pointee. Doing substitution back in for your typedefs yields:

void test(CCHAR) = void test(const CHAR)
= void test(CHAR const)
= void test(char* const) != void test(const char*)

Compare this example for more on this distinction:

struct S
{
char *p;
};

void Foo( S& s, const S& cs )
{
char c = 'c';
*s.p = 'a'; // Ok: non-const
s.p = &c; // Ok: non-const
*cs.p = 'a'; // Ok: pointee is non-const
cs.p = &c; // Error: pointer is const!
}

The reason for the error is that constness is not deep. It is applied
to the pointer in [cs], not the pointee of that pointer. Your example
is similar since it applies const to the pointer, not the pointee.
How to resolve this ?

First, call your pointer something other than the name of the type in
all caps. Try this:

#include <iostream>

using namespace std;

typedef char* PCHAR;
typedef const char* PCCHAR;

void test( PCCHAR pp)
{
cout<<"hello"<<endl;
}

int main()
{
const char* const s = "tt";
test(s);
return 0;
}

Cheers! --M
 
J

Jim Langston

dwaach said:
Hi,

Thanks for the explanation.

Its understood now that the typedef need to be changed particularly
this one
typedef char* CHAR;
as
typedef const char* CHAR;

But here are the some bottlenecks,
1. Cannot modify 'typedef char* CHAR;' as it comes from a 3rd party
library and is use d internally in that.
2. Cannot change the datatype in main program as to
const char* const s = "tt";
3. I have created another library which requires CHAR to be const to
pointee. Here lies the [Bug]

So its like i get a typdefined value say '_VAL_' and i need to const
qualify this, in another set of programs,
thats why i tried with typdef and ended up in entirely different set of
usages.

Any suggstions on this,

Yeah. Don't use a typedef in your library.
:)

Regards,
Abhishek
dwaach said:
Hi,

I am trying to compile the following program,

#include <iostream>

using namespace std;

typedef char* CHAR;
typedef const CHAR CCHAR;

void test(CCHAR pp)
{
cout<<"hello"<<endl;
}




void main()

See http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
{
const char* myChar = "tt";
test(myChar);
}


Output:

error: 'test' : cannot convert parameter 1 from 'const char *' to 'char
*const '
Conversion loses qualifiers


Why ?

When you add const to CCHAR it makes the *pointer* const, not the
pointee. Doing substitution back in for your typedefs yields:

void test(CCHAR) = void test(const CHAR)
= void test(CHAR const)
= void test(char* const) != void test(const char*)

Compare this example for more on this distinction:

struct S
{
char *p;
};

void Foo( S& s, const S& cs )
{
char c = 'c';
*s.p = 'a'; // Ok: non-const
s.p = &c; // Ok: non-const
*cs.p = 'a'; // Ok: pointee is non-const
cs.p = &c; // Error: pointer is const!
}

The reason for the error is that constness is not deep. It is applied
to the pointer in [cs], not the pointee of that pointer. Your example
is similar since it applies const to the pointer, not the pointee.
How to resolve this ?

First, call your pointer something other than the name of the type in
all caps. Try this:

#include <iostream>

using namespace std;

typedef char* PCHAR;
typedef const char* PCCHAR;

void test( PCCHAR pp)
{
cout<<"hello"<<endl;
}

int main()
{
const char* const s = "tt";
test(s);
return 0;
}

Cheers! --M
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top