Jerry Warra Jerry Warra - 1 month ago 7
C Question

C program value changing from 1 to 2 characters

I have inherited a C program and I have very limited C experience.

This is the code that is currently there:

char *ptr ;
if (data == 0) /* Buffer not acquired */
data = (char *)getmem(65535);
ptr = data; /* create data record */
*ptr = '9';


Later in this process it dumps this information to a text file.

I need the data to show double 9's.

char *ptr ;
if (data == 0) /* Buffer not acquired */
data = (char *)getmem(65535);
ptr = data; /* create data record */
*ptr = '99';


When this outputs the info it still only shows one 9. Am I not doing the memory allocation correctly?

Answer

You should not being doing multi-character in a single quotes. If you want double 99, at ptr, you should do:

*ptr = '9';
*(ptr+1) = '9';
Comments