By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

Assigning unsigned char*

P: n/a
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?

Oct 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
Oct 9 '07 #2

P: n/a
On 9 Oct, 22:04, Donos <dongu...@gmail.comwrote:
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).
Oct 9 '07 #3

P: n/a

"Donos" <do******@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
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.
Oct 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.