| | |
How to preserve the value contains by char * pointer
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 38
Reputation:
Solved Threads: 0
hi guys,
i have declared a char pointer. i am using this in itoa() function to convert integer data into hex data e.g
this results in equalent of 1484820 in hex decimal and store it in hexData.
now when I want to some process in hexData(char pointer). when I pass it to my function for further process, the value of hexData(char pointer is changed now contains garbage) see the code below
I am confused whats problem going on here.
can any tell me its solution?
Asif
i have declared a char pointer. i am using this in itoa() function to convert integer data into hex data e.g
C++ Syntax (Toggle Plain Text)
char * hexData= NULL; itoa(1484820,hexData,16);
now when I want to some process in hexData(char pointer). when I pass it to my function for further process, the value of hexData(char pointer is changed now contains garbage) see the code below
C++ Syntax (Toggle Plain Text)
main() { char * hexData=NULL; itoa(148420,hexData,16); printf("%s",hexData);---------------------//output is 243C4 writeinFile(hexData,fptr); } writeinFile(char* str, FILE *fptr) { printf("%s",hexData);---------------------//output is 2434C but some time a garbage value char *hex = new char[30]; printf("%s",hexData);---------------------//output is always garbage value after any new //char *pointer declaration. ---- ---- ---- } }
I am confused whats problem going on here.
can any tell me its solution?
Asif
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
hi guys,
i have declared a char pointer. i am using this in itoa() function to convert integer data into hex data e.g
this results in equalent of 1484820 in hex decimal and store it in hexData.C++ Syntax (Toggle Plain Text)
char * hexData= NULL; itoa(1484820,hexData,16);
now when I want to some process in hexData(char pointer). when I pass it to my function for further process, the value of hexData(char pointer is changed now contains garbage) see the code below
C++ Syntax (Toggle Plain Text)
main() { char * hexData=NULL; itoa(148420,hexData,16); printf("%s",hexData);---------------------//output is 243C4 writeinFile(hexData,fptr); } writeinFile(char* str, FILE *fptr) { printf("%s",hexData);---------------------//output is 2434C but some time a garbage value char *hex = new char[30]; printf("%s",hexData);---------------------//output is always garbage value after any new //char *pointer declaration. ---- ---- ---- } }
I am confused whats problem going on here.
can any tell me its solution?
Asif
use:
char hexData[BUFFSIZE];
or
char * hexData = (char*)malloc( BUFFSIZE* sizeof( char));
u get garbage because u use unallocated memory that can be overwritten at any time
![]() |
Similar Threads
- Pointer error (C++)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
- RE: Leaked Windows Source Code (IT Professionals' Lounge)
Other Threads in the C++ Forum
- Previous Thread: c++ convert numbers to words
- Next Thread: storing data into array
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






