why cant you assign a char* to ctime()? 
Author Message
 why cant you assign a char* to ctime()?

from my file...

char now[1000];
time_t ticks;

now = ctime(&ticks);

when i compile this code, the compiler complains about "incompatible types
in assignment", despite ctime def being

char *ctime(const time_t *tp).

of course, strcpy worked, but why doesn't the assignment work??

thanks,

salim



Sun, 25 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?


Quote:
> from my file...

> char now[1000];
> time_t ticks;

> now = ctime(&ticks);

You can't change the value of 'now' since it is pointing to a statically
allocated array.
[cut]

Using the 'ctime_r' function, you could have written "ctime_r(&ticks,
now);" I guess...

/A

--
+---------------------+
| Andreas K?h?ri
| Uppsala University
| Sweden

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 25 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?

Quote:

> from my file...

> char now[1000];
> time_t ticks;

> now = ctime(&ticks);

Try:

char *now;

..matthew



Sun, 25 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?
char now[1000] is an array, not a pointer.
it under some circumstances decays into an a char pointer.

You can't assign a pointer to an array. ie.
        now = ctime(&ticks);

you can however declare a now as a char * and assign that to the
output of pointer of ctime.

char *now;
now=ctime(&ticks);

This doesn't copy the data, this just points now at the static or
global character array that exists in the runtime that ctime is
returing a pointer to.  If your planning on doing this it's probably
be a good idea to use a const char *.

You should probably copy the data out of ctime (as you mentioned) into
the now array that you've defined.

char now[1000];

strncpy(now,ctime(&sticks),999);



Quote:
>from my file...

>char now[1000];
>time_t ticks;

>now = ctime(&ticks);

>when i compile this code, the compiler complains about "incompatible types
>in assignment", despite ctime def being

>char *ctime(const time_t *tp).

>of course, strcpy worked, but why doesn't the assignment work??

>thanks,

>salim



Sun, 25 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?

Quote:

> char now[1000];
> time_t ticks;
> now = ctime(&ticks);
> when i compile this code, the compiler complains about "incompatible types
> in assignment", despite ctime def being
> char *ctime(const time_t *tp).
> of course, strcpy worked, but why doesn't the assignment work??

   Because you can't assign an array to a pointer.  What you
should do is:

char *now;
time_t ticks;

now = ctime(&ticks);




Sun, 25 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?

Quote:

> from my file...

> char now[1000];
> time_t ticks;

> now = ctime(&ticks);

> when i compile this code, the compiler complains about "incompatible types
> in assignment", despite ctime def being

> char *ctime(const time_t *tp).

> of course, strcpy worked, but why doesn't the assignment work??

> thanks,

> salim

as all the others already mentioned you can't assign a char * to an
array.
But what is the problem? From my mind:

"char arr[]" is an equvivalent to "char const * arr"

this means that an array is a constant pointer, but the contents is not
constant at all!

ctime returns a pointer, so the assignment tries to change a const
pointer
which is not allowed!!

Z



Mon, 26 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?


Quote:
>... means that an array is a constant pointer ...

An array is not a pointer; a pointer is not an array.  An array is
an array and a pointer is a pointer.  If they *were* the same, why
would we need two separate terms?

There are several reasons for the confusion people have over when
arrays and pointers act alike (and they *do* act alike, often).  These
are all covered quite well in the comp.lang.c FAQ.  Read it as many
times as necessary. :-)
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Mon, 26 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?


[snip]

Quote:
>as all the others already mentioned you can't assign a char * to an
>array.

Correct.

Quote:
>But what is the problem?

They're incompatible.

Quote:
>From my mind:

>"char arr[]" is an equvivalent to "char const * arr"

Your mind I'm afraid is mistaken - these two are not equivalent.

Quote:
>this means that an array is a constant pointer, but the contents is not
>constant at all!

No, an array is not a constant pointer.  An array is an array.  Apart
from anything else an array has a size - a number of elements.  A pointer
does not.

HTH
John
--
John Winters.  Wallingford, Oxon, England.

The Linux Emporium - the source for Linux CDs in the UK
See http://www.linuxemporium.co.uk/



Mon, 26 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?

Quote:

> as all the others already mentioned you can't assign a char * to an
> array.

   The other way around.  You can't assign a char[] to a char *.

Quote:
> But what is the problem? From my mind:
> "char arr[]" is an equvivalent to "char const * arr"
> this means that an array is a constant pointer, but the contents is not
> constant at all!

   Who told you char arr[] is equivalent to char const *arr?
Tell this person they were wrong.  One is an array and the other
is a constant pointer.  There is a difference, and a pretty big
difference it is.




Mon, 26 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?


Quote:
>    Who told you char arr[] is equivalent to char const *arr?

Brian W.Kernighan, Dennis M.Richie
The C Programming Language
1977, Prentice Hall, Inc.

Regards
Horst



Tue, 27 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?

Quote:

> Brian W.Kernighan, Dennis M.Richie
> The C Programming Language
> 1977, Prentice Hall, Inc.

   For the first minute or so, I was thinking "huh?"

   I misunderstood the post and thought that it was with regards
to assignment as in the original post:

char const *foo;
char bar[] = foo;

   and more specifically,

char bar[] = ctime(xxx);

   Sorry for the mix-up.




Tue, 27 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?


Quote:

> > Brian W.Kernighan, Dennis M.Richie
> > The C Programming Language
> > 1977, Prentice Hall, Inc.

>    For the first minute or so, I was thinking "huh?"

>    I misunderstood the post and thought that it was with regards
> to assignment as in the original post:

> char const *foo;
> char bar[] = foo;

>    and more specifically,

> char bar[] = ctime(xxx);

>    Sorry for the mix-up.

There were more mixups. A reader claimed that char arr[] was
equivalent to char const *arr (pointer to const char), meaning to say
that it was equivalent to char* const arr (const pointer to char).

You asked

Quote:
>    Who told you char arr[] is equivalent to char const *arr?

and I answered the question

     Who told you char arr[] is equivalent to char * const arr?

by pointing to the above mentioned book. Due to two sentences from the
chapter "pointers and arrays"

  ...therefore an array name _is_ a pointer expression

and

  ...but an array name is a constant, not a variable. Expressions
     like ... p = &a  are not allowed...

(retranslated to english from a translation)

many people believe and teach (!) still nowadays that an array _is_ a
constant pointer.

Regards
Horst



Tue, 27 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?


Quote:


>>    Who told you char arr[] is equivalent to char const *arr?

> Brian W.Kernighan, Dennis M.Richie
> The C Programming Language
> 1977, Prentice Hall, Inc.

Highly unlikely, as that edition doesn't mention `const` at all (it
not being in the language at the time).

Page 94 of the (C) 1978 edition to my left says:

    "But an array name is a *constant*, not a variable; ..."

which (in K&R C terms) is why you can't assign to an array. There
is no notion in K&R C of a general `const` qualifier. Page 95
says:

    As formal parameters in a function definition,
        char s[];
    and
        char *s;
    are exactly equivalent.

Note the qualifier in the first sentence; it's important.

So these most-plausible pages do *not* allow us to conclude that

    "char arr[] is equivalent to char const *arr"

--
Chris "knew there was a *reason* for keeping the book" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Tue, 27 Aug 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?


Quote:



>> > Brian W.Kernighan, Dennis M.Richie
>> > The C Programming Language
>> > 1977, Prentice Hall, Inc.

>>    For the first minute or so, I was thinking "huh?"

>>    I misunderstood the post and thought that it was with regards
>> to assignment as in the original post:

>> char const *foo;
>> char bar[] = foo;

>>    and more specifically,

>> char bar[] = ctime(xxx);

>>    Sorry for the mix-up.

>There were more mixups. A reader claimed that char arr[] was
>equivalent to char const *arr (pointer to const char), meaning to say
>that it was equivalent to char* const arr (const pointer to char).

Which is false.

Quote:
>You asked

>>    Who told you char arr[] is equivalent to char const *arr?

>and I answered the question

>     Who told you char arr[] is equivalent to char * const arr?

>by pointing to the above mentioned book.

However that book does not say or imply this.

Quote:
>Due to two sentences from the
>chapter "pointers and arrays"

>  ...therefore an array name _is_ a pointer expression

A pointer expression perhaps (at least as far as K&R C is concerned), a
designator for a pointer object, no. I.e. is is not a pointer lvalue.

Quote:
>and

>  ...but an array name is a constant, not a variable. Expressions
>     like ... p = &a  are not allowed...

I wonder about sizeof(a). Constant here means in the same sense that
42 is a constant, it has nothing to do with const.

Quote:
>(retranslated to english from a translation)

>many people believe and teach (!) still nowadays that an array _is_ a
>constant pointer.

It can be used as a pointer value but not a pointer lvalue. Values simply
are, they aren't modifiable because they don't consist of anything you
can write to. All you can do is use them as operands of an operator which
either derives a new value, produces a side-effect or ignores the value.

--
-----------------------------------------


-----------------------------------------



Sun, 01 Sep 2002 03:00:00 GMT  
 why cant you assign a char* to ctime()?

Quote:

> >You asked

> >>    Who told you char arr[] is equivalent to char const *arr?

> >and I answered the question

> >     Who told you char arr[] is equivalent to char * const arr?

> >by pointing to the above mentioned book.
> However that book does not say or imply this.

Correct. But "say" and "imply" don't mean the same to everybody ;-)

Quote:
> >Due to two sentences from the
> >chapter "pointers and arrays"

> >  ...therefore an array name _is_ a pointer expression
> A pointer expression perhaps (at least as far as K&R C is concerned), a
> designator for a pointer object, no. I.e. is is not a pointer lvalue.
> >and

> >  ...but an array name is a constant, not a variable. Expressions
> >     like ... p = &a  are not allowed...
> I wonder about sizeof(a). Constant here means in the same sense that
> 42 is a constant, it has nothing to do with const.

sizeof (a) just works by miracle ;-)

Of course the interpretation problem is buried in the word "constant".
Using another terminolgy we might say that it "is" - behaves like - an
rvalue (pointer value) which is constant in two senses: you can't
assign anything to it and its value doesn't change during the array's
lifetime.

Regards
Horst



Tue, 03 Sep 2002 03:00:00 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. problem assigning char* to char*

2. Cant install the VC - Samples?WHY!?

3. Cant see why malloc() req'd.

4. Why cant I Catch Messages from CSliderCtrl?

5. Why the message box cant be popped up

6. Why cant I see every messasge in PreTranslateMessage?

7. Why cant CStatic respond to MOUSE_MOVE event?

8. Why cant I get EM_SETSEL messages?

9. hi why i cant use import namespace

10. assigning to a char array

11. Assigning char* to int* and vice-versa???

12. assigning the value of a char to a string (newbie)

 

 
Powered by phpBB® Forum Software