#include <stdio.h>
#include <string.h>
int divideAndRefer(char* base, char** num1, char** num2){
char* delimiter = strchr(base, ':');
if (delimiter != NULL){
*delimiter = '\0';
*num1 = base;
*num2 = delimiter + 1;
return 1;
}
return 0;
}
int divideAndCopy(char* base, char* num1, int sizeNum1, char* num2, int sizeNum2){
char* delimiter = strchr(base, ':');
if (delimiter != NULL){
char former = *delimiter;
*delimiter = '\0';
snprintf(num1, sizeNum1, "%s", base);
snprintf(num2, sizeNum2, "%s", delimiter+1);
*delimiter = former;
return 1;
}
return 0;
}
int main(void){
char readLine[8] = "001:330";
char* num1, *num2;
divideAndRefer(readLine, &num1, &num2);
printf("%s %s.\n", num1, num2);
char readLine2[8] = "001:330";
char num3[4], num4[4];
divideAndCopy(readLine2, num3, sizeof(num3), num4, sizeof(num4));
printf("%s %s %s.\n", readLine2, num3, num4);
return 0;
}
First thing, you need 8 bytes to keep the string "001:330", since you need an extra for the '\0'.
I am not a fan of strtok, so here you have two functions that will do what you need. The first one will change the content of the readline, so you won't be able to use it anymore, but it is faster and uses less memory. The second will copy the content to other variables. You should probably study both of them and understand how they work, feel free to ask any part you don't understand.
Good luck.