473,386 Members | 1,830 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,386 software developers and data experts.

typedef is failing in const char*

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
10 5276
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
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
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
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
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

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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Krishna | last post by:
I have three typedefs are defined below. typedef char* PCHAR; typedef const PCHAR PCCHAR1; typedef const char* PCCHAR2; In a piece of code, these typedef's are used as follows. PCCHAR1 pc1...
6
by: Lorn | last post by:
I was hoping some of you might be able to help me understand the following code defining a typedef for a time query to the WINAPI. I understand the basics of what's going on, I just don't...
15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
2
by: Mehta Shailendrakumar | last post by:
Hi all, Have alook at two different declarations below: typedef unsigned char uc; typedef void (*fptr) (void); The first declaration has a left part which is a C keyword: "unsigned char"...
5
by: John | last post by:
I thought I had a fairly good understanding of typedefs until I came across the following passage from C++ Primer, fourth edition by Stanley B. Lippman: "The mistake is in thinking of a typedef...
6
by: wongjoekmeu | last post by:
I need to rewrite some typedef to #define. For instance I rewrote typedef void* handle to #define handle void* But I saw for instance another typedef in my code which I don't understand,...
1
by: sophia | last post by:
Dear all, the following are the differences b/w #define and typedef ,which i have seen in Peter van der lindens book. is there any other difference between thes two ? The right way to...
7
by: Alex | last post by:
The code below can't be compiled: typedef char* POINTER; const POINTER ptr; ptr++; The compiler (Sun C 5.8 Patch 121015-04 2007/01/10) complains: "test.c", line ...: operand must be...
11
by: copx | last post by:
For some reason I cannot add a const qualifier to a typedefed pointer type if said type is used for a return value. It does work if the type is used for a parameter. I do not see the logic behind...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.