Printf Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dis_is_eagle@yahoo.com

    #1

    Printf Problem

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

    int a=3,b=5;
    printf(&a["Ya!Hello!h ow 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

  • Ico

    #2
    Re: Printf Problem

    dis_is_eagle@ya hoo.com wrote:
    Hi.I could not understand the following printf statement.Thank s for any
    help.
    >
    int a=3,b=5;
    printf(&a["Ya!Hello!h ow 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!h ow 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!h ow is this?%s"]

    is the same as

    &"Ya!Hello!h ow 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

    Comment

    • steve

      #3
      Re: Printf Problem

      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
      Blogger is a blog publishing tool from Google for easily sharing your thoughts with the world. Blogger makes it simple to post text, photos and video onto your personal or team blog.


      dis_is_eagle@ya hoo.com wrote:
      Hi.I could not understand the following printf statement.Thank s for any
      help.
      >
      int a=3,b=5;
      printf(&a["Ya!Hello!h ow 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

      Comment

      • Keith Thompson

        #4
        Re: Printf Problem

        dis_is_eagle@ya hoo.com writes:
        Hi.I could not understand the following printf statement.Thank s for any
        help.
        >
        int a=3,b=5;
        printf(&a["Ya!Hello!h ow 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_Keit h) kst-u@mib.org <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.

        Comment

        • Christopher Benson-Manica

          #5
          Re: Printf Problem

          steve <adsrikanth@gma il.comwrote:
          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)gma il.com | don't, I need to know. Flames welcome.

          Comment

          Working...