473,218 Members | 1,931 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,218 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 5250
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.