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

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


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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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