423,850 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

typedef is failing in const char*

P: n/a
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

Jul 12 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
dwaach wrote:
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!
>

Why ?
See above.
How to resolve this ?
typedef char const * CCHAR;
Best

Kai-Uwe Bux
Jul 12 '06 #2

P: n/a
dwaach wrote:
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.

--
Ian Collins.
Jul 12 '06 #3

P: n/a
In article <11*********************@i42g2000cwa.googlegroups. com>,
"dwaach" <xb**************@gmail.comwrote:
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;
Jul 12 '06 #4

P: n/a
dwaach wrote:
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-lit....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

Jul 12 '06 #5

P: n/a
Hi

Daniel T. wrote:
>typedef char* CHAR;
typedef const CHAR CCHAR;

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

Jul 12 '06 #6

P: n/a

Markus Moll wrote:
>
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).

Jul 12 '06 #7

P: n/a
In article <44***********************@newsread2.arcor-online.net>,
Markus Moll <mo**@rbg.informatik.tu-darmstadt.dewrote:
Hi

Daniel T. wrote:
typedef char* CHAR;
typedef const CHAR CCHAR;
Welcome to the crazy world of typedefs.

Crazy? I think it would be crazy if it was the other way around.
I was being silly.
Jul 12 '06 #8

P: n/a
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

mlimber wrote:
dwaach wrote:
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-lit....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
Jul 13 '06 #9

P: n/a

"dwaach" <xb**************@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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

mlimber wrote:
>dwaach wrote:
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-lit....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

Jul 13 '06 #10

P: n/a
dwaach wrote:
Hi,

Thanks for the explanation.
Please don't top-post. Your reply belongs following or interspersed
with properly trimmed quotes. See the newsgroup FAQ:
<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>

Brian
Jul 13 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.