0

I have a short char array called "array". I'm trying to realloc more space to it then add more chars onto the end. for some reason when I print the array, these extra characters don't show up, although they do display when I index them individually.

#include <stdio.h>
#include <stdlib.h>
int main(){
    char *array = malloc(2);
    array[0] = 'b';
    array[1] = '\0';
    char stringToAdd[] = "honey";
    array = realloc(array, (16));
    int pos;
//add stringToAdd into array one char at a time
    for (pos = 0; pos < 5; pos++){
        array[2+pos] = stringToAdd[pos];
        printf("%d ", 2+pos);
        printf("%c ", array[2+pos]);
        printf("%s\n", array);
    }
    array[pos] = '\0';
    int k = sizeof(array);
//should print out the string "bhoney" and its length
    printf("%s, length = %d\n", array,k);
    free(array);
    return 0;
}

output is:

2 h b
3 o b
4 n b
5 e b
6 y b
b, length = 8

also the length of the array seems to be 8 no matter how much space I try to realloc to it?

plotka
  • 63
  • 1
  • 8
  • OT: regarding `char *array = malloc(2);` Always check (!=NULL) the returned value to assure the operation was successful. If not successful, then call `perror( "malloc failed" );` to output to `stderr` your error message "malloc failed" and the text reason the system thinks the error occurred. This is not recoverable, so the next statement should be: `exit( EXIT_FAILURE );` – user3629249 Feb 15 '20 at 17:45
  • OT: regarding: `array = realloc(array, (16));` along with my comment about `malloc()` there is the consideration: The function `realloc()` can fail. When it fails, then a NULL pointer value will overlay the original pointer in `array()` The result is a unrecoverable memory leak. Suggest: `char temp = realloc(array, (16)); if( ! temp ) { // handle error and exit } array = temp;` – user3629249 Feb 15 '20 at 17:49

2 Answers2

4

You added the characters after the null terminator. Printing a string stops at the null.

Assign the new characters to array[1+pos] instead of array[2+pos]. This also goes for adding the new null terminator after the loop, it should be

array[1+pos] = '\0';

You could also use strcat() instead of the loop:

strcat(array, stringToAdd);

It will find the null terminator automatically so you don't have to know the offset, and add the new null terminator properly.

sizeof(array) is the size of the pointer (8 bytes), not the length of the string. If you want the string length, you should use strlen(array). See difference between sizeof and strlen in c

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • wouldn't `strcpy` be better given we already know where the end of the string is? – Christian Gibbons Feb 15 '20 at 00:03
  • My point in suggesting strcat is so that we don't have to keep track of the offset, just let it find it automatically. – Barmar Feb 15 '20 at 00:04
  • Ah, that wasn't clear since you mentioned it as an alternative to looping, which `strcpy` is more of a drop-in replacement for. Or perhaps `strncpy` given the loop as-written is set to stop after 5 iterations. – Christian Gibbons Feb 15 '20 at 00:07
  • Yeah, I added more clarification. The loop is written to stop after 5 iterations because that's how long `honey` is. I don't think he intended this to be any different from the string length, he just hard-coded the length. – Barmar Feb 15 '20 at 00:09
1

It should be:

for (pos = 0; pos < 5; pos++){
    array[1+pos] = stringToAdd[pos];
    printf("%d ", 1+pos);
    printf("%c ", array[1+pos]);
    printf("%s\n", array);
}
array[1+pos] = '\0';
Akshay G Bhardwaj
  • 339
  • 1
  • 4
  • 14