Re: confusion when comparing char * with a string literal
- From: "william" <william.maji@xxxxxxxxx>
- Date: 19 Mar 2007 09:02:40 -0700
On Mar 17, 6:06 pm, Flash Gordon <s...@xxxxxxxxxxxxxxxxxx> wrote:
ani...@xxxxxxxxx wrote, On 17/03/07 19:29:
On Mar 17, 10:25 pm, "william" <william.m...@xxxxxxxxx> wrote:
On Mar 17, 1:22 pm, "william" <william.m...@xxxxxxxxx> wrote:
below is a short piece of code I wrote to testify my understanding ofBy the way, gcc also reported:
char *, and array.
#include <stdio.h>
int main()
{
char *str=NULL;
char x[]="today is good!";
printf("%s", str);
str=strtok(x," ");
if (str=="today") //<==here is line that confuses me
printf("they equals!\n");
return 0;
}
I printed "str" first, and the console displayed "today". However,
when I try to comapare 'str' with "today", the condition failed!
Exactly speaking, I know that 'str' is a 4 byte pointer of char type,
so it is not equal to a string. But even in the gdb, I used 'p str',
it printed "today".
So, my question is how do we compare arrays and char *(I found that it
is so commonly used as string)?
one more question: is it true that we have to initialize a char * or
points it to somewhere in memory, or to malloc memory to it before we
can use it?
Because I got segfault several times arising from this problem too.
Thank you for your reply in advance!
Ji
warning: assignment makes pointer from integer without a cast
on the line: str=strtok(x," ");
where I think the type is compatible :-(
char * str means ..u know ..: " that 'str' is a 4 byte pointerWhy does it print "today" ? :
of char type"
No it does not. It means str is a pointer to char, there is nothing to
suggest how large the pointer is.
Also, please don't use contractions like u and ur, it makes reading your
post difficult, and more people read each post that write it.
But what ur program does is it makes a pointer variable, to which u
havn't given any memory address..so if u try to access the memory
location pointed to by ur pointer ,it will access some random
memory .. Here itz the place were u hav stored the array "today " ..If
it accesses some memory location outside the limits alloted for ur
program ,ur OS will hook it by saying 'segfault'..
Or it might not. It could do anything.
Can't u use 'strcmp': in string.h ... U 'll need to compareHow can u compare arrays ? :
each character seperately to compare two arrays...
Yes ... 'char * ' is just a pointer ..It has to point somewhereIs it necessary to initialize ? :
before u could access content from it..
Special to gcc...Wat's the error in str=strtok(x," ") ? :
No it is not.
> In gcc u won't be able to change a char []
Complete rubbish. A char array is modifiable unless declared as const,
string literals are a completely different matter (you are not allowed
to modify them) but since the OP did NOT try to modify a string literal
they are not relevant.
declared unless u use --enable options as argument to gcc ...Moreover
itz a warning leave it ...(I think so ..but to err is human)
Incredibly stupid advice. The warning was due to a very serious error,
one whic *will* cause the program to fail on some modern C
implementations. Namely failing to provide a prototype for strtok, and
the best solution to this is including the standard header that provides it.
Ignoring warnings is NEVER a good idea. Very occasionally it makes sense
to leave in a warning, but only when you have enough experience to know
*why* you are write in that specific situation.
--
Thank you for this piece of advice: "DO NOT ingore warnings"
Flash Gordon
.
- Follow-Ups:
- Re: confusion when comparing char * with a string literal
- From: CBFalconer
- Re: confusion when comparing char * with a string literal
- References:
- confusion when comparing char * with a string literal
- From: william
- Re: confusion when comparing char * with a string literal
- From: william
- Re: confusion when comparing char * with a string literal
- From: anilnf
- Re: confusion when comparing char * with a string literal
- From: Flash Gordon
- confusion when comparing char * with a string literal
- Prev by Date: Re: qsort descending order
- Next by Date: Re: confusion when comparing char * with a string literal
- Previous by thread: Re: confusion when comparing char * with a string literal
- Next by thread: Re: confusion when comparing char * with a string literal
- Index(es):
Relevant Pages
|
Loading