| Author |
question on passed values and pointer assignments
|
|
|
| Why can i do :
char *ptr;
ptr="test";
should it not be malloced ?
and why can i pass a string literal to a function(char *string)
does string not need to be malloced as well ?
or is this something the compiler takes care of?
| |
| Pascal Bourguignon 2006-12-18, 7:22 pm |
| atv <alef@xs4all.nl> writes:
> Why can i do :
>
> char *ptr;
>
> ptr="test";
Because ptr is of type char* and "test" is of type char*.
> should it not be malloced ?
Why should it?
> and why can i pass a string literal to a function(char *string)
Why not?
> does string not need to be malloced as well ?
Why should it?
> or is this something the compiler takes care of?
The compiler just takes care that you give a pointer to some character
when the function expects a pointer to some character. That's all.
--
__Pascal Bourguignon__ http://www.informatimago.com/
Grace personified,
I leap into the window.
I meant to do that.
| |
| Jim Cochrane 2006-12-19, 1:23 pm |
| On 2006-12-19, atv <alef@xs4all.nl> wrote:
> Why can i do :
>
> char *ptr;
>
> ptr="test";
>
> should it not be malloced ?
Because it is allocated statically at compile time - no need for dynamic
allocation; you can think of it as global/static data/memory.
>
> and why can i pass a string literal to a function(char *string)
>
> does string not need to be malloced as well ?
Same reason as above.
>
> or is this something the compiler takes care of?
>
--
| |
|
| On 2006-12-19 15:08:15 +0100, Jim Cochrane
<allergic-to-spam@no-spam-allowed.org> said:
[vbcol=seagreen]
> On 2006-12-19, atv <alef@xs4all.nl> wrote:
>
> Because it is allocated statically at compile time - no need for dynamic
> allocation; you can think of it as global/static data/memory.
>
>
> Same reason as above.
>
Ok, then what is strdup still useful for if i can do:
ptr="string";
And it's ok?
| |
| Jens Thoms Toerring 2006-12-19, 1:23 pm |
| atv <alef@xs4all.nl> wrote:
> Ok, then what is strdup still useful for if i can do:
> ptr="string";
For example when you need a string that you can modify - please
always remember that you're not allowed to change string literals.
E.g. trying
prt[ 5 ] = 'x';
to make "strinx" out of it is forbidden. Even if it should "work"
on you machine it won't on other architectures (or when you use
some compiler flags, e.g. to enforce ANSI C compliance etc.).
And of course, you may have normal strings that you first assemble
and then need to copy. strdup() can be quite convenient for that.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
| |
| Wayne C. Morris 2006-12-19, 1:23 pm |
| In article <45881ac4$0$322$e4fe514c@news.xs4all.nl>, atv <alef@xs4all.nl>
wrote:
> On 2006-12-19 15:08:15 +0100, Jim Cochrane
> <allergic-to-spam@no-spam-allowed.org> said:
>
>
> Ok, then what is strdup still useful for if i can do:
> ptr="string";
When you do
ptr = "string";
You get a pointer to the string constant, not a copy of it. If you follow
that with:
ptr[3] = 'u';
Your program will probably crash because the string constant in in a
read-only part of the process's memory space. If it doesn't crash (it
depends on the platform and the compiler), you'll have modified the
original constant, so that the first statement becomes:
ptr = "strung";
Using strdup would give you a copy of the string that you can modify.
strdup is also useful when you need an independent copy of a string, and
not a second pointer pointing to the same string.
|
|
|
|