473,498 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2455
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
4857
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
3279
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
25300
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
24958
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
13251
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
3390
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
1804
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
1793
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
1918
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
5823
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
7125
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
7002
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
7165
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,...
1
6885
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...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.