By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

C/C++ question on char*

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
Peithon <Pe*****@googlemail.comwrote in news:1180716830.904080.138610
@k79g2000hse.googlegroups.com:
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?
You're assigning a pointer to const char to a pointer to non-const char.
Make pS1 and pS2 const char*. I'm guessing that C4090 (look in your
compiler documentation, and copying the entire warning message may have
been useful) is complaining about you removing the const.
Jun 1 '07 #2

P: n/a
Peithon wrote:
>
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,
I see something rather different. <gs1 is a pointer to const char. pS1
is a pointer to (non-const) char. So *s1 = 'a'; isn't legal, but *pS1 =
'a'; is. That's why the compiler is warning you: the interface to
strspcmp says that it won't modify the passed strings, but the code
permits modification.

You can do pointer arithmetic on s1 and s2. They're not const.

Keep in mind that when you're using pointers you really have two
objects: the pointer and the thing it points to. Either or both of those
objects can be const. const char* is a non-const pointer to const char.
char*const is a const pointer to non-const char.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 1 '07 #3

P: n/a
Pete Becker wrote:
Keep in mind that when you're using pointers you really have two
objects: the pointer and the thing it points to. Either or both of those
objects can be const. const char* is a non-const pointer to const char.
char*const is a const pointer to non-const char.
And the pointer is being passed by value so doesn't need to be const.
The pointers you passed in will point to the same place no matter what
manipulations you do with s1 and s2.
Jun 1 '07 #4

P: n/a
On 1 Jun, 18:13, Noah Roberts <u...@example.netwrote:
Pete Becker wrote:
Keep in mind that when you're using pointers you really have two
objects: the pointer and the thing it points to. Either or both of those
objects can be const. const char* is a non-const pointer to const char.
char*const is a const pointer to non-const char.

And the pointer is being passed by value so doesn't need to be const.
The pointers you passed in will point to the same place no matter what
manipulations you do with s1 and s2.
Thx. I'll just use the original const char *.
I forgot that pass by value applies to pointers as well.

Jun 1 '07 #5

P: n/a
Peithon wrote:
Hi,

This is a very simple question but I couldn't find it in your FAQ.
Please don't multi-post. You posted the same message to comp.lang.c.

Even though there is some common subset, there is no language called
C/C++. Figure out which one you want to use and direct your questions
accordingly.


Brian
Jun 1 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.