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

Posted on 2000-03-22
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

Accepted Solution

pitonyak earned 5 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 " ".
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now