Printf Problem

I

Ico

Hi.I could not understand the following printf statement.Thanks for any
help.

int a=3,b=5;
printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);

Neither does my compiler. If you post any code, please make sure it
compiles. I will assume you missed some quotes and ment :

printf(&a["Ya!Hello!how is this?%s"],&b["junk/super"]);

This is an old trick, not to be used in real-life code. Due to the way
arrays an pointers work in the C language, the following two lines
have the same effect:

"hello"[2]
2["hello"]

Both expressions return 'l'. If I recall correctly, the clc faq has a
detailed explanation why this is the case.
I think a equals to*[a+i].But I could not understand the use of &


In your example, the expression

&a["Ya!Hello!how is this?%s"]

is the same as

&"Ya!Hello!how is this?%s"[a]

A was set to 3 before, so this expression returns the address of the
string+3, which happens to be "Hello!how is this?%s"

The same happens with the second expression, which is used as an
argument for the %s directive in the printf format string.
 
D

dis_is_eagle

Hi.I could not understand the following printf statement.Thanks for any
help.

int a=3,b=5;
printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);

I think a equals to*[a+i].But I could not understand the use of &
here.Bye.
Regards,
Eric
 
K

Keith Thompson

Hi.I could not understand the following printf statement.Thanks for any
help.

int a=3,b=5;
printf(&a["Ya!Hello!how is this?%s],&b["junk/super]);

I think a equals to*[a+i].But I could not understand the use of &
here.


No, a means *(a+i), which is equivalent to *(i+a), which is
equivalent to i[a].

You should never use this trick in real code.
 
C

Christopher Benson-Manica

steve said:
please make sure that the code is compiled properly

Good advice.
syntax for printf
printf (" variables" , " control sting " );

It's difficult to tell what you meant by this, and difficult to
believe that you in fact know that the prototype of printf is

int printf( const char * format, ... );
if you want to print an address of a variable use %i or % u

Wrong. The format specifier for printing an address (which must be
cast to void * if it is not already of that type) is %p.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top