How do I append a string to an array of strings within a loop in C?

amroman

I'm struggling to see how I can append a string to an array of strings, given that the value being added to each time (therefore the value the pointer points to is changing). I'm expecting array to be in the format {"hello, "helloo", ...} but I can't get this to work. What changes can I make so that array stores this correctly?

int size = 10;
char *string = "hello";
char c = "o";
char *array[size];

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    array[i] = string;
}
Serge Ballesta

Oops... there are tons of errors here!

Let us go back to the basics: C language has no notion of what a string could be. At the language level you only have the (single) character type: char. At the library level, a string is representented by a null terminated character array.

So to be able to add a character to a string, you must have an array of the correct dimension. BTW, a litteral like "hello" is a const character array: the programmer cannot change anything to it.

So here you want to be able to add 1 to 10 character to "hello", so you need a 16 character array: 5 for hello, 10 for the additional o and 1 for the null. But that is not all, array is just an array of pointers. If all elements of array point to the same string to which you add characters, at the end they will all have the very same value! You must have each element of the array to point to a different character array. Assuming that strdup is available you could write:

int size = 10;
char string[16] = "hello";  // build an array of size 16 initialized to "hello"
const char *c = "o";        // beware a string literal IS const
char *array[size];

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    array[i] = strdup(string);  // build a dynamic copy
}

When you will no longer need array you should free all strings allocated with strdup:

for (int i = 0; i < 10; i++) {
    free(array[i]);
}

BTW, strdup is only in the standard C library since C23, it previously was just a POSIX extension. If is in not available on your system you can roll you own:

char *strdup(const char *src) {
    char *dest = malloc(1 + strlen(src));
    strcpy(dest, src);
    return dest;
}

Alternatively you could avoid dynamic allocation by using a true 2D array:

int size = 10;
char string[16] = "hello";  // build an array of size 16 initialized to "hello"
const char *c = "o";        // beware a string literal IS const
char *array[size][16];      // this is an array of 10 strings of size 16

for (int i = 0; i < 10; i++) {
    strcat(string, c);
    strcpy(array[i], string);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to append strings to a string in a loop?

How do I reverse a string within a for loop?

How do I append a value to an array within an array in PHP?

C How do I print the size of a string in array of strings

How do I append same string variable to a string in a loop

How do I convert an array of strings into append-able list?

How do I reference the string in a array of strings?

How do I read a string enclosed within square braces in a file in C with each line containing several such strings

How do I convert a Go array of strings to a C array of strings?

In Typescript, how do I convert a string of strings as an array of strings?

How do I create a while loop within a PHP string?

How do I remove characters from a mutable string within a loop?

How do I structure an array within an .each loop using jQuery?

How do I call an array within an IIFE using a forEach loop?

How to append String at particular index in an array of Strings

How do I append to a string?

How to append an array within a for loop that gets new outputs every loop?

How can I append an array with result of each iteration within a for-loop in python?

How do I validate string within an array to a method in java

How do I add a string array within a string array array in a string list?

How do you search for strings within a string?

How do you loop an array and append it to a div?

How do I get a function to accept a String or an Array of Strings?

How do I check if the strings inside an array are part of another string?

How do I append to a list when the name of the list lies within a string?

How to append randomized float values into array within loop

How do I update an array of strings within an array of objects using ObjectId in mongodb?

How do I append data to the list in loop?

How do I use a for loop to append to a UITextView?