what the meaning of this code

S

sam

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.
 
S

Sumanth

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
 
M

MiB

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.
 
Ä

书呆彭

sam 写é“:
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.
 
G

gtkmm

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 #
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top