Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what the meaning of this code

3 views
Skip to first unread message

sam

unread,
Sep 5, 2008, 7:20:09 AM9/5/08
to
Hi,
I just want to ask the question about this code
int len =0; len =5;
"x41" * (len);
In this is "x41" is multiply by 5 or it just can acquire space like
this
x41x41x41x41x41 .

I am a newbie and i am confused , If you can't understand this
question i am sorry, but i just want the clarification of this code.

Alf P. Steinbach

unread,
Sep 5, 2008, 9:15:34 AM9/5/08
to
* sam:

> I just want to ask the question about this code
> int len =0; len =5;
> "x41" * (len);
> In this is "x41" is multiply by 5 or it just can acquire space like
> this
> x41x41x41x41x41 .

Which compiler accepted that code?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Sumanth

unread,
Sep 5, 2008, 9:16:23 AM9/5/08
to

Hi,
You are trying to multiply an integer with a string, which has no
meaning. You will get a compilation error.

Rgds,
Sumanth

MiB

unread,
Sep 5, 2008, 9:26:39 AM9/5/08
to

The code you cite is not something you should waste your time with, it
seems to be part of an obfuscated program (intentionally written to be
hard to understand), or written by a real first-timer, or it may be
wrongly presented here.
int len = 0; len = 5;

This is identical to:
int len = 5;

The snippet:
"x41" * (len);

This is not valid in C++, you cannot multiply a const char array of
length 4 with an int. Old compilers for C and C++ may interpret this
as:
(int) "x41" * len;

This expression uses the memory address of the string constant "x41",
which may vary on each run of the program, and multiplies it with 5
(the content of the variable len). The round braces around len are
unneeded. Absent any instruction on what to do with the result of the
expression, it is then discarded - i.e. the whole line does nothing
and my be outright optimized away by the compiler. I assume there is
something missing in the code snippet you give.

best,

MiB.

书呆彭

unread,
Sep 5, 2008, 10:08:46 AM9/5/08
to
sam 写道:

well, i dont quite understand what you want to ask, nor what your code mean.

just from the code itself i thought:

"x41" is a const string, or, say the type is (char const *)

then the expression just got nothing meaningful.

gtkmm

unread,
Sep 6, 2008, 12:12:32 AM9/6/08
to

#include<iostream>
#include<string>
using namespace std;

string operator* ( string str,unsigned int times){
string tmp;
for( unsigned int i=0; i<times; i++) tmp+=str;
return tmp;
}

int main()
{
string str="hello,";
cout<<str*5<<endl;
return 0;
}
____________compiled by gcc 4.30_____
traxex # string_mul.exe
hello,hello,hello,hello,hello,
traxex #

0 new messages