By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

why doesn't this work?

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
See below.

Robert wrote:
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.
url="index.html";
puts (url);

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

Many thanks,
Robert


Nov 13 '05 #2

P: n/a
Robert <R.****@hetnet.nl> wrote in message
news:bj**********@reader11.wxs.nl...
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

Nov 13 '05 #3

P: n/a
Robert <R.****@hetnet.nl> writes:
if (url == "/")


This is in the FAQ.
Nov 13 '05 #4

P: n/a
On Fri, 05 Sep 2003 23:31:27 +0200
Robert <R.****@hetnet.nl> wrote:
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.

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #5

P: n/a
Robert wrote:
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
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.