Re: confusion when comparing char * with a string literal



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 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 :-(

Why does it print "today" ? :
char * str means ..u know ..: " that 'str' is a 4 byte pointer
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.

How can u compare arrays ? :
Can't u use 'strcmp': in string.h ... U 'll need to compare
each character seperately to compare two arrays...
Is it necessary to initialize ? :
Yes ... 'char * ' is just a pointer ..It has to point somewhere
before u could access content from it..
Wat's the error in str=strtok(x," ") ? :
Special to gcc...

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


.



Relevant Pages

  • Re: Arrays issue
    ... x.c:158: warning: format argument is not a pointer ... size NAMESIZE) of "char". ... encountered a service with extra aliases, ... It happens to be an object of type "pointer to pointer to char" ...
    (comp.lang.c)
  • Re: confusion when comparing char * with a string literal
    ... char *, and array. ... when I try to comapare 'str' with "today", ... points it to somewhere in memory, or to malloc memory to it before we ... But what ur program does is it makes a pointer variable, ...
    (comp.lang.c)
  • Re: Simple question on Pointers
    ... Why does the latter example give the following warning? ... "pointer to pointer to char". ... The first expression doesn't group like the second. ...
    (microsoft.public.vc.language)
  • Re: warning: return makes integer from pointer without a cast
    ... That silences the warning, but the warning wasn't the problem. ... strdup returns a result of type char*. ... pointer to integer. ... adding a cast to silence a warning is seldom a good ...
    (comp.lang.c)
  • [PATCH 5/5 v4] vsprintf: unify the format decoding layer for its 3 users
    ... Why not make the variable "const char *" and drop the cast? ... Subject: vsprintf: unify the format decoding layer for its 3 users ... if (buf < end) ... char *str, *end, c; ...
    (Linux-Kernel)

Loading