C/C++ question on char*

P

Peithon

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

Andre Kostur

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

Pete Becker

Peithon said:
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. <g> s1 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)
 
N

Noah Roberts

Pete said:
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.
 
P

Peithon

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

Default User

Peithon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top