hi guys,

i have declared a char pointer. i am using this in itoa() function to convert integer data into hex data e.g

char * hexData= NULL;
             itoa(1484820,hexData,16);

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

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

hi guys,

i have declared a char pointer. i am using this in itoa() function to convert integer data into hex data e.g

char * hexData= NULL;
             itoa(1484820,hexData,16);

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

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

well, hexData points to unallocated memory ...
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

Alas, you write itoa result to nowhere. Just before itoa call YOU say: my hexData pointer points to NOWHERE (NULL is this NOWHERE).
You must pass a real memory pointer to itoa. Declare a char array or allocate memory chunk by new operator.
Or (better) reread your C or C++ textbook...

This question has already been answered. Start a new discussion instead.