0

I have the following char pointer string "Friday 6 June 11:08:04 2020" and I would like to split it into the following:

date = Friday 6 June

time = 11:08:04

The code I am trying to implement is below:

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

int main()
{
    char *message = "Friday 6 June 11:08:04 2020";
    char time_buff[9] = {0};
    char date_buff[13] = {0};

    for(int d=0; d<13; d++){
        date_buff[d] = message[d];
    }

    for(int t=14; t<23; t++){
        time_buff[t] = message[t];
    }

    for(int p=0; p<13; p++){
        printf("%c ", date_buff[p]);
    }

    return 0;
}

When I run this code I get the following error:

*** stack smashing detected ***: terminated
Aborted (core dumped)

How can I fix this problem and split the string into date and time parts?

Mahmoud Abdel-Rahman
  • 497
  • 2
  • 10
  • 27
  • 6
    `time_buff[t] = message[t]` Index `14` may be valid for `message`, but clearly not for `time_buff`. Try `for(int t=0; t<9; t++){ time_buff[t] = message[t+14]; }` – Gerhardh Mar 28 '23 at 14:07
  • Does this answer your question? [Stack smashing detected](https://stackoverflow.com/questions/1345670/stack-smashing-detected) – matt Mar 28 '23 at 14:09
  • The answer at matt's link explains it in detail, but a quick summary here: the problem is that you're writing to time_buff[14] through 22, and time_buff only goes up to 8. So you instead write to whatever happens to be in the memory *next to* time_buff. One thing that lives in memory around there is the location of the code the function should return to when it's finished, and overwriting that [can be used as a security exploit](https://inst.eecs.berkeley.edu/~cs161/fa08/papers/stack_smashing.pdf). So modern systems have mechanisms to detect when "stack smashing" happens, as it does here. – Ray Mar 28 '23 at 14:18
  • 1
    @matt link is only partially related to the problems OP is having here – 0___________ Mar 28 '23 at 14:20

2 Answers2

4

Some minor tweaks here and there:

  • The correct form of main is int main (void).
  • Good practice: Replace "magic numbers" with constants.
  • The time is actually 8 characters.
  • Good practice: allocate room for null terminators to make these proper C strings.
  • Good practice: Always const qualify pointers to string literals.
  • Calculate a string offset from where to copy.
  • Append null terminator after each copy.
  • (You might as well use memcpy here instead of these loops.)
#include <stdio.h>
#include <string.h>

int main (void)
{
    const char *message = "Friday 6 June 11:08:04 2020";
    const size_t date_size = 13;
    const size_t time_size = 8;
    char date_buff[date_size + 1];
    char time_buff[time_size + 1];
    size_t i;

    for(i=0; i<date_size; i++){
        date_buff[i] = message[i];
    }
    date_buff[i] = '\0';

    size_t time_offset = date_size + 1; // skip the space
    for(i=0; i<time_size; i++){
        time_buff[i] = message[i + time_offset];
    }
    time_buff[i] = '\0';

    puts(date_buff);
    puts(time_buff);

    return 0;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

The method is very naive and it will work only with this particular date - but even here you have 2 major errors

  1. You need the space to accommodate the null terminating character (your char arrays are too small).
  2. You copy to the wrong index (second loop starts from 14 but you want to copy to index starting from zero)
int main(void)
{
    char *message = "Friday 6 June 11:08:04 2020";
    char time_buff[10] = {0};
    char date_buff[14] = {0};

    for(int d=0; d<13; d++){
        date_buff[d] = message[d];
    }

    for(int t=14; t<22; t++){
        time_buff[t - 14] = message[t];
    }

    printf("\"%s\"\n\"%s\"\n", time_buff, date_buff);
    return 0;
}

https://godbolt.org/z/vz34v3h39

0___________
  • 60,014
  • 4
  • 34
  • 74