473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Assigning unsigned char*

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
3 6728
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ben | last post by:
Hi all, I would like to know if there is an easy way to assign a string to an int. I have a struct such as: struct Values { int a; int b; }
13
by: - Kees van der Bent - | last post by:
/* With the following: */ typedef struct { unsigned char a : 1; unsigned char b : 1; } sss_t; sss_t sss; unsigned char ppp; main()
3
by: yogi | last post by:
Hi guys, I'm trying to write a program that will read in a series of files and create a 3D array from the files read in for converting 2D images to 3D objects. The values read in will be...
9
by: Noel Milton | last post by:
Hi: Ok, I've just read in up to 65,535 bytes into a char array (using the recvfrom socket API call). So I have an array of 8 bit char's (char recvString;). Now, four (4) consecutive bytes...
17
by: Cliff | last post by:
Hi, I'm in the process of porting some code from a 3rd party and have hit a problem with the following: typedef struct { unsigned char Red; unsigned char Green; unsigned char Blue;...
2
by: Donos | last post by:
Hello I have the following program, class CMyValues { public: void getValue(unsigned char* pVal) {
7
by: Donos | last post by:
Hello I posted this earlier,but some how it's still not working for me. So i thought of giving a better description this time. The following is the code that am using, class CMyValue {...
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.