Solved

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

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

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

18 Experts available now in Live!

Get 1:1 Help Now