Hello,
The purpose of the program is to print a sub string from the prompt inputs. I do not understand why char pointer does not work but char array will for line 40 and Line 41.
./a.out thisisatest 0 8
substring = "thisisat"
And my code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *substring(size_t start, size_t stop, const char *src, char *dst,
size_t size)
{
unsigned int count = stop - start;
if (count >= --size) {
count = size;
}
sprintf(dst, "%.*s", count, src + start);
return dst;
}
int main(int argc, char **argv)
{
char *text; //Line 40, change to text[100] will work
char *a; //Line 41, change to a[100] will work
int start, end;
if (argc != 4) {
printf("Error! Usage:\n\t \
argv[0]=program;\n\t \
argv[1]=input string\n\t \
argv[2]=start_position of string\n\t \
argv[3]=end_postion of string\n");
return 1;
}
strcpy(text, argv[1]);
start = atoi(argv[2]);
end = atoi(argv[3]);
printf("substring = \"%s\"\n",
substring(start, end, text, a, sizeof(a)));
return 0;
}
Thanks a lot!