Strings C++

V

VJ

Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.

Why not use std::string ? Look at this code how I compared strings:


#include <string>
#include <iostream>
int main()
{


std::string a("aaa");
std::string b("aba");
char str[10]="aba";

if(a==b)
{
std::cout<<"11111111111111"<<std::endl;
}
if(std::string(str)==b)
{
std::cout<<"2222222222222"<<std::endl;
}
}


In the 2nd if, I converted char[] to string and compared to string b
 
M

Marcus Kwok

Roland Pibinger said:
char string[10];
if (string == "test") {
printf("OK");
}

as other folks have mentioned, you are using C in C++. it is not a
standard C++ programme.

What's not standard C++ in the above code? It doesn't print "OK" but
that's another question ...

Well, in the other post where he posted the entire code, he has such
things as:

#define false 0
#define true 1

Redefining keywords is not allowed. Granted, this wasn't in the
original post, nor in the quoted bits above.
 
F

Frederick Gotham

Marcus Kwok:
#define false 0
#define true 1

Redefining keywords is not allowed.


I don't see why not... the compiler proper won't ever know what was written
before the preprocessor had its way.
 
G

Gavin Deane

Frederick said:
Marcus Kwok:



I don't see why not... the compiler proper won't ever know what was written
before the preprocessor had its way.

I was sure that redefining keywords was explicitly forbidden in the
standard, but I can't find anything that says so. Maybe someone knows
if it says that...

Gavin Deane
 
K

Kai-Uwe Bux

Gavin said:
I was sure that redefining keywords was explicitly forbidden in the
standard, but I can't find anything that says so. Maybe someone knows
if it says that...

The closest I found is [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to keywords.

Now, this applies to virtually every program I write.


Best

Kai-Uwe Bux
 
M

Marcus Kwok

Kai-Uwe Bux said:
The closest I found is [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to keywords.

Now, this applies to virtually every program I write.

So, is that saying that if you don't include any headers, then
redefining keywords is fine?
 
K

Kai-Uwe Bux

Marcus said:
Kai-Uwe Bux said:
The closest I found is [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to
keywords.

Now, this applies to virtually every program I write.

So, is that saying that if you don't include any headers, then
redefining keywords is fine?

The wording itself leaves open the question about translation units that do
not include headers. However, I have not found any provision in the
standard that would say you cannot redefine keywords in such translation
units. So, as far as I can tell, redefining keywords is well-defined in
that particular case. Now, I may have missed something -- the standard is
huge.


Best

Kai-Uwe Bux
 
M

Marcus Kwok

Kai-Uwe Bux said:
Marcus said:
Kai-Uwe Bux said:
The closest I found is [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to
keywords.

So, is that saying that if you don't include any headers, then
redefining keywords is fine?

The wording itself leaves open the question about translation units that do
not include headers. However, I have not found any provision in the
standard that would say you cannot redefine keywords in such translation
units. So, as far as I can tell, redefining keywords is well-defined in
that particular case.

Based on what you wrote, I am inclined to agree, though it is surprising
to me. Of course, I think it would be extremely difficult to write any
non-trivial program without using any headers at all.

I think that the rationale for the above allows for the situation in
which you manually implemented every particular feature you use, then it
would be possible to redefine the keywords to create some sort of
pseudo-language and be fully conforming. However, when including a
header, the original header implementer would probably assume standard
language semantics and thus redefining keywords would cause undefined
behavior because of the unforeseen changes in semantics.
Now, I may have missed something -- the standard is
huge.

Very true.
 
E

Earl Purple

You should first know how things work before you try to use them.

Perhaps it's what you meant but you should know what they do, not
necessarily how they do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top