Thread: Pointers - ????

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Pointers - ????

    I am not familiar with pointers.. So, Kindly help to get clear understanding of pointers...

    in Main, i am Passing ptr ip to func...
    In func, I am pointing it to "treat"..
    After returning 4m Main, I am not getting "Value of treat" in Main..
    I am getting only previous ip value.............

    Moreover, Address of Ip in Main and st in func are Same..... But if i pass &ip to func, i am getting value of treat in Main......... Why i am not getting "value of treat" earlier and How i am getting aftr passing &?..


    [ Might be silly Quesn.. I m not familiar with that]..


    Code:
    #include<stdio.h>
    
    
    void func(char *st);
    char *ip="I am Here Only Buddy";
    char treat[]="Hello i am no more here!!";
    int main()
    {
        
        func(ip);
        
        printf("Result aftr func:%s",ip);
        printf("Address of ip:%u",ip);
        getchar();
        
        return 0;
        
    }
    
     
    void func(char *st)
    {
         printf("Value here b4:%s",st);
         printf("Address of st:%u",st);
         st=treat;
         printf("Value:%s",st);
         //return st;
         
         }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ganesh bala
    in Main, i am Passing ptr ip to func...
    In func, I am pointing it to "treat"..
    After returning 4m Main, I am not getting "Value of treat" in Main..
    That is because you merely change the pointer parameter named st in func(). If you actually wanted to change ip to point to something else, you should have func() take a pointer to a pointer as an argument.

    Other things to note: since it points to the first char in a string literal, ip should be declared as pointing to a const char*, not just a char*. Likewise for the parameter of func(). You should avoid global variables, and use %p to print pointers (cast them to void* for printing).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Unhappy Thanks

    Thanks 4 ur suggestions...

    still i am not clear... Address of st = address of ip;

    So, st=treat; // will make ip to point to treat array..

    Why it was nt reflecting ??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ganesh bala
    still i am not clear... Address of st = address of ip;

    So, st=treat; // will make ip to point to treat array..
    No, that is not the case. The address of st is not the address of ip. After you assign treat to st, the value of st is the same as the value of treat, but the value of a pointer is either the address of what the pointer points to or a null pointer constant.

    Quote Originally Posted by ganesh bala
    Why it was nt reflecting ??
    You are merely changing the value of st in the function. When the function returns, st is destroyed. ip continues to point to what it pointed to, since ip has nothing to do with st other than the fact that st happened to point to what ip points to at the start of func().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Talking Thanks again

    Thanks LaserLight !!

    I understood ...

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    as a side note to print the address %p format should be used
    printf("%p", (void*)s);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM