473,404 Members | 2,114 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,404 software developers and data experts.

passing unsigned char[]

hi

got a little prob with data types.
I´m reading data from a socket in to a unsigned char buf[4096]; .
Now, lets say there are 60Bytes written into the array( but it is
always different).
Now i want to give the bytes to a other class like:

pushControlPacket(buf);

but i only want to give the recieved data, not the whole buffer (4096)
icluding the length of the recieved data.

should i cast the recieced data into a std::string and define the
function like this:

pushControlPacket(std::string);

or is there a way to define a function for variable chararrays like
this:

pushControlPacket(unsigned char[]);

it should be "call by value"

thankx

Oct 14 '06 #1
2 3115
bubzilla wrote:
hi

got a little prob with data types.
I´m reading data from a socket in to a unsigned char buf[4096]; .
Now, lets say there are 60Bytes written into the array( but it is
always different).
Now i want to give the bytes to a other class like:

pushControlPacket(buf);

but i only want to give the recieved data, not the whole buffer (4096)
icluding the length of the recieved data.

should i cast the recieced data into a std::string and define the
function like this:

pushControlPacket(std::string);
That is certainly one way. You can also use std::vector<charinstead
of string.

Most of the socket apps I've used I create a special container type but
unless you're pushing a huge amount of data, std::string is fine.
>
or is there a way to define a function for variable chararrays like
this:

pushControlPacket(unsigned char[]);

it should be "call by value"
You can't pass arrays by value, passing an array always is done by
passing a reference or degenerating to the pointer to the first object
in the array.
>
thankx
Oct 14 '06 #2

Gianni Mariani wrote:
bubzilla wrote:
hi

got a little prob with data types.
I´m reading data from a socket in to a unsigned char buf[4096]; .
Now, lets say there are 60Bytes written into the array( but it is
always different).
Now i want to give the bytes to a other class like:

pushControlPacket(buf);

but i only want to give the recieved data, not the whole buffer (4096)
icluding the length of the recieved data.

should i cast the recieced data into a std::string and define the
function like this:

pushControlPacket(std::string);

That is certainly one way. You can also use std::vector<charinstead
of string.

Most of the socket apps I've used I create a special container type but
unless you're pushing a huge amount of data, std::string is fine.
The OP has a buffer of unsigned char. Unless you are happy with the
consequences of converting unsigned char to char[*], std::string and
std::vector<charare not suitable. Use a std::vector<unsigned char>.
[*] which will either be nothing, if char has the same representation
as unsigned char, or will be implementation defined (i.e. not
necessarily portable, and you need to look it up) if char has the same
representation as signed char.

Gavin Deane

Oct 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
5
by: Bucky Pollard | last post by:
I am calling a PVCS DLL function from C#. According to the documentation, I can pass NULL to any output parameter that I do not want to receive data back for. How do I do this? Every thing I've...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
2
by: jeniffer | last post by:
gcc -c test.c gcc -o test test.c ./test i=6 0 14 22 42 48 e6 014224248ffffffe6 ea =
9
by: rob.kirkpatrick | last post by:
Hello I need to populate an array of char arrays at run-time. A very simplifed version of the code is below. char ** list should contain cnt char arrays. The values of char ** list are set by...
0
by: MichK | last post by:
Hi, I have a problem with passing a pointer in visual basic. The thing is I receive an array address of a certain API I call in visual basic. There is no value passed, just an address in the...
11
by: Bob Yang | last post by:
Hi, I have this in C++ and I like to call it from c# to get the value but I fail. it will be good if you can give me some information. I tried it in VB.net it works but I use almost the same way as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.