char *

C

Carmen Sei

why the following compile is OK

but will crash when execute?

the problem seem to in -
convertToUppercase( phrase );

==========
// Converting lowercase letters to uppercase letters
// using a non-constant pointer to non-constant data.
#include <iostream>
using std::cout;
using std::endl;

#include <cctype> // prototypes for islower and toupper
using std::islower;
using std::toupper;

void convertToUppercase( char * );

int main()
{
//char phrase[] = "characters and $32.98";

char * phrase = "characters and $32.98";

convertToUppercase( phrase );

return 0; // indicates successful termination
} // end main

// convert string to uppercase letters
void convertToUppercase( char *sPtr )
{
while ( *sPtr != '\0' ) // loop while current character is not '\0'
{
if ( islower( *sPtr ) ) // if character is lowercase,
*sPtr = toupper( *sPtr ); // convert to uppercase

sPtr++; // move sPtr to next character in string
} // end while
} // end function convertToUppercase
 
C

Carmen Sei

to declare a string like

"abcd"

I should never use char * right?

i should always use const char * for string declare?

or i should always use char[]?
 
S

Sharad

Carmen Sei said:
why the following compile is OK

but will crash when execute?

the problem seem to in -
convertToUppercase( phrase );

==========
// Converting lowercase letters to uppercase letters
// using a non-constant pointer to non-constant data.
#include <iostream>
using std::cout;
using std::endl;

#include <cctype> // prototypes for islower and toupper
using std::islower;
using std::toupper;

void convertToUppercase( char * );

int main()
{
//char phrase[] = "characters and $32.98";

char * phrase = "characters and $32.98";

convertToUppercase( phrase );

return 0; // indicates successful termination
} // end main

// convert string to uppercase letters
void convertToUppercase( char *sPtr )
{
while ( *sPtr != '\0' ) // loop while current character is not '\0'
{
if ( islower( *sPtr ) ) // if character is lowercase,
*sPtr = toupper( *sPtr ); // convert to uppercase

sPtr++; // move sPtr to next character in string
} // end while
} // end function convertToUppercase

Check a recent thread with subject "strcat" on this NG. In short, what are
you trying to modify and is it modifiable?

Sharad
P.S. - Also, does it work when you make phrase an array (the commented code)
and why?
 
S

Sharad

Carmen Sei said:
to declare a string like

"abcd"
I should never use char * right?

No, depends on your intention. "abcd" is a *string literal*.
i should always use const char * for string declare?

or i should always use char[]?

You can't modify string literals in legal C++ programs. You can use an array
if you have to modify the string, or even better look at std::string class.

Sharad
 
C

Carmen Sei

are you meaing any string like

"abcd" is imply a const char* automatically?

which means if i assign "abcd" to a variable -
char* var = "abcd";

then I suppose not to modify the content of char* var???
 
I

Ian Collins

Carmen Sei wrote:

[Please don't top-post]
are you meaing any string like

"abcd" is imply a const char* automatically?

which means if i assign "abcd" to a variable -
char* var = "abcd";
One would expect a decent compiler to emit a warning here, not required,
but useful.
then I suppose not to modify the content of char* var???
Yes, although the pointer should be const.
 
J

Jim Langston

Carmen said:
are you meaing any string like

"abcd" is imply a const char* automatically?

which means if i assign "abcd" to a variable -
char* var = "abcd";

then I suppose not to modify the content of char* var???

"abcd" is a constant character array. What happens with it is a differnet
story.

char Foo[] = "abcd";
Foo is allocated 5 bytes and the array "abcd" is copied into it.

char Foo* = "abcd";
Foo is allocated the size of a pointer and it points to the start of the
constant char array. Now, it is up to you to remember if Foo is pointing to
something you can change or not.

char Foo[] = "abcd";
char Bar* = "abcd";
// Bar[0] = 'x'; // Illegal/undefined
Bar = Foo;
Bar[0] = 'x'; // Now legal/well defined

The only reason that a char* can be assigned to point to a const char array
such as "abcd" is for C compatability. It was legal in C and for C code to
compile in C++ it is allowed there also. But shouldn't be done. Better is:

const char* Foo = "abcd";

Which if C++ didn't try to remain compatable with C would be the only legal
way to do it.
 
P

Paul Brettschneider

Jim said:
Carmen said:
are you meaing any string like

"abcd" is imply a const char* automatically?

which means if i assign "abcd" to a variable -
char* var = "abcd";

then I suppose not to modify the content of char* var???

"abcd" is a constant character array. What happens with it is a differnet
story.

char Foo[] = "abcd";
Foo is allocated 5 bytes and the array "abcd" is copied into it.

char Foo* = "abcd";
Foo is allocated the size of a pointer and it points to the start of the
constant char array. Now, it is up to you to remember if Foo is pointing
to something you can change or not.

char Foo[] = "abcd";
char Bar* = "abcd";
// Bar[0] = 'x'; // Illegal/undefined
Bar = Foo;
Bar[0] = 'x'; // Now legal/well defined

The only reason that a char* can be assigned to point to a const char
array
such as "abcd" is for C compatability. It was legal in C and for C code
to
compile in C++ it is allowed there also. But shouldn't be done.

This is such an inconsistent and disgusting language "feature" that it
should be removed, IMHO. Are there any plans to do so?

Sometimes you have to break backwards compatibility (as does for example
every new keyword). People with legacy code bases probably can rely on
compiler switches for non standard behaviour or, better yet, simply should
fix their code. (I realise this could me very tedious for large code
bases.)
 
J

Jim Langston

Paul said:
Jim said:
Carmen said:
are you meaing any string like

"abcd" is imply a const char* automatically?

which means if i assign "abcd" to a variable -
char* var = "abcd";

then I suppose not to modify the content of char* var???

"abcd" is a constant character array. What happens with it is a
differnet story.

char Foo[] = "abcd";
Foo is allocated 5 bytes and the array "abcd" is copied into it.

char Foo* = "abcd";
Foo is allocated the size of a pointer and it points to the start of
the constant char array. Now, it is up to you to remember if Foo is
pointing to something you can change or not.

char Foo[] = "abcd";
char Bar* = "abcd";
// Bar[0] = 'x'; // Illegal/undefined
Bar = Foo;
Bar[0] = 'x'; // Now legal/well defined

The only reason that a char* can be assigned to point to a const char
array
such as "abcd" is for C compatability. It was legal in C and for C
code to
compile in C++ it is allowed there also. But shouldn't be done.

This is such an inconsistent and disgusting language "feature" that it
should be removed, IMHO. Are there any plans to do so?

Sometimes you have to break backwards compatibility (as does for
example every new keyword). People with legacy code bases probably
can rely on compiler switches for non standard behaviour or, better
yet, simply should fix their code. (I realise this could me very
tedious for large code bases.)

I personally think it would be a good idea to not allow it be default and
allow some compiler switch to turn on the "feature". Or at least have the
compiler issue a warning about it. I doubt the "feature" will go away in
the standard any time soon though.
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top