Thread: Why is *format a pointer in printf()?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    Why is *format a pointer in printf()?

    Bear with me on this one. I simply don't understand the following description:

    int printf ( const char * format, ... );

    Why is format a pointer here? If I can be pushed in the right direction, I'd greatly appreciate it. Thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Because you're giving it a string. A string in C is a sequence of chars, basically a char array. When you pass an array to a function, in reality you are passing a pointer to the first element of the array.

    When you use a string literal in C, its address is substitued into its place. So when you write something like the following code, the internal behavior is quite different than you might expect:

    Code:
    printf("Hello, world!\n");
    The string "Hello, world!\n" is probably saved in the .data segment of your program or some other area where it resides outside of the .code section. In its place, the address of the start of where the entire string is stored in memory is given to the printf() function. This allow printf() to receive the entire sequence of chars by a pointer.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    I couldn't have asked for a better explanation. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM