Re: confusion when comparing char * with a string literal



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"
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'..
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... In gcc u won't be able to change a char []
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

.



Relevant Pages

  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: Simple code leads to segfault.. help
    ... non-const char. ... A pointer variable is just like any other variable -- it consumes memory ... The purpose of a double is to store a floating-point number. ... The purpose of a pointer is to store a memory address. ...
    (comp.lang.c)
  • Re: Simple code leads to segfault.. help
    ... They are a non-const pointer to a non-const char. ... A pointer variable is just like any other variable -- it consumes memory and stores data by arranging bits in a particular pattern. ... The purpose of a double is to store a floating-point number. ...
    (comp.lang.c)
  • Re: Mysterious pointer mixed with array indices bug
    ... Converting that `char' to a `void*' is suspect, ... variable `pnew_res_names' points to some memory ... this call prints random garbage (if it prints ... some of the levels of pointer indirection -- I am usually averse ...
    (comp.lang.c)
  • Re: confusion when comparing char * with a string literal
    ... char *, and array. ... Include string.h to get the prototype for strtok. ... printf("%s", str); ... You're passing a null pointer to printf and telling it to follow ...
    (comp.lang.c)

Loading