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.

The memory that argv[0] points to is not writable.

I want to hard code the address in the program.

Why don't you just do something like

opt_httphost = "the hard-coded address";

opt_httphost = argv[0] + 7; //+ 7 removes http:// Nope, argv[0] is a pointer to the beginning of the string that hold the name of the program being execute.

Given char *opt_httphost; : opt_httphost = argv[1] + 7; might put you at the beginning of the name address as long as you entered an URL as an argument.

> The original code takes in a url as an argument.
The first argument will be argv[1].
argv[0] is traditionally the program name.

Also, rather than assuming that argv strings can be modified, just make a copy of it in a variable you KNOW you can modify.

It's not like you're fighting for a few bytes here or there.

Sorry, I forgot to mention about the argv. Argv[0] is originally the program name, but after dealing with flags, the program does some magic (that I haven't really tried to figure out because it doesn't really affect the outcome) that ends up with argv[0] as the url.

opt_httphost = "the hard-coded address";

also results in a segfault.

Also, rather than assuming that argv strings can be modified, just make a copy of it in a variable you KNOW you can modify.

The original program modified the argv strings. I'm trying to use another variable and failing.

Sorry, I forgot to mention about the argv. Argv[0] is originally the program name, but after dealing with flags, the program does some magic (that I haven't really tried to figure out because it doesn't really affect the outcome) that ends up with argv[0] as the url.

The original program modified the argv strings. I'm trying to use another variable and failing.

How about getting rid of the original magic altogether? So you'd have untouched argv[] and simply hard-coded opt_httphost = "the hard-coded address"; .

Would an example help?

#include <stdio.h>
#include <stdlib.h> /* for exit() */
#include <string.h> /* for strlen() */

int main ( int argc, char *argv[] ) {
    
    char *tmp;

/* Is there any arguments */
    if ( argc > 0 ) {
        tmp = argv[1];
    }
    else {
        fprintf( stderr, "format: program_name <URL>" );
        exit(0);
    }

/* If less than 11 chars long is not a proper URL */
    if ( strlen( argv[1] ) > 11 ) {
        tmp += 7; /* advance ahead beyond the http:// */
    }
    else {
        fprintf( stderr, "Invalid URL" );
        exit(0);
    }

    printf( "Here's the URL name: %s\n", tmp );
    
    return 0;
}

Would an example help?

if ( argc > 0 ) {
        tmp = argv[1];
    }
}

No, this is what it does now. I want to do

tmp = "http://example.com/asdf";

I don't want to take in an argument. The problem is that it breaks when I try to code it myself.

You might post the relevant parts of the code, otherwise there is no way to help much more.

I thought I had already included most of the relevant code. I'll try a full example. I'm reusing Aia's code (ignoring incorrect input for simplicity).

#include <stdio.h>
#include <stdlib.h> /* for exit() */
#include <string.h> /* for strlen() */

int main ( int argc, char *argv[] ) {
    
    char *url;
    url = argv[1] + 7;
    char *cp = strchr(url,'/');
    *cp++ = '\0';
    
    return 0;
}

If I pass "http://example.com/asdf" to the program, it works fine. The problem is when I try to replace

url = argv[1] + 7;

with

url = "example.com/asdf";

it seg faults.

No, this is what it does now. I want to do

tmp = "http://example.com/asdf";

I don't want to take in an argument. The problem is that it breaks when I try to code it myself.

Possible you are trying to modify a 'read only' variable.
Instead of: char *opt_httphost = "http://example.com/asdf"; which is an unchangeable variable,
use: char opt_httphost[] = "http://example.com/asdf"; so later when you try strchr() to search for the remainder '/' and over-write with '\0' you will not try to access memory that is illegal.

Possible you are trying to modify a 'read only' variable.
Instead of: char *opt_httphost = "http://example.com/asdf"; which is an unchangeable variable,
use: char opt_httphost[] = "http://example.com/asdf"; so later when you try strchr() to search for the remainder '/' and over-write with '\0' you will not try to access memory that is illegal.

This is exactly what I needed. opt_httphost has to be a pointer, though, so I did

char address[] = "http://example.com/asdf";
char *opt_httphost = address + 7;

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.