Appending char arrays

I have a 2d char array of 10 rows * 10 columns, which I would like to put into a 1d char array. I need to display 10 rows, some of them will have less than 10 characters but not more than 10.

I have tried memcpy and memmove, but as some of the rows have less than 10 characters, it doesn't really work as they only seem to append a string right after the previous string ends, and cannot contain "empty" characters. I'm not sure if there is any way around this. I have also tried strcat, but I need a way of keeping track of where in the new array I am putting stuff in.

Not sure if code is useful in this instance as this is a more general question about how to handle char arrays.

buf[10][11] = {{'\0'}};
tmpbuf[110] = {'\0'};
int i = 0;
int n = 0;

// something is put into buf

while(something) {
memcpy(tmpbuf + i, buf[n], strlen(buf[n]));
i += 11;

I am sure this is a really dumb question...
Copy all 11 chars from each buf to tmpbuff?
for(unsigned i=0; i<110; ++i)
    tmpbuff[i] = buf[i/11][i%11];
Thanks for your reply!

But that only works if each buf actually has 11 chars, doesn't it? The problem is that some bufs will have less than 11 chars, and I suspect there is no way of putting several null chars in an array and then putting chars into it afterwards?
Does it have to be a C Program?

Just that there would be a very different solution in C++ .

You could have a vector of 10 strings of length 10, and you could do anything you like with the strings.

If it has to be C, then you could have an struct which contains 10 char arrays of length 10. You can use strcat to concatenate 2 of these.

Why do you need to keep track of where strings are added?
I forgot to mention you can translate a 2d array into 1d with this formula:

RowNumber * RowLength + ColPosition

You need to have you blank spots filled with spaces say for this to work.

Edit: That's what fun2code's code does.
Last edited on
Thanks guys!

I appear to have made the silly mistake of thinking that a null char and a blank space is the same thing... Won't be doing that again!
Topic archived. No new replies allowed.