Solved

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

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

0
Comment
Question by:abulka
9 Comments
 
LVL 5

Accepted Solution

by:
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.

Try

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

Or if this complains, try

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

Andy
0
 
LVL 11

Expert Comment

by:mikeblas
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
0
 
LVL 1

Expert Comment

by:nutsnuts
ID: 2647758
mikeblas was right, you should use ' ' instead of " ".
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:abulka
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.


0
 

Expert Comment

by:DennisSparrow
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 *.
0
 
LVL 11

Expert Comment

by:mikeblas
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
0
 
LVL 11

Expert Comment

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

..B ekiM
0
 

Author Comment

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

Author Comment

by:abulka
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 " "


0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 107
I could not build boost code, 10 95
Which Linux flavors will this run on? 6 96
Copy output image from TWindowsMediaPlayer 6 48
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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

829 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