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

C/C++ question on 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
5 1497
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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:
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.