7

What I want to do is to iterate through the quote till the end of the quote/(*quote has nothing in it). Is my code valid?

char *quote = "To be or not to be, that is the question.";
for (quote = 0; *quote != NULL; quote++){
*quote = tolower(*quote);
}
2
  • 3
    To cut to the point, quote = 0 is incorrect. Commented Sep 24, 2013 at 23:11
  • Why not use unsigned int? Commented Sep 24, 2013 at 23:15

3 Answers 3

13

You probably need another pointer to traverse the array, otherwise access to your original string will be lost.

And preferably only use NULL for pointers.

Don't use 0 as the initial value, unless you want to use indices instead (see below).

Doing char *quote = will simply make quote point to the read-only literal, instead of copying the string. Use char quote[] = instead.

char quote[] = "To be or not to be, that is the question.";
char *quotePtr;
for (quotePtr = quote; *quotePtr != '\0'; quotePtr++){
  *quotePtr = tolower(*quotePtr);
}

Test.

Using indices:

char quote[] = "To be or not to be, that is the question.";
int i;
for (i = 0; quote[i] != '\0'; i++){
  quote[i] = tolower(quote[i]);
}

Test.

Sign up to request clarification or add additional context in comments.

3 Comments

You know that this is most likely to blow up with an access violation due to the string literal being in a readonly page?
@Dukeling:- doing char *quote=" to be or not ....question"; and taking another char *quotePtr=quote; the same thing can be done .. what's wrong with taking char *quote ="to be or not...question" ...
@krishnaChandra Any string literal (something in "") is read-only. Trying to modify it would result in undefined behaviour - an error can occur, nothing can happen, it can actually modify the string literal or something worse. If you do char *quote=, you simply make quote point to this read-only string, so the above code would try to modify it. However, if you do char quote[]=, this would actually create a copy of the string literal and make quote point to the copy, which you are free to modify.
4

Consider this as an expansion to the answer given by Dukeling

When you use

char *quote = "Hello World";

This makes a read-only string, means that you can't change its contents in a simpler way.

Here *quote points to 'H'
BUT, you cannot do *quote = 'A';
This will give you an error.

If you wish to make changes to the characters in a string, it is a good habit to use arrays.

char quote[] = "Hello World";
Here also *quote points to 'H'
BUT, in this case *quote = 'A' is perfectly valid.
The array quote will get changed.

Comments

0

You're reassigning quote in your for initializer, which is invalid and will cause an access-violation because you're dereferencing it in the *quote != NULL part.

Semantically NULL and '\0' are equivalent, but syntactically I'd prefer this. Note that by using this approach you keep a pointer to (the start of) the string.

wchar const_t* quote = L"To be or not to be, that is the question.";

for( wchar_t* c = quote; *c != '\0'; c++ ) {

    *c = tolower( *c );
}

alternatively using an index:

wchar const_t quote[] = L"To be or not to be, that is the question.";

for( size_t i = 0; i < sizeof(quote); i++ ) {

    quote[i] = tolower( quote[i] );
}

(note that the semantics of sizeof will change if the value of quote is not known at compile time)

2 Comments

Why is reassigning then dereferencing a pointer invalid? Your code does the same, so presumably your reasons are just incorrect.
wchar const_t* quote is a constant string literal which you cannot modify so your code will fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.