Thread: Confusing pointers, Prints wierd result

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Confusing pointers, Prints wierd result

    Hi All,

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    //First funtion
    void func(char *p)
    {
          p = "Hello";  // Statement 1
    }
    
    int main()
    {
     
     char *p = NULL;
     func(&p,&c);
     printf("Val of p:%s\n",p);
     
    }
    Alternative function declaration: //Function 2

    Code:
    void func(char **p)
    {
        
        *p = "Hello";         // Statement 2
        
    }


    What is confusing to me is the outputs:

    With the function declared as in Function 1:
    <NULL>

    With the function declared as in Function 2:
    Hello

    Doubts:
    1. How come Statement 1 is valid?
    2. Will address be assigned to the pointer variable p in Statement 1?
    3. What is the difference b/w *p="Hello" and p="Hello"?
    4. how is that by calling Function 2 instead of Function 1 it prints Hello?


    Thanks in advance

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    replace pointer with int var and to the same exercise

    Code:
    void func(int val)
    {
       val = 5;
    }
    void func2(int* pVal)
    {
       *pVal = 6;
    }
    int main()
    {
       int x = 0;
       func(x);
       printf("Value of x is %d\n",x);
    
       func2(&x);
       printf("Value of x now is %d\n", x);
       return 0;
    }
    After you understand what is going on here - look at your example where int type is replaced with char* type, that's all
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks for your time...

    1. How come Statement 1 is valid?
    2. Will address be assigned to the pointer variable p in Statement 1?
    3. What is the difference b/w *p="Hello" and p="Hello"?


    More doubts follow

    Code:
    int main()
    {
     
     char *p = NULL;
     func(&p,&c);
     printf("Val of p:%s\n",p);
    }
    
    Function 3
    void func(char **p)
    {
        char *q = "test"
        *p = &q;         // Statement 3
        
    }
    Ouput :
    Test

    What my doubts are while introducing Function 3:

    1. Now that i have declared char *q which is local to the Function, how come output is "test"?
    2. Is this dangling pointer? As the memory location is cleared once the function comes out?

    Thanks..

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    1. How come Statement 1 is valid?
    Why wouldn't it be? You're creating a local variable char *p, and setting it to point to the address of string "Hello" in static storage. Function exits and local variable p is destroyed. Sounds valid. The value of p in main() remains untouched.
    2. Will address be assigned to the pointer variable p in Statement 1?
    Yes. To the address of "Hello" in static storage.

    1. Now that i have declared char *q which is local to the Function, how come output is "test"?
    All string literals (e.g. everything inside double quotes) persist for the duration of the program.
    2. Is this dangling pointer? As the memory location is cleared once the function comes out?
    See above.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by sanddune008 View Post
    More doubts follow
    "doubt" is a state of uncertain belief. "question" is a request for information. What you have is a question, not a doubt. Do no use the word "doubt".
    Every once in a while I have to correct people on this. People that are mostly of indian origin are taught this wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers are confusing!
    By ali1 in forum C Programming
    Replies: 10
    Last Post: 09-07-2004, 10:41 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. COnfuSing PoIntErS
    By anamol12 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2003, 01:16 AM
  4. functions declaration, pointers, cast, .... CONFUSING
    By Rhodium in forum C Programming
    Replies: 7
    Last Post: 01-09-2003, 06:21 AM
  5. Replies: 16
    Last Post: 11-01-2002, 09:28 PM