Assigning unsigned char*

Discussion in 'C++' started by Donos, Oct 9, 2007.

  1. Donos

    Donos Guest

    Hello

    I have the following program,

    void getValue(unsigned char* pVal)
    {
    pVal = allValues();
    }

    unsigned char* allValues()
    {
    unsigned char* a;
    -------------------
    // Some code in here
    -------------------
    return a;
    }

    Somehow am not able to get the character pointer "a" back into "pVal".

    Why this is happening?
     
    Donos, Oct 9, 2007
    #1
    1. Advertising

  2. Donos wrote:
    > Hello
    >
    > I have the following program,
    >
    > void getValue(unsigned char* pVal)


    C++ (and C) pass all variables by value, however C++ has a special value
    called a "reference". When variables are passed by reference,
    assignment to them happens to the variable being passed and not the
    usual copy in the call.

    void getValue(unsigned char * & pVal)

    > {
    > pVal = allValues();
    > }
    >
    > unsigned char* allValues()
    > {
    > unsigned char* a;
    > -------------------
    > // Some code in here
    > -------------------
    > return a;
    > }
    >
    > Somehow am not able to get the character pointer "a" back into "pVal".
    >
    > Why this is happening?


    Consider this:

    void foo( int x )
    {
    x = 2;
    }

    int main()
    {
    foo( 5 ); // what should foo do here ?

    int x = 4;

    foo( x ); // so why should it change what it does here ?
    }

    If you want to be able to change the passed variable then pass by
    reference or pass a pointer to it


    void foo( int & x )
    {
    x = 3;
    }

    int main()
    {
    foo( 5 ); // ILLEGAL - can't convert 5 to a non const reference
    }

    using pointers ...

    void foo( int * x )
    {
    * x = 5;
    }

    Passing pointers should be relegated to situations where a reference
    can't be used. e.g. passing arrays or passing null pointers.
     
    Gianni Mariani, Oct 9, 2007
    #2
    1. Advertising

  3. On 9 Oct, 22:04, Donos <> wrote:
    > void getValue(unsigned char* pVal)
    > {
    > pVal = allValues();
    > }
    >
    > unsigned char* allValues()
    > {
    > unsigned char* a;
    > -------------------
    > // Some code in here
    > -------------------
    > return a;
    > }
    >
    > Somehow am not able to get the character pointer "a" back into "pVal".
    >


    Global replace "unsigned char*" with "int" and you
    should see why this doesn't work as you expect.
    One solution is to amend getValue to:
    void getValue(unsigned char*& pVal)
    {
    pVal = allValues();
    }
    or, less opaquely:
    unsigned char* getValue()
    { return allValues(); }
    (but I'm guessing that the reason for the first
    form is because you've simplified the code
    for posting).
     
    tragomaskhalos, Oct 9, 2007
    #3
  4. Donos

    Jim Langston Guest

    "Donos" <> wrote in message
    news:...
    > Hello
    >
    > I have the following program,
    >
    > void getValue(unsigned char* pVal)


    The paramater pVal is a pointer value. It is passed by copy, that is, what
    ever pointer is passed as the paramater is copied into the local variable
    pVal.

    > {
    > pVal = allValues();


    The local variable pVal is changed. But remember, it's just a local
    variable. It's just a copy of the addess/pointer that was passed it. Once
    this function returns, the temporary varaible is destroyed.

    > }
    >
    > unsigned char* allValues()
    > {
    > unsigned char* a;
    > -------------------
    > // Some code in here
    > -------------------
    > return a;
    > }
    >
    > Somehow am not able to get the character pointer "a" back into "pVal".
    >
    > Why this is happening?


    As stated, because you are only changing the local variable pVal, not
    whatever was passed in. I'm guessing you wanted to change the pointer that
    was passed in. If you wish to change the variable that is passed it you
    need to pass it by reference. There are 2 ways to do this, a pointer to the
    variable, or a reference to the variable. In C the only way was a pointer
    to the variable. In C++ we also have references to variables. If you
    change getVal like:

    void getVal( unsigned char*& pVal )
    {
    pVal = allValue();
    }

    then the variable itself that is passed by as the parameter is changed. The
    C way was to pass a pointer to the variable.

    void getVal( unsigned char** pVal )
    {
    *pVal = allValue();
    }

    Just understand, that if you are not passing a reference, you are passing a
    copy, even if they are pointers, it's still a copy of the pointer.
     
    Jim Langston, Oct 10, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    748
    Eric Sosman
    Jul 8, 2003
  2. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    624
    Jack Klein
    May 9, 2005
  3. Ioannis Vranos
    Replies:
    11
    Views:
    788
    Ioannis Vranos
    Mar 28, 2008
  4. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    643
    Ben Bacarisse
    Mar 29, 2008
  5. Alex Vinokur
    Replies:
    9
    Views:
    832
    James Kanze
    Oct 13, 2008
Loading...

Share This Page