473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

why doesn't this work?

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
5 2449
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
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
Robert <R.****@hetnet.nl> writes:
if (url == "/")


This is in the FAQ.
Nov 13 '05 #4
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
39
by: Mark Johnson | last post by:
It doesn't seem possible. But would the following also seem a violation of the general notions behind css? You have a DIV, say asociated with class, 'topdiv'. Inside of that you have an anchor...
3
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
4
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't...
3
by: Dave Moore | last post by:
Hi All, Ok, here's my problem. I want to open a file and process its contents. However, because it is possible that the file may not exist, I also want to check whether the file() function is...
10
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
6
by: Johnny Jörgensen | last post by:
I've got a usercontrol derived from a normal ComboBox that contains some special formatting code. On my main form I've got a lot of my custom comboboxes. I discovered a bug in the derived...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.