Connecting Tech Pros Worldwide Forums | Help | Site Map

what the meaning of this code

sam
Guest
 
Posts: n/a
#1: Sep 5 '08
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.

Sumanth
Guest
 
Posts: n/a
#2: Sep 5 '08

re: what the meaning of this code


On Sep 5, 12:20 pm, sam <sameer...@gmail.comwrote:
Quote:
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.
Hi,
You are trying to multiply an integer with a string, which has no
meaning. You will get a compilation error.

Rgds,
Sumanth
MiB
Guest
 
Posts: n/a
#3: Sep 5 '08

re: what the meaning of this code


On 5 Sep., 13:20, sam <sameer...@gmail.comwrote:
Quote:
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.
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.
=?UTF-8?B?5Lmm5ZGG5b2t?=
Guest
 
Posts: n/a
#4: Sep 5 '08

re: what the meaning of this code


sam 鍐欓亾:
Quote:
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.
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
Guest
 
Posts: n/a
#5: Sep 6 '08

re: what the meaning of this code


On 9月5日, 下午7时20分, sam <sameer...@gmail.comwrote:
Quote:
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.
#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 #

Closed Thread