0

I have a character pointer , char *buf; I have a array of integers , int console_buffer[256]; I need to copy the console_buffer contents to character buf. How do I do this? The buf and console_buffer are part of different structures.

3
  • 5
    This question is ill-posed. How do you want to convert between int and char? Commented Nov 19, 2011 at 16:20
  • I will tell you why I need this. I am trying to implement read system call. The console_buffer is declared as integer array since when EOF is encountered it returns -1, so I know I have to stop here. Actually console_buffer will contain characters. Finally after console_biffer is read, I need to copy it to my Process control block structure which has a char * buf member. Commented Nov 19, 2011 at 16:29
  • You can do this with a simple loop. What have you tried so far? Commented Nov 19, 2011 at 16:33

3 Answers 3

1

Going by your comment,

buf = malloc(256); // 257 if console_buffer may be full without EOF
/* if you want to allocate just as much space as needed, locate the EOF in console_buffer first */
for(int i = 0; i < 256 && console_buffer[i] != -1; ++i){
    buf[i] = (char)console_buffer[i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. My question is since I am storing characters in console_buffer which would be stored as ascii values, will (char)console_buffer[i] convert them back to characters? or do I need to handle this in different way?
Yes, casting to char does exactly what you want, since what you stored in console_buffer is in the range of char. There's a possible problem if char is signed, though. The getc family returns unsigned chars cast to int, these may be out of range for signed char, then the conversion is not guaranteed (but it almost certainly does the right thing).
0

If you already allocated the memory for buf, and if each integer is between 0 and 9, you can do:

for(int i = 0; i < 256; i++)
{
    buf[i] = '0' + console_buffer[i]; /* convert 1 to '1', etc. */
}

If the integers are larger than 9, you can use the sprintf function.


Reading your new comment, perhaps you can also achieve your goal by reading from console buffer directly to an array of chars until you have -1 (check by integers comparison, or by strcmp, or by comparing the 2 last characters to 0 and to 1).

2 Comments

Will the following code work? for(i=0; i< 256; i++){ pcb -> buf = (char*) console_buffer(i); } Pardon me if I sound silly , I have just started using pointers, so not yet comfortable with it.
@Igor Oks: I dont understand. In my console_buffer integer array characters are going to be stored like 'C','I' ... ( I guess the ascii values will be stored am I right? ) and once all is read, -1 integer will be stored at the end.
0

I think this is a better way to convert values to chars

int i = 0;
while (i <= 256) {
    buf[i] = (char) console_buffer[i];
    i++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.