Thread: Pointer

  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
    3,383
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    2,645
    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...)

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,383
    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, 04:41 PM
  2. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  3. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  4. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  5. Replies: 4
    Last Post: 11-05-2006, 01:57 PM
Website Security Test