3

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.

0

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

As your array is null terminated you know you have reached the end of the array when you get a NULL element.

if (args[i] == NULL){
    //DONE
}

If you wanted to get the length of the array args you could loop through it until you get a null, counting the number of iterations:

int length_of_args = 0;
while (args[length_of_args] != NULL){
    length_of_args++;
} 

Someone has posted a similar question Copy argv to string in C(newbie question) with some answers you might find helpful.

1 Comment

sizeof will determine the size of arrays, but only if you actually give it the array. The problem is the OP only has a pointer to the array, not the array itself.
1

You are looking for something like this:

char* concat_string_array(char** input)
{
    int i, len;
    char* result;

    len = 1;
    for (i=0; input[i]; i++)
        len += strlen(input[i]);

    result = malloc(len);
    result[0] = '\0';
    for (i=0; input[i]; i++)
        result = strcat(result, input[i]);

    return result;
}

The key part that I believe you are missing is that the array is terminated by a NULL entry. That's what the test in the for loops checks.

Comments

0

In C99:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char* args[] = { "hello", "my", "world", NULL};
    size_t str_size = 0;
    char** args_itr = args;
/* calculate resulting size */
    while(*args_itr != NULL) {
        str_size += strlen(*args_itr);
        args_itr++;
    }
    char result[str_size+1];
    result[str_size] = '\0'; // protect against 0 size                                                                                                                                          
    args_itr = args;
    char* result_ptr = result;
    while(*args_itr != NULL) {
        strcpy(result_ptr, *args_itr);
        result_ptr += strlen(*args_itr);
        args_itr++;
    }
    /* if you use it in a lib function you could do */
    // return strdup(result);                                                                                                                                                                   

    printf("%s\n", result);
    return 0;
}

Comments

0

You could use sprintf to concatenate 2 strings. You would need to control size of the new string though. Something like that:

int main ()
{
    char* s1 = "abc";
    char *s2 = "def";

    char* snew = (char *)malloc (strlen (s1) + strlen (s2) + 1);

    sprintf (snew, "%s%s", s1, s2);
    printf ("%s\n", snew);

    return EXIT_SUCCESS;
}

2 Comments

In this case you are under control but as a rule one should never ever use sprintf. Use snprintf. It will profit in less bugs by just using this rule. Also, in your code, snew should alloc 1 byte more to leave space for the ending \0 string termination character.
You're right about the second one, I edited my answer. As for the never use sprintf, you're probably right too, it just happens to work. So I thought I'd put it up.

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.