0

I am tring to create a sub-routine that inserts a string into another string. I want to check that the host string is going to have enough capacity to hold all the characters and if not return an error integer. This requires using something like sizeof but that can be called using a pointer. My code is below and I would be very gateful for any help.

#include<stdio.h>
#include<conio.h>
//#include "string.h"

int string_into_string(char* host_string, char* guest_string, int insertion_point);

int main(void) {
    char string_one[21] = "Hello mother";    //12 characters
    char string_two[21] = "dearest ";        //8 characters
    int c;

    c = string_into_string(string_one, string_two, 6);
    printf("Sub-routine string_into_string returned %d and creates the string: %s\n", c, string_one);
    getch();
    return 0;
}

int string_into_string(char* host_string, char* guest_string, int insertion_point) {
    int i, starting_length_of_host_string;
    //check host_string is long enough
    if(strlen(host_string) + strlen(guest_string) >= sizeof(host_string) + 1) {
        //host_string is too short
        sprintf(host_string, "String too short(%d)!", sizeof(host_string));
        return -1;
    }

    starting_length_of_host_string = strlen(host_string);
    for(i = starting_length_of_host_string; i >= insertion_point; i--) {    //make room
         host_string[i + strlen(guest_string)] = host_string[i];
    }
    //i++;
    //host_string[i] = '\0';
    for(i = 1; i <= strlen(guest_string); i++) {    //insert
         host_string[i + insertion_point - 1] = guest_string[i - 1];
    }
    i = strlen(guest_string) + starting_length_of_host_string;
    host_string[i] = '\0';

    return strlen(host_string);
}
1
  • 1
    You have to pass the size of the array to the function, there is no way to find it out inside the function. Aug 13, 2012 at 8:48

3 Answers 3

1

C does not allow you to pass arrays as function arguments, so all arrays of type T[N] decay to pointers of type T*. You must pass the size information manually. However, you can use sizeof at the call site to determine the size of an array:

int string_into_string(char * dst, size_t dstlen, char const * src, size_t srclen, size_t offset, size_t len);

char string_one[21] = "Hello mother";
char string_two[21] = "dearest ";

string_into_string(string_one, sizeof string_one,   // gives 21
                   string_two, strlen(string_two),  // gives 8
                   6, strlen(string_two));

If you are creating dynamic arrays with malloc, you have to store the size information somewhere separately anyway, so this idiom will still fit.

(Beware that sizeof(T[N]) == N * sizeof(T), and I've used the fact that sizeof(char) == 1 to simplify the code.)

6
  • Thanks! I know it's not a very good objection, but it's not so elegant to have to send the size to the function as well. All other aspects of the function are fine with just sending the strings (even strlen). It's a shame there is no function I can use to return turn the capacity. Aug 13, 2012 at 9:09
  • It's not the best but I have decided to use a macro: #define STRING_INTO_STRING( a, b, c) (string_into_string(a, sizeof(a), b, c)). This means that I can write the function call without the sizeof(host_string) and that also avoids errors. Aug 13, 2012 at 12:46
  • 1
    @user1228123 Until the string is declared in main(), then passed by pointer to a function, which in turn calls string_into_string(). It doesn't avoid errors, it creates errors. If you, for reasons unknown, are concerned about passing an extra parameter, then inline the function. However, you really shouldn't be doing weird optimizations like this before you have spotted an actual performance problem, by reading the disassembly or clocking with an oscilloscope. Make sure to enable all compiler optimizations.
    – Lundin
    Aug 13, 2012 at 14:17
  • Using the extra (sizeof) parameter in the function call doesn't provide a full fix though because if I call the function from inside another function, that itself was passed a pointer to the target string, the sizeof function would just return the size of the pointer (4) so not fix the issue. Aug 13, 2012 at 14:50
  • I'm creating a simple find and replace function which takes a string like "The cat sat on the mat" and can, say, replace the first instance of "cat" with "dog". I am trying to acheive this by creating two other functions int string_find_and_remove(char* string, char* string_to_remove) and int string_into_string(char* host_string, char* guest_string, int insertion_point). The main function int string_replace(char* string, char* string_remove, char* string_add) calls these two functions in turn. Aug 13, 2012 at 14:55
0

This code needs a whole lot more error handling but should do what you need without needing any obscure loops. To speed it up, you could also pass the size of the source string as parameter, so the function does not need to calculate it in runtime.

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

signed int string_into_string (char*       dest_buf, 
                               int         dest_size,
                               const char* source_str,
                               int         insert_index)
{
  int   source_str_size;
  char* dest_buf_backup;

  if (insert_index >= dest_size) // sanity check of parameters
  {
    return -1;
  }

  // save data from the original buffer into temporary backup buffer
  dest_buf_backup = malloc (dest_size - insert_index);
  memcpy (dest_buf_backup, 
          &dest_buf[insert_index], 
          dest_size - insert_index);

  source_str_size = strlen(source_str);

  // copy new data into the destination buffer
  strncpy (&dest_buf[insert_index], 
           source_str, 
           source_str_size);

  // restore old data at the end
  strcpy(&dest_buf[insert_index + source_str_size],
         dest_buf_backup);

  // delete temporary buffer
  free(dest_buf_backup);
}


int main()
{
  char string_one[21] = "Hello mother";    //12 characters
  char string_two[21] = "dearest ";        //8 characters

  (void) string_into_string (string_one,
                             sizeof(string_one),
                             string_two,
                             6);

  puts(string_one);

  return 0;
}
2
  • Thanks for this, I see how your code works. I don't mind having the loops as once compiled the code will be doing that anyway. It's for use in an embedded application so I am keen not to use extra memory when I don't have to. Aug 13, 2012 at 9:49
  • @user1228123 You should have mentioned that in the question. Ok then get rid of malloc and replace it with a static buffer of max allowed size. You should not be concerned about the additional function calls: a good compiler will inline when needed. You should be aware of that functions like memcpy() and strcpy() are most often optimized to the extreme by the compiler, they are most likely written in inline assember with optimal performance for your target hardware. Thus they are far more effective than any pre-mature optimization the average user can hope to achieve by themselves.
    – Lundin
    Aug 13, 2012 at 14:11
0

I tried using a macro and changing string_into_string to include the requirement for a size argument, but I still strike out when I call the function from within another function. I tried using the following Macro:

#define   STRING_INTO_STRING( a, b, c) (string_into_string2(a, sizeof(a), b, c))

The other function which causes failure is below. This fails because string has already become the pointer and therefore has size 4:

int string_replace(char* string, char* string_remove, char* string_add) {
    int start_point;
    int c;

    start_point = string_find_and_remove(string, string_remove);
    if(start_point < 0) {
        printf("string not found: %s\n    ABORTING!\n", string_remove);
        while(1);
    }
    c = STRING_INTO_STRING(string, string_add, start_point);
    return c;
}

Looks like this function will have to proceed at risk. looking at strcat it also proceeds at risk, in that it doesn't check that the string you are appending to is large enough to hold its intended contents (perhaps for the very same reason).

Thanks for everyone's help.

1
  • There is no reason why you can't pass the size as parameter to the function.
    – Lundin
    Aug 13, 2012 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.