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

assigning const char* to char*

Hi,

This is a very simple question but I couldn't find it in your FAQ.

I'm using VC++ and compiling a C program, using the /TC flag.

I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
{

char* pS1 = s1;
char* pS2 = s2;
...
...

}

As you can see, I assign the two const ptrs to some non-const ptrs to
do some ptr arithmetic,
like incrementing the ptrs to compare the strings.
But I'm getting warning C4090 complaining about the assignment.
I thought I was doing the standard thing for ptr arithmetic. What am I
doing wrong?
How do I get rid of the warning?

Jun 1 '07 #1
9 10478
Peithon said:
Hi,

This is a very simple question but I couldn't find it in your FAQ.

I'm using VC++ and compiling a C program, using the /TC flag.

I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
Although it has nothing to do with your question, I should perhaps point
out that the name strspcmp is reserved. str_spcmp is not, however.
{

char* pS1 = s1;
char* pS2 = s2;
...
...

}

As you can see, I assign the two const ptrs to some non-const ptrs to
do some ptr arithmetic,
like incrementing the ptrs to compare the strings.
You don't need to do that, and in fact it's a bad idea. The const char *
syntax doesn't mean "this pointer can't change". It means "you can't
write through this pointer". ++s1 is fine. *s1 = '\0', however, is not.
And since you don't want to do that anyway, I suggest that you use
const char * - in which case pS1 and pS2 become redundant; you might as
well use s1 and s2 directly. (Remember that C is pass-by-value, so ++s1
only changes your local copy of the pointer, not the value of the
pointer passed by the caller.)

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 1 '07 #2
Peithon <Pe*****@googlemail.comwrites:
Hi,

This is a very simple question but I couldn't find it in your FAQ.

I'm using VC++ and compiling a C program, using the /TC flag.

I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
{

char* pS1 = s1;
char* pS2 = s2;
...
...

}

As you can see, I assign the two const ptrs to some non-const ptrs to
do some ptr arithmetic,
like incrementing the ptrs to compare the strings.
But I'm getting warning C4090 complaining about the assignment.
I thought I was doing the standard thing for ptr arithmetic. What am I
doing wrong?
How do I get rid of the warning?
For a start you post the warning you are getting :-;
Jun 1 '07 #3
Peithon <Pe*****@googlemail.comwrites:
Hi,

This is a very simple question but I couldn't find it in your FAQ.

I'm using VC++ and compiling a C program, using the /TC flag.

I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
You are invading the implementation's name space.
{

char* pS1 = s1;
char* pS2 = s2;
...
...

}

As you can see, I assign the two const ptrs to some non-const ptrs
The pointers are not const. They point to const char. Subtle but
important distinction.
to do some ptr arithmetic,
Odd. You can do arithmetic on const char * as well and you would not
get the warnings.
like incrementing the ptrs to compare the strings.
But I'm getting warning C4090 complaining about the assignment.
As you must. It requires a diagnostic.
I thought I was doing the standard thing for ptr arithmetic. What am I
doing wrong?
How do I get rid of the warning?
Just to the pointer arithmetic on the const char *s1 and s2.

--
Ben.
Jun 1 '07 #4
On 1 Jun, 18:47, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Peithon <Peit...@googlemail.comwrites:
Hi,
This is a very simple question but I couldn't find it in your FAQ.
I'm using VC++ and compiling a C program, using the /TC flag.
I've got a function for comparing two strings
int strspcmp(const char * s1, const char * s2)

You are invading the implementation's name space.
{
char* pS1 = s1;
char* pS2 = s2;
...
...
}
As you can see, I assign the two const ptrs to some non-const ptrs

The pointers are not const. They point to const char. Subtle but
important distinction.
to do some ptr arithmetic,

Odd. You can do arithmetic on const char * as well and you would not
get the warnings.
like incrementing the ptrs to compare the strings.
But I'm getting warning C4090 complaining about the assignment.

As you must. It requires a diagnostic.
I thought I was doing the standard thing for ptr arithmetic. What am I
doing wrong?
How do I get rid of the warning?

Just to the pointer arithmetic on the const char *s1 and s2.

--
Ben.
Thanks, I didn't see the distinction between const char * and char *
const.
Much appreciated.

Jun 1 '07 #5
Peithon wrote:
Hi,

This is a very simple question but I couldn't find it in your FAQ.

I'm using VC++ and compiling a C program, using the /TC flag.

I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
{

char* pS1 = s1;
char* pS2 = s2;
...
...

}

As you can see, I assign the two const ptrs to some non-const ptrs to
do some ptr arithmetic,
like incrementing the ptrs to compare the strings.
But I'm getting warning C4090 complaining about the assignment.
I thought I was doing the standard thing for ptr arithmetic. What am I
doing wrong?
How do I get rid of the warning?
typing

const char *a;

means you have a pointer to a char and are not allowed to modify what it
points to (but you are allowed to modify the pointer).
that means

char b;

a="hello";
a++;
b=*a;

is valid code, but not:
a=&b;
*a=' '; /* this line is not valid */

since this is valid:

char *c;
c=&b;
*c=' ';

but modifying something pointed to by a const pointer is not is this
also not valid:

a=&b;
c=a; /* gives the warning you mentioned */
*c=' '; /* this statement is still valid */

Jun 1 '07 #6
Peithon <Pe*****@googlemail.comwrites:
This is a very simple question but I couldn't find it in your FAQ.

I'm using VC++ and compiling a C program, using the /TC flag.

I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
{

char* pS1 = s1;
char* pS2 = s2;
...
...

}

As you can see, I assign the two const ptrs to some non-const ptrs to
do some ptr arithmetic,
like incrementing the ptrs to compare the strings.
But I'm getting warning C4090 complaining about the assignment.
I thought I was doing the standard thing for ptr arithmetic. What am I
doing wrong?
How do I get rid of the warning?
This has already been ably answered, but perhaps a simpler example
might make the point clearer. I think your point of confusion is that
"const char*" means "pointer to const char", not "const pointer to
char", but I'll explore other issues as well.

int main(void)
{
const char *ptr = "hello";
char *copy_of_ptr = ptr;
*ptr = 'H';
return 0;
}

The declaration "const char *ptr" doesn't make ptr const; it makes
what it points to const. Here I've initialized ptr to point to a
string literal, i.e., to an array of 6 characters 'h', 'e', 'l', 'l',
'o', '\0'. This array could be stored in read-only memory; attempting
to modify it could cause the program crash (it's undefined behavior).

The declaration of copy_of_ptr doesn't use the "const" keyword, which
means I'm allowed to modify either the pointer object itself *or* what
it points to. By "allowed to", I mean that the compiler won't
complain if I try to do it; it will go ahead and generate code to do
what I asked. It's up to me to make sure that I actually *can* modify
whatever I'm trying to modify. If I lie to the compiler, it may or
may not complain.

But in this case, I *tried* to lie to the compiler and it caught me at
it. I attempted to initialize a char* to the value of ptr. If I
could do this, it would allow me to (attempt to) modify whatever
copy_of_ptr points to. And that's exactly why the language forbids
assigning the value of ptr to copy_of_ptr (it's a constraint
violation, requiring a diagnostic). My error is caught during
compilation, when I attempt to copy the pointer, which is much better
than not catching it until run time, when I actually try to modify the
string.

If the language, and therefore the compiler, allowed me to copy the
value of ptr to copy_of_ptr, then it would also allow me to attempt to
modify the string that it points to (remember that it might be in
read-only memory), all without telling me that I've done something
dangerous. Fortunately, the rules of the language are such that I
can't get away with this particular error quite so easily. (It is
overly lax about string literals, but that's another story.)

If you want a pointer to const char, declare it as:

const char *ptr;

If you want a const pointer to char (you can't modify the pointer, but
you can modify what it points to):

char *const ptr;

And if you want both the pointer *and* what it points to be read-only:

const char *const p;

The placement of the "const" keyword is tricky. If you can find a
program called "cdecl", it can help you with this kind of thing; I
used it to confirm these declarations.

% cdecl
Type `help' or `?' for help
cdecldeclare p as const pointer to char
char * const p
cdecldeclare p as const pointer to const char
const char * const p
cdeclexplain const char *p
declare p as pointer to const char
cdecl>

One thing to watch out for: cdecl treats "ptr" as a synonym for
"pointer", so if you tell it "declare ptr as pointer to const char" it
will report a syntax error.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 1 '07 #7
Peithon wrote:
>
This is a very simple question but I couldn't find it in your FAQ.
I'm using VC++ and compiling a C program, using the /TC flag.
I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
{
char* pS1 = s1;
char* pS2 = s2;
...
}

As you can see, I assign the two const ptrs to some non-const ptrs
to do some ptr arithmetic, like incrementing the ptrs to compare
the strings. But I'm getting warning C4090 complaining about the
assignment. I thought I was doing the standard thing for ptr
arithmetic. What am I doing wrong? How do I get rid of the warning?
Declare the local variables as const:

const char *pS1 = s1;

etc. You have declared the input as pointers to const, which are
therefore not modifiable.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 1 '07 #8
Keith Thompson wrote:
>
.... snip ...
>
One thing to watch out for: cdecl treats "ptr" as a synonym for
"pointer", so if you tell it "declare ptr as pointer to const
char" it will report a syntax error.
[OT] A devilish concoction. Not mentioned in the help material.
And there are others of this type.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 1 '07 #9
Richard wrote:
Peithon <Pe*****@googlemail.comwrites:
>This is a very simple question but I couldn't find it in your
FAQ. I'm using VC++ and compiling a C program, using the /TC
flag. I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
{
char* pS1 = s1;
char* pS2 = s2;
...
}

As you can see, I assign the two const ptrs to some non-const
ptrs to do some ptr arithmetic, like incrementing the ptrs to
compare the strings. But I'm getting warning C4090 complaining
about the assignment. I thought I was doing the standard thing
for ptr arithmetic. What am I doing wrong? How do I get rid of
the warning?

For a start you post the warning you are getting :-;
You mean you can't see the problem?

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ben | last post by:
Hi all, I would like to know if there is an easy way to assign a string to an int. I have a struct such as: struct Values { int a; int b; }
3
by: Senthilraja | last post by:
I am able to compile the following code and get "hello" as ouput everytime I execute it:- #include <stdio.h> int main (void) { char *str; str="hello"; puts(str); return 0;
10
by: Steve Pope | last post by:
The first of the following functions compiles, the second gives what I think is a spurious error: "cannot convert `const char' to `char *' in assignment". void foo(int m) { char *str; if (m...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: allan.mcrae | last post by:
As part of a very simple memory leak detector, I am trying to store the value of __FILE__ in a char*. Since gcc4.2 I get the following warning... warning: deprecated conversion from string...
17
by: Cliff | last post by:
Hi, I'm in the process of porting some code from a 3rd party and have hit a problem with the following: typedef struct { unsigned char Red; unsigned char Green; unsigned char Blue;...
7
by: Usenet User | last post by:
I am using Visual Studio.NET 2003, and to my surprise, the below code in a simple console application does not work for me: char *str = "hello!"; *str = 'H'; <-- error str = 'H'; <-- error...
1
by: hingwah | last post by:
Consider the following code:: void log_function(const char *str); // 3rd party logging function which don't use const std::string & as input class Test { public: std::string...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.