Assigning unsigned char*

D

Donos

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?
 
G

Gianni Mariani

Donos said:
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.
 
T

tragomaskhalos

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).
 
J

Jim Langston

Donos said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top