473,237 Members | 1,402 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,237 software developers and data experts.

How to pass unsigned char** to a function?

Hey there in this program i am passing a unsigned char** from main fuction but stream to which is the argument is not taking value plz help figure out y?



Expand|Select|Wrap|Line Numbers
  1. struct BinaryData* DeFragment::defragment(unsigned char **stream, int nOfPackets)
  2. {
  3.     int i=0,max=0,k=0,ptr=0,low=0,j,t=0,flag=0,size=0,c[3],n=1;
  4.     const int row=nOfPackets;
  5.     unsigned char *tempstr=NULL;
  6.     int **temp=NULL;
  7.     *temp=new int[nOfPackets];
  8. ------------------------------------------------------------------------------
  9.     cout<<"\n\n fucnt disp:  "<<stream[0];
  10.  
  11.                        HERE STREAM ARRAY CONTENTS SHLD BE PRINTED BUT ITS SHOWING GARBAGE VALUES.
  12. -----------------------------------------------------------------------------------
  13. }
  14.  
  15.  
  16. void main()
  17. {
  18. clrscr();
  19.     DeFragment d;
  20.     BinaryData *res;
  21.     const int nOfPackets=2;
  22. ////////////////////////////////////////////////////////////////////////////////////////////
  23.     unsigned char **stream=NULL;
  24.     *stream=new unsigned char[nOfPackets];
  25.        //    stream[0]=new char[184];
  26.  
  27.     stream[0]="0,20,  0,37,  0,1,  0,44,  0,135,  192,168,0,12,  209,111,12,233,  0,0,  65,0,75,45,34,56,4,6,89,120,200,13,55,234,255,77,123";
  28.  stream[1]="0,20,  0,25,  0,2,  0,44,  0,135,  192,168,0,12,  209,111,12,233,  0,0,  6,5,4,3,2";
  29. res=new BinaryData;
  30. cout<<stream[0];
  31. res=d.defragment(stream,nOfPackets);
  32. ///////////////////////////////////////////////////////////////////////////////////////////
  33. if(res==NULL)
  34. cout<<"Error";
  35. else
  36. {
  37.     cout<<res->bytes<<"n\n";
  38.     puts(res->data);
  39. }
  40. getch();
  41. }
  42.  
Please tell me why it is not taking the value and how I can pass the value. Please. Thank you in advance.
Mar 22 '08 #1
8 6417
Hey there in this program i am passing a unsigned char** from main fuction but stream to which is the argument is not taking value plz help figure out y?



Expand|Select|Wrap|Line Numbers
  1. struct BinaryData* DeFragment::defragment(unsigned char **stream, int nOfPackets)
  2. {
  3.     int i=0,max=0,k=0,ptr=0,low=0,j,t=0,flag=0,size=0,c[3],n=1;
  4.     const int row=nOfPackets;
  5.     unsigned char *tempstr=NULL;
  6.     int **temp=NULL;
  7.     *temp=new int[nOfPackets];
  8. ------------------------------------------------------------------------------
  9.     cout<<"\n\n fucnt disp:  "<<stream[0];
  10.  
  11.                        HERE STREAM ARRAY CONTENTS SHLD BE PRINTED BUT ITS SHOWING GARBAGE VALUES.
  12. -----------------------------------------------------------------------------------
  13. }
  14.  
  15.  
  16. void main()
  17. {
  18. clrscr();
  19.     DeFragment d;
  20.     BinaryData *res;
  21.     const int nOfPackets=2;
  22. ////////////////////////////////////////////////////////////////////////////////////////////
  23.     unsigned char **stream=NULL;
  24.     *stream=new unsigned char[nOfPackets];
  25.        //    stream[0]=new char[184];
  26.  
  27.     stream[0]="0,20,  0,37,  0,1,  0,44,  0,135,  192,168,0,12,  209,111,12,233,  0,0,  65,0,75,45,34,56,4,6,89,120,200,13,55,234,255,77,123";
  28.  stream[1]="0,20,  0,25,  0,2,  0,44,  0,135,  192,168,0,12,  209,111,12,233,  0,0,  6,5,4,3,2";
  29. res=new BinaryData;
  30. cout<<stream[0];
  31. res=d.defragment(stream,nOfPackets);
  32. ///////////////////////////////////////////////////////////////////////////////////////////
  33. if(res==NULL)
  34. cout<<"Error";
  35. else
  36. {
  37.     cout<<res->bytes<<"n\n";
  38.     puts(res->data);
  39. }
  40. getch();
  41. }
  42.  
Please tell me why it is not taking the value and how I can pass the value. Please. Thank you in advance.
modify like as follow:

unsigned char **stream=NULL;
stream=new unsigned char *[2];

stream[0]="0,20, 0,37, 0,1, 0,44, 0,135, 192,168,0,12, 209,111,12,233, 0,0, 65,0,75,45,34,56,4,6,89,120,200,13,55,234,255,77,1 23";
stream[1]="0,20, 0,25, 0,2, 0,44, 0,135, 192,168,0,12, 209,111,12,233, 0,0, 6,5,4,3,2";


Because stream is a pointer that point to a char* array.
Mar 23 '08 #2
thanks it worked but 1 more problem after it:-

i am trying to convert it into char* using static cast but cant :-


struct BinaryData* DeFragment::defragment(unsigned char **stream, int nOfPackets)
{

char **stream1=NULL;
stream1=new char*[2];
stream1[0]=static_cast<char*>(stream[0]);

}

getting error -
: error C2440: 'static_cast' : cannot convert from 'unsigned char *' to 'char *'

can tell me y i am getting this error or how can i do it correctly ?
Mar 23 '08 #3
thanks it worked but 1 more problem after it:-

i am trying to convert it into char* using static cast but cant :-


struct BinaryData* DeFragment::defragment(unsigned char **stream, int nOfPackets)
{

char **stream1=NULL;
stream1=new char*[2];
stream1[0]=static_cast<char*>(stream[0]);

}

getting error -
: error C2440: 'static_cast' : cannot convert from 'unsigned char *' to 'char *'

can tell me y i am getting this error or how can i do it correctly ?

You want to transform 'unsigned char * pointer into 'char * pointer, this is not correctly. although all pointer is a 32-bit number ( in 32-bit system), the scope of the data what the pointer point to is different. For example ,the CHAR
type data is from -128~127, but the unsigned char is 0~255. So the compiler
would forbid you to do that . You can only define like as follow:

unsigned char **stream1=NULL;
stream1=new unsigned char *[2];
stream1[0]=stream[0]);
Mar 23 '08 #4
but dude i have to convert it into char* by any how to perform necessary operations.

Plzz tell me how????????????
Mar 23 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
unsigned char **stream=NULL;
stream=new unsigned char *[2];

stream[0]="0,20, 0,37, 0,1, 0,44, 0,135, 192,168,0,12, 209,111,12,233, 0,0, 65,0,75,45,34,56,4,6,89,120,200,13,55,234,255,77,1 23";
stream[1]="0,20, 0,25, 0,2, 0,44, 0,135, 192,168,0,12, 209,111,12,233, 0,0, 6,5,4,3,2";
This is not going to work.

This syntax:
stream[0]="0,20, 0,37, 0,1, 0,44, 0,135, 192,168,0,12, 209,111,12,233, 0,0, 65,0,75,45,34,56,4,6,89,120,200,13,55,234,255,77,1 23";
is initialization syntax that can be use only when the array is defined. However, it is already defined when the stream array was allocated.

You need to:
Expand|Select|Wrap|Line Numbers
  1.    unsigned char data0[]="0,20,  0,37,  0,1,  0,44,  0,135,  192,168,0,12,  209,111,12,233,  0,0,  65,0,75,45,34,56,4,6,89,120,200,13,55,234,255,77,1  23";
  2.    stream[0] = data0;
  3.  unsigned char data1[] ="0,20,  0,25,  0,2,  0,44,  0,135,  192,168,0,12,  209,111,12,233,  0,0,  6,5,4,3,2";
  4.  stream[1] = data1;
  5.  
and then your code will compile without the cast.

And I expect yopur garbage will disappear also.

Remember, you only cast in C++ when a) you are calling a relic C function, or b) when there is a flaw in your C++ design.
Mar 23 '08 #6
This is not going to work.

This syntax:

is initialization syntax that can be use only when the array is defined. However, it is already defined when the stream array was allocated.

You need to:
Expand|Select|Wrap|Line Numbers
  1.    unsigned char data0[]="0,20,  0,37,  0,1,  0,44,  0,135,  192,168,0,12,  209,111,12,233,  0,0,  65,0,75,45,34,56,4,6,89,120,200,13,55,234,255,77,1  23";
  2.    stream[0] = data0;
  3.  unsigned char data1[] ="0,20,  0,25,  0,2,  0,44,  0,135,  192,168,0,12,  209,111,12,233,  0,0,  6,5,4,3,2";
  4.  stream[1] = data1;
  5.  
and then your code will compile without the cast.

And I expect yopur garbage will disappear also.

Remember, you only cast in C++ when a) you are calling a relic C function, or b) when there is a flaw in your C++ design.

This is not going to work? You have try it?



unsigned char **stream=NULL;
stream=new unsigned char *[2];

stream[0]="0,20, 0,37, 0,1, 0,44, 0,135, 192,168,0,12, 209,111,12,233, 0,0, 65,0,75,45,34,56,4,6,89,120,200,13,55,234,255,77,1 23";
stream[1]="0,20, 0,25, 0,2, 0,44, 0,135, 192,168,0,12, 209,111,12,233, 0,0, 6,5,4,3,2";

In my opinion, Define a char * pointer, and let it point to const char array.
This is no problem. unsigned char data1[] that you define is excrescent.
Mar 24 '08 #7
but dude i have to convert it into char* by any how to perform necessary operations.

Plzz tell me how????????????
You want to convert unsigned char * into char *. This is unsafe casting. I don not know why you have to do .
Mar 24 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
This is not going to work? You have try it?
Of course I tried it. However, these are char arrays and the names of these arrays are char*. So, you have to declare an array or char*.

This won't work:
unsigned char **stream=NULL;
*stream=new unsigned char[nOfPackets];
But this will:
Expand|Select|Wrap|Line Numbers
  1.  unsigned char **stream=NULL;
  2.  stream=new unsigned char*[nOfPackets];
  3.  
There is an article that helps understand this: http://www.thescripts.com/forum/thread772412.html.
Mar 24 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: lokb | last post by:
Hi, I have a structure which and defined a smart pointer to the structure. /* Structure of Begin Document Index Record */ typedef struct BDI_Struct{ unsigned char rname; unsigned short int...
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
11
by: HSeganfredo | last post by:
Folks, I want to write a init string function that mallocs an area, fills it with a char, sticks a NUL char in the last position and returns it to the user. So far I noticed that my...
8
by: Steven | last post by:
Hello, everyone! I find a version of strcpy(), I don't know why it return the unsigned char value. Can I change it into return *s1-*s2? int strcmp(const char *s1, const char *s2) { while...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.