I'm parsing urls for web client in C on linux. I noticed something I don't understand. When taking in the url as a cli arg my code funtions fine, when hard coding it, it fails. I'm new to network C programming.
#include<stdio.h>
#include<string.h>
void parse_url(char* url) {
printf("url: %s\n", url);
char* p = strstr(url, "://");
if(p) {
*p = 0;
p += 3;
}
printf("url body: %s\n", p);
}
int main(int argc, char* argv[]) {
parse_url(argv[1]);
parse_url("http://github.com/");
}
when ran as
./client http://github.com/
the first one behaves as intended giving expected output of
url: http://github.com/
url body: github.com/
second hard coded string fails
url: http://github.com/
Segmentation fault (core dumped)
I am more used to C++ and am C noob so sorry if this is incompetence but I can't find an explanation any where, thank you.