I'm trying to make an startswith
function which if i had a string consists of
"Hello I am kira"
will split into the first word
"Hello"
Only
and I really tried my best to get it to this form
#include <stdio.h>
unsigned char *startswith(unsigned char *str)
{
char *result;
char *cstr = (char *)str;
for (int i=0; cstr[i] != ' '; i++)
result[i] = cstr[i];
return(result);
}
int main()
{
printf("%s\n",startswith("What is your name momo?"));
return 0;
}
which should print "in my imagination"
What
and an newline
then exit with 0
but i get the unknown holy error when compiling
Segmentation fault
I can't understand why that happens or even locate the problem
gcc
doesn't help or show me warnings
stdio.h
header was just to print the result no more
I know there's a way to achieve this without using any standard libraries but i need leading
Thanks!
A bit deeper explanation of what's happening here... You are getting a segfault because you are dereferencing a pointer to memory your process doesn't own. Your
char* result
pointer is uninitialized, meaning it could have any kind of junk data in it. When you tryresult[i] = cstr[i]
, you are invoking undefined behavior (SO won't let me link their undefined behavior documentation anymore), which simply means that from this point forward execution of the program has unpredictable results. In your case, the UB manifests itself with a segfault, although it may not always.In order to initialize your pointer, you must allocate some space for it using
malloc
or one of its similar functions. You should alsofree
your memory when you are done using it. Your program is so short, in this case it's not a huge deal (the OS will most certainly clean up any memory allocated to the process when it exits), but it's good to get in the habit.