I found some open source code written in C that does most of what I want for a project and am having some trouble making the modifications I need. I am hoping the solution will help me better understand pointers and memory allocation as well.
The original code takes in a url as an argument.
opt_httphost = argv[0] + 7; //+ 7 removes http://
I want to hard code the address in the program. Here's where the problem comes.
(paraphrasing)
char *cp;
if((cp = strchr(opt_httphost,'/') != NULL)
{
*cp++ = '/0';
...
}
When I try to hard code opt_httphost, I get a segfault here. I read that argv is an array of character arrays, so pointing to argv[0] + 7 should point to a single character. I've tried making the address a char* and pointing to it, but it segfaults when I try to set a character equal to null. I've also tried to malloc space for the address before dereferencing and setting to null but same deal. What am I doing wrong?
I should note that the whole project is in c++, so this is being compiled with g++ and as such can use c++ libraries in case someone finds a better solution.