Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Is nth char of string a space char - won't compile

Posted on 2000-03-22
Medium Priority
Last Modified: 2010-04-10
I'm trying to get the nth character from a string which lives in an array of strings, and see if that character is a space.  But it won't compile:
  string strArray[3];
  if (strArray[n][m] = " ") {

complains that
error C2440: '=' : cannot convert from 'char [2]' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Question by:abulka
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

pitonyak earned 10 total points
ID: 2647675

A couple of suggestions.... Do not use =, use == if you do it this way...

Second,  use isspace() which is probably in in <ctype.h>

The error is probalby because you are using unicode so you have a wide character which is two bytes.


if (isspace(StrArray[n][m])) {

Or if this complains, try

if (isspace((StrArray[n])[m])) {

LVL 11

Expert Comment

ID: 2647713
> The error is probalby because you are using unicode so
 > you have a wide character which is two bytes.

That's an incorrect diagnosis.

The problem is simply because abulka is using the assignment operator inappropriately. Simplified, he's doing

      char ch;
      ch = " ";

the right hand side is really a char[2]: one for the space, and one for the terminating null.  The compiler correctly complains that there's no  assignment operator that takes a char for the left hand side and a char[2] for the right hand side. And it also points out that there's no implicit conversions or implicit casting operators which can be applied

Wide characters have nothing to do with this.  If they did, you would've seen a wide character type (unsigned short, for instance) mentioned in the message.

..B ekiM

Expert Comment

ID: 2647758
mikeblas was right, you should use ' ' instead of " ".
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 2647778
Ok - I was doing a = instead of a ==
but then again...
    if ((strArray[n])[m] == " ") {
still complains that

D:\My Documents\aa Vc\ts1\CNodeView1.cpp(635) : error C2446: '==' : no conversion from 'char *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\My Documents\aa Vc\ts1\CNodeView1.cpp(635) : error C2040: '==' : 'int' differs in levels of indirection from 'char [2]'

the other technique
  if (isspace(strArray[n][m])) {
seems to compile ok.


Expert Comment

ID: 2647830
I think you need to use single quotes around the space instead of double quotes.

' ' denotes a single space character, type char.

" " is really a pointer to a string consisting of a space followed by a null character, data type char *.
LVL 11

Expert Comment

ID: 2648011
> data type char *.

It can be cast to char*, but it's really of the type char[2].

A little surprising you'd offer so little and cause it an answer, BTW.

..B ekiM
LVL 11

Expert Comment

ID: 2648015
Note that isspace() returns true for '\t' as well as a plain old space, by the way.

..B ekiM

Author Comment

ID: 2651230
nutsnuts already made this point re "" vs. '' as a comment.  thanks anyway.

Author Comment

ID: 2651275
Guys thanks for all the comments - I've accepted pitonyak becaue his solution was the first that compiled.
  if (isspace(strArray[n][m])) {

the solution using the other technique is of course
 if (strArray[n][m] == ' ') {

which uses == and ' '
instead of = and " "


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question