Pointer

This is a discussion on Pointer within the C Programming forums, part of the General Programming Boards category; Hi all I have a question regarding pointer. Hope someone can help me. Below are my codes, I have tagged: ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Pointer

    Hi all

    I have a question regarding pointer. Hope someone can help me. Below are my codes, I have tagged:

    =========================================
    Code:
    int main (int argc, const char * argv[]) {
    
    if ( fp == NULL )
            DoError( "Couldn't open file!" ); //<<<<
    }
    
    void    DoError( char *message ) { // <<<<
        printf( "%s\n", message );
        exit( 0 );
    }
    =========================================

    Looking at the //<<<< line of codes which I have tagged, can someone explain to me why void DoError (char *message) function requires a pointer (*message)?

    Thanks and much appreciated

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    if you pass a char instead of a char pointer, you get one character instead of a string. printf handles a char pointer (when used with the %s placeholder) as a string. it starts at the first character, and prints each one until it gets to a '\0'

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by Elkvis View Post
    if you pass a char instead of a char pointer......'
    Won't the compiler think that the value of the char is actually the address to the start of a string ? (...and give colourful results...)
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    Quote Originally Posted by manasij7479 View Post
    Won't the compiler think that the value of the char is actually the address to the start of a string ? (...and give colourful results...)
    yes, but if you pass a char, the function gets a char. what you do with it from there is your own problem

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Thank Elkvis for the explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2009, 05:41 PM
  2. Replies: 9
    Last Post: 06-13-2009, 03:31 AM
  3. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  4. Replies: 4
    Last Post: 08-28-2007, 12:51 AM
  5. Replies: 4
    Last Post: 11-05-2006, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21