Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flatten array into string

Tags:

arrays

c

I have an array that is a "NULL-terminated array of NULL-terminated strings". This is given by char **args.

I can access individual elements using args[0], args[1], etc. I wanted to take the entire array and flatten all the elements into a string. If the array contained:

args[0] = "abc"
args[1] = "def"

I want a resulting string to be:

abcdef

I tried to do this by looping through all the elements and concatenating them all together but I do not know how to tell when I have reached the end of the array because sizeof() does not work.

like image 262
Aaron Avatar asked Jan 01 '26 00:01

Aaron


1 Answers

I have an array that is a "NULL-terminated array of NULL-terminated strings".

The array ends with NULL, that is as soon as args[i] == NULL you stop your iteration.

like image 186
K-ballo Avatar answered Jan 02 '26 13:01

K-ballo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!