-2

I've the following code, where.

s[] - generates a char array and

longStr - is a cons char*. I want to combine these 2 into a single const char* such that s should be added first followed by longStr. something like below:

    const char* combinedStr = ADD s[] and then longStr;

The size of longStr can keep changing. Hence, allocating the combinedStr statically wouldn't be a good utilization of memory. Is there a way to d o it dynamically without allocating the size statically for the combinedStr( also without using VLA).

Code

void concatenate(const char* longStr)
{
    time_t t = time(NULL);
    struct tm timeinfo;
    localtime_s(&timeinfo, &t);
    char s[100];
    strftime(s, sizeof(s), "%c", &timeinfo);

   //NOW I WANT TO Combine "s[]" & longStr in such a way that s should be added 1st followed by longStr.

    const char* combinedStr = ADD s[] and then longStr;


}
codeLover
  • 3,720
  • 10
  • 65
  • 121
  • 2
    `strcat()` or `snprintf()` is your friend. beware of the `const`, though. – Sourav Ghosh Sep 14 '17 at 09:40
  • `strcat`? [You need a good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – StoryTeller - Unslander Monica Sep 14 '17 at 09:40
  • Don't describe your code, but show it. – Jabberwocky Sep 14 '17 at 09:42
  • Note that you will need to allocate some more storage for the combined string. You'll need to know how long `longStr` is (remember the null byte). And you should show what you're going to do with the combined string. If you're just going to print it, then you don't need to create it explicitly. Since you don't return it as a function value or via the argument list, that means you must also remember to release the combined string, unless you use a VLA (variable-length array). – Jonathan Leffler Sep 14 '17 at 09:46
  • @JonathanLeffler: the size of longStr can keep changing. Without allocating the size statically for the combinedStr is there a way to d o it dynamically without using VLA. – codeLover Sep 14 '17 at 09:53
  • Use `malloc()` to allocate the right amount of space. And `free()` to release that which was allocated. And you can use `strcpy()` and `strcat()` (or `s[n]printf()`) to copy the two component strings into the allocated result string. – Jonathan Leffler Sep 14 '17 at 09:55

2 Answers2

1

You could use malloc, strcpy and strcat

Something like:

#include <stdio.h>
#include <time.h>

void concatenate(const char* longStr)
{
    time_t t = time(NULL);
    struct tm timeinfo;
    localtime_r(&t, &timeinfo);
    char s[100];
    strftime(s, sizeof(s), "%c", &timeinfo);

    // Allocate memory and put the string together
    const char* p = malloc(strlen(s) + strlen(longStr) + 1); // note: Add 1 for the string termination
    strcpy(p, s);
    strcat(p, longStr);

    printf("%s\n", p);

    free(p);
}

int main(void) {
    char* p = "Hello world";
    concatenate(p);
    return 0;
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

strcpy and strcat, as mentioned in 4386427's answer. You have to be careful of buffer overflows (as in that answer).

Other options are sprintf and (if not using too old a compiler) snprintf.

void concatenate(const char* longStr)
{
    time_t t = time(NULL);
    struct tm timeinfo;
    localtime_s(&timeinfo, &t);
    char s[100];
    strftime(s, sizeof(s), "%c", &timeinfo);

    //NOW I WANT TO Combine "s[]" & longStr in such a way that s should be added 1st followed by longStr.
    // calculate the size:
    //  either this:
    int buflen = snprintf(NULL, 0, "%s%s",s, longstr) + 1;
    // or this:
    int buflen = strlen(s) + strlen(longstr) + 1;

    // allocate:
    const char* combinedStr = malloc(buflen);

    // then either this:
    snprintf(combinedStr, buflen, "%s%s", s, longstr);
    // or this:
    sprintf(combinedStr,  "%s%s", s, longstr);  

    // do what you need to with combinedStr

    free(combinedStr); 

}

Remember to free the memory when you are done with combinedStr. If you pass it out of the function and use it there, then you need to free it outside the function, when you are finished with it.

Basya
  • 1,477
  • 1
  • 12
  • 22