Re: confusion when comparing char * with a string literal
- From: anilnf@xxxxxxxxx
- Date: 17 Mar 2007 12:29:49 -0700
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 of
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
By the way, gcc also reported:
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"
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'..
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... In gcc u won't be able to change a char []Wat's the error in str=strtok(x," ") ? :
declared unless u use --enable options as argument to gcc ...Moreover
itz a warning leave it ...(I think so ..but to err is human)
-Nf
.
- Follow-Ups:
- Re: confusion when comparing char * with a string literal
- From: Flash Gordon
- 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
- confusion when comparing char * with a string literal
- Prev by Date: Re: confusion when comparing char * with a string literal
- Next by Date: Re: Steve Summit C notes , exercise
- 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