468,457 Members | 1,686 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

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 5526
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,207 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,207 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

Post your reply

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

Similar topics

11 posts views Thread by lokb | last post: by
6 posts views Thread by aurgathor | last post: by
5 posts views Thread by Stephen Cawood | last post: by
33 posts views Thread by Michael B Allen | last post: by
11 posts views Thread by HSeganfredo | last post: by
8 posts views Thread by Steven | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.