473,385 Members | 1,766 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,385 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 6451
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.