Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Printf Problem

Reply
Thread Tools

Printf Problem

 
 
Ico
Guest
Posts: n/a
 
      08-20-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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[i] 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.


--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
 
 
 
dis_is_eagle@yahoo.com
Guest
Posts: n/a
 
      08-20-2006
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[i] equals to*[a+i].But I could not understand the use of &
here.Bye.
Regards,
Eric

 
Reply With Quote
 
 
 
 
steve
Guest
Posts: n/a
 
      08-20-2006
please make sure that the code is compiled properly
syntax for printf
printf (" variables" , " control sting " );
if you want to print an address of a variable use %i or % u

thanx
http://eecsatri.blogspot.com

(E-Mail Removed) wrote:
> 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[i] equals to*[a+i].But I could not understand the use of &
> here.Bye.
> Regards,
> Eric


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-20-2006
(E-Mail Removed) writes:
> 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[i] equals to*[a+i].But I could not understand the use of &
> here.


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

You should never use this trick in real code.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      08-21-2006
steve <(E-Mail Removed)> wrote:

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

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Re: printf scanf problem code_wrong C Programming 1 08-02-2003 08:10 PM



Advertisments