1

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

12
  • You could try debugging the code -- for example, start by printing out sizeof(array) and lastIndex and seeing if they're taking the values you expect. Commented Dec 20, 2016 at 22:33
  • 3
    sizeof (newArray) that does not do what you want. sizeof on a pointer gives the pointer size not the array size (because it is not actually a C array - pointers and arrays are different in C even though they behave similar in many cases). Commented Dec 20, 2016 at 22:33
  • Possible duplicate of How to find the 'sizeof'(a pointer pointing to an array)? Commented Dec 20, 2016 at 22:35
  • 1
    If all you know is that the last element in the array is NULL, you have to loop through the array until you find NULL, and count the elements. Then you can add 1 to that and multiply by sizeof(char*) Commented Dec 20, 2016 at 22:42
  • 2
    malloc(strlen(string)) is pretty much always wrong. Commented Dec 20, 2016 at 22:45

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

im not sure if its me just having a bad day, the core is still dumping using this code?
Maybe it has to do how you call the function; added an example.
@M.M: Thanks; Seems that I should not post answers late at night :-)
This is C++, not C, the question is about C.

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.