Thread: Help with pointers and a function

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    Help with pointers and a function

    Right i have the following:


    Code:
    char *G_PTR = NULL   //its a global
    
    in a module i have this
    
    char * ptr
    func(&ptr)
    
    void func (char *ptr);
    {
    
          //lets say G_PTR has been assigned some values
         //so how do i assign the value in G_PTR to ptr?
        //tried  different options
        *ptr = G_PTR
          ptr = G_PTR
    }
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is just a pointer, in the end, so:
    Code:
    *ptr = *G_PTR;
    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
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by newbie30 View Post
    Right i have the following:


    Code:
    char *G_PTR = NULL   //its a global
    
    in a module i have this
    
    char * ptr
    func(&ptr)
    
    void func (char *ptr);
    {
    
          //lets say G_PTR has been assigned some values
         //so how do i assign the value in G_PTR to ptr?
        //tried  different options
        *ptr = G_PTR
          ptr = G_PTR
    }
    Thanks
    The *(related to pointer) operator is the "value at address" operator. So, whenever there is a pointer then you can use the * operator to find the value at the address pointed to by the pointer.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by newbie30 View Post
    Code:
          //lets say G_PTR has been assigned some values
         //so how do i assign the value in G_PTR to ptr?
        //tried  different options
    I would be slightly suspicious of laserlight's method if this char* pointer refers to a string. In that case, to copy the value you will need to assign memory to ptr and use strcpy. If you just want them to point to the same data:

    ptr = G_PTR;

    is fine, but beware changes to the data (via either of the pointers) will obviously mean that data is also changed for the other one.

    You need to be a bit more particular about what you are trying to do.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    I would be slightly suspicious of laserlight's method if this char* pointer refers to a string. In that case, to copy the value you will need to assign memory to ptr and use strcpy.
    Good point. However...

    Quote Originally Posted by MK27
    If you just want them to point to the same data:

    ptr = G_PTR;

    is fine
    Yes, but it is also useless, since ptr will go out of scope immediately afterwards.
    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

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Yes, but it is also useless, since ptr will go out of scope immediately afterwards.
    Yeah, it's not really clear why the OP wants or needs to use a GLOBAL pointer this way, unless s/he has misunderstood something about global scope.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed