1

I want to create a substring, which I see online everywhere and it makes sense. However, is there any way to, instead of outputting to a regular array of characters, output the substring as a char* array?

This is the idea of my code:

char *str = "ABCDEF";
char *subStr = calloc(3, sizeof(char));
memcpy(subStr, &str[3], 3);
fprintf(log, "Substring: %s", subStr);

I am hoping this will print out DEF. Let me know what you guys think I should do, or if this will work. Thanks!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Ryan237
  • 17
  • 3
  • C strings are "NUL" terminated, so you need to allocate 4 bytes, not 3. `calloc` will initialize the memory to 0 so you just need to `calloc(4, sizeof(char)) ` and then your `memcpy` will work. – Zachary Hamm Apr 25 '21 at 23:53

3 Answers3

2

If you need just to output a substring then you can write

fprintf(log, "Substring: %.*s", 3, str + 3);

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    char *str = "ABCDEF";
    FILE *log = stdout;
    
    fprintf(log, "Substring: %.*s", 3, str + 3);
    
    return 0;
}

The program output is

Substring: DEF
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • This is a really interesting way of doing it, so if I did fprintf(log, "Substring: %.*s", 2, str + 3); would it just print out DE? PS - I can't do this because my program is a lot more complicated and it would probably result in too many errors, but I definitely think this is viable for the future in smaller sections. Also, is there any really solid documentation for the arguments using %? – Ryan237 Apr 25 '21 at 23:13
  • @Ryan237 Yes, it will.:) You can try the demonstrative program I provided in the answer. – Vlad from Moscow Apr 25 '21 at 23:13
  • @Ryan237 See a C Standard Draft and the section that describes fprintf. – Vlad from Moscow Apr 25 '21 at 23:17
2

Your code does not create C substring as you allocate only 3 element char array, but you also need the 4th one for the null terminating character.

char *str = "ABCDEF";
char *subStr = calloc(4, sizeof(char));
memcpy(subStr, &str[3], 3);

or less expensive

char *str = "ABCDEF";
char *subStr = malloc(4);
memcpy(subStr, &str[3], 3);
substr[3] = 0;

You should also check if the result of the allocation was successful,

char *str = "ABCDEF";
char *subStr = calloc(4, sizeof(char));
if(subStr) memcpy(subStr, &str[3], 3);
0___________
  • 60,014
  • 4
  • 34
  • 74
0

You have to terminate the string by adding terminating null-character.

const char *str = "ABCDEF"; /* use const char* for constant string */
char *subStr = calloc(3 + 1, sizeof(char)); /* allocate one more element */
memcpy(subStr, &str[3], 3);
fprintf(log, "Substring: %s", subStr);

calloc() will zero-clear the buffer, so no explicit terminating null-character is written here.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • I don't see it in what you just wrote, but just to be clear I still need to add the terminating null char after I've increased the size by 1, otherwise it'll just be 0 right? So before memcpy I can just say subStr[3] = "\0"; – Ryan237 Apr 25 '21 at 23:04
  • @Ryan237 No, you don't need to manually add terminating null character because `calloc()` sets all bits to zero ([N1570](http://chimera.roma1.infn.it/SP/COMMON/iso-iec-9899-1990.pdf) 7.22.3.2) and null character is represented by a byte with all bits set to zero (N1570 5.2.1). `subStr[3] = "\0";` is bad because it is assigning (an integer converted from) a pointer (converted from the string literal) to `subStr[3]` instead of a NUL. Doing `subStr[3] = '\0';` (use single quote, not double) is OK. – MikeCAT Apr 25 '21 at 23:09