yasgur99
yasgur99

Reputation: 858

How do i add a string to a string array in c?

I am trying to add a string to an array, but I'm not sure how come this code isn't working. Would anyone please be able to leave me some feedback?

    /* Exercise b: Add <string> to the end of array <array>.
 * Returns: pointer to the array after the string has been added.
 */
char **add_string(char **array, const char *string) {
    /*reallocate so the array of strings can hold one more string*/
    char** newArray = realloc(array, sizeof (array) + sizeof (char*));
    if (!newArray) return array;

    /*allocate memory for new string*/
    char* newString = malloc(strlen(string) * sizeof (char));
    if (!newString) return newArray;

    /*copy old string to new string*/
    int lastIndex = sizeof (newArray) / sizeof (char*);
    strncpy(newString,string,strlen(string));

    /*null terminate array and set old end of array to new string*/
    newArray[lastIndex] = NULL;
    newArray[lastIndex-1] = newString;

    return newArray;
}

When I run the code, the core dumps, but I dont see why

The reason why I am asking is because I am aware that it would be easy to pass in the size of the array. But for this method, I am not allowed. Unfortunatley, my professor does says that the fuction parameters must stay the same

Upvotes: 1

Views: 3030

Answers (1)

Stephan Lechner
Stephan Lechner

Reputation: 35154

There are several things wrong, e.g. sizeof (array) + sizeof (char*), where sizeof(array) is wrong and the + as well (should be *); and all the other comments above also apply.

Wrote a solution and left your code as comments; see the differences.

/* Exercise b: Add <string> to the end of array <array>.
 * Returns: pointer to the array after the string has been added.
 */
char **add_string(char **array, const char *string) {

    int lastIndex = 0;
    while (array[lastIndex] != nullptr)
        lastIndex++;

    /*reallocate so the array of strings can hold one more string; note that lastIndex is zero-based; hence, the size of the current array is lastIndex+1, and consequently the size of the new array needs to be lastIndex+2 */
    // char** newArray = (char**)realloc(array, sizeof (array) + sizeof (char*));
    char** newArray = (char**)realloc(array, (lastIndex+2) * sizeof (char*));
    if (!newArray) return array;

    /*allocate memory for new string*/
    char* newString = strdup(string);
    //char* newString = malloc(strlen(string) * sizeof (char));
    if (!newString) return newArray;

    /*copy old string to new string*/
    //int lastIndex = sizeof (newArray) / sizeof (char*);
    //strncpy(newString,string,strlen(string));


    /*null terminate array and set old end of array to new string*/
    //newArray[lastIndex] = NULL;
    //newArray[lastIndex-1] = newString;

    newArray[lastIndex++] = newString;
    newArray[lastIndex] = nullptr;

    return newArray;
}

void printArray (char** array) {
    char* str; int i=0;
    while ((str = array[i]) != nullptr) {
        std::cout << i << ":" << str << std::endl;
        i++;
    }
}

int main() {

    char** myArray = (char**) malloc(1 * sizeof(char*));
    myArray[0] = nullptr;

    std::cout << "empty:" << std::endl;
    printArray (myArray);

    myArray = add_string(myArray, "Tom");
    std::cout << "one element:" << std::endl;
    printArray (myArray);

    myArray = add_string(myArray, "Jerry");
    myArray = add_string(myArray, "Fin");
    std::cout << "three elements:" << std::endl;
    printArray (myArray);

    return 0;
}

Upvotes: 1

Related Questions