Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

Please be aware that I am a C++ newbie!

I need to call a function where I pass in a variable declared as unsigned char*, this variable needs to be initialized as 5 bytes of 17, how would I do this?

Also this function passes back a variable declared as as unsigned char*, it has to hold 5 bytes, how would I do this?

Thank you

What I have tried:

I tried googling but could not fimd anything applicable to my case.
Posted
Updated 10-Dec-19 21:38pm
Comments
CPallini 11-Dec-19 3:09am    
"Also this function passes back a variable declared as as unsigned char*, it has to hold 5 bytes, how would I do this?
This part, as noted by our Griff, is tricky. Could you please provide more details about such function?

You could start at C++ Language Reference | Microsoft Docs[^].

However, rather basic code but ...
C++
unsigned char uchars[5] = { 17, 17, 17, 17, 17 };
unsigned char* uresult = thefunction(uchars);
 
Share this answer
 
Comments
CPallini 11-Dec-19 3:07am    
5.
Just to add to Richard's solution, a couple of bits for you to think about.
1) The specification says that the name of an array is a pointer t the first element. SO when Richard does this:
unsigned char uchars[5] = { 17, 17, 17, 17, 17 };
thefunction(uchars);
he passed the name of the array into the function as a pointer to the first element. He could have written it as
unsigned char uchars[5] = { 17, 17, 17, 17, 17 };
thefunction(&(uchars[0]));
and explicitly pass the pointer, but it isn't needed.
2) You need to be careful when returning pointers from functions: if the array you return is not the same array in memory as is passed in - and it probably shouldn't be - then you have to be sure that you don't declare teh array directly inside your function as it will be created on the stack and the memory "recycled" when the array exits. This can cause some really nasty bugs, and is called a "hanging reference". To allocate heap memory - which you can return from your function, but which will need correct disposal to prevent "memory leaks" from your app - use either malloc or the new keyword.
 
Share this answer
 
Comments
CPallini 11-Dec-19 3:07am    
5.
You must learn the language to understand its power and its pitfalls. Always remember C++ is a sledgehammer: very powerful but it can harm a lot when misused.

One common problem is understand data types and pointers. A data type or object is this piece in the memory. The pointer (aka with "*") is only the address to it.

Example
C++
unsigned char myVar = 'a';
unsigned char *pToVar = 0;
pToVar = &myVar;//assign pointer to point to myVar
This is an outstanding C++ tutorial which also introduces the Microsoft Visual Studio and its powerful debugger. Learning from this tutorial is very well invested time.
 
Share this answer
 
Comments
Stefan_Lang 11-Dec-19 7:46am    
"Always remember C++ is a sledgehammer"
I beg to disagree: C++ is a very large toolbox containing great tools for all kinds of ptojects. You just have to be careful not to stick to the first tool you find for all projects: there are times when you need a sledgehammer, but sometimes you'll want a toothpick. Better make sure you're using the right tool at the right time! ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900