Pointers - ????

This is a discussion on Pointers - ???? within the C Programming forums, part of the General Programming Boards category; I am not familiar with pointers.. So, Kindly help to get clear understanding of pointers... in Main, i am Passing ...

  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
    20,981
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,981
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    as a side note to print the address %p format should be used
    printf("%p", (void*)s);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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