why doesn't this work?

R

Robert

Hi,

I am new to c and use to program in basic before so i was
wondering why this isn't working:

char geturl(char *header, int client)
{
char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");
url = strtok(NULL, " ");
if (url == "/")
url="index.html";
puts (url);

if url = / then it stay's this way
why? what is wrong with this?

Many thanks,
Robert
 
I

Isaac Mushinsky

See below.
Hi,

I am new to c and use to program in basic before so i was
wondering why this isn't working:

char geturl(char *header, int client)
{
char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");
url = strtok(NULL, " ");
if (url == "/")

Your url is a pointer to somewhere whithin header, and " " points to a
constant string somewhere at a different address. You probably want
*url=='/' or !strcmp(url, "/") here.
 
M

Mike Wahler

Robert said:
Hi,

I am new to c and use to program in basic before so i was
wondering why this isn't working:

char geturl(char *header, int client)
{
char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");
url = strtok(NULL, " ");

strtok() returns NULL when no more tokens are found.
You need to check for this.
if (url == "/")

if(*url == '/'); /* (if url == NULL then undefined behavior here */
url="index.html";
puts (url);

if url = / then it stay's this way
why? what is wrong with this?

'url' is a pointer. After the 'strtok()' call
locates a token, 'url' contains an address equal to
or greater than the address contained by the pointer
'header'. The expression "/" signifies a 'string literal',
which occupies its own memory somewhere (we don't know
(or care) where).

Above you were comparing the address of the literal
"/" to the address returned by 'strtok()'. These
will never be equal.

-Mike
 
P

Pieter Droogendijk

Hi,

I am new to c and use to program in basic before so i was
wondering why this isn't working:

char geturl(char *header, int client)
{
char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");

may return NULL, do error checking
url = strtok(NULL, " ");

may return NULL
if (url == "/")

NO!
You're comparing a pointer to the string 'url' to a pointer to the string
literal "/". This won't be true, EVER, as 'url = "/"' will not nessacerily make
'url' point to the same address.

To see if the first character is '/', do this:
if (*url == '/')

If you want to see if the string is equal to "/", do this:
if (strcmp (url, "/") == 0)
url="index.html";
puts (url);

if url = / then it stay's this way
why? what is wrong with this?

Many thanks,
Robert

I can see you're used to basic, by the way you tried to compare two 'strings'
you did. C doesn't have any string functions 'built in', but there are plenty of
string manipulation FUNCTIONS around to do the job.

Also note that C doesn't have a data type for a 'string'. Here you're comparing
two pointers, pointing to some memory, containing a list of characters,
terminated by character 0 - the null terminator, or '\0' to be correct. What you
want is compare the CONTENTS of the memory pointed to, up until the terminator,
and see if they're both equal.

strcmp (string1, string2) will return <0, 0 or >0 if string1 is considered less
than, equal to or greater than string2. This is what you'll want to use.

strncmp (string1, string2, length) will do the same, but only compare 'length'
bytes of both strings at maximum.
 
R

Robert

Robert said:
Hi,

I am new to c and use to program in basic before so i was
wondering why this isn't working:

char geturl(char *header, int client)
{
char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");
url = strtok(NULL, " ");
if (url == "/")
url="index.html";
puts (url);

if url = / then it stay's this way
why? what is wrong with this?

Many thanks,
Robert

Thanks guys, works now
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top