:: Re: [DNG] Making sense of C pointer…
Top Pagina
Delete this message
Reply to this message
Auteur: Rainer Weikusat
Datum:  
Aan: dng
Onderwerp: Re: [DNG] Making sense of C pointer syntax.
aitor_czr <aitor_czr@???> writes:

> On 03/31/16 16:07, KatolaZ wrote:
>> On Thu, Mar 31, 2016 at 03:58:49PM +0200, aitor_czr wrote:
>>
>> [cut]
>>
>>>> > >On Wed, Mar 30, 2016 at 6:04 PM, Rainer Weikusat <
>>>> > >rainerweikusat@???> wrote:
>>>> > >
>>>> > >There's one
>>>>> > >>important difference:
>>>>> > >>
>>>>> > >>char chars[] = "12345";
>>>>> > >>
>>>>> > >>cause the compiler to reserve six bytes (five chars + terminating 0) in
>>>>> > >>some location the program can write to. Afterwards,
>>>>> > >>
>>>>> > >>*chars = R;
>>>>> > >>
>>>>> > >>could be used to change the string to R2345.
>>> >
>>> >This is because
>>> >
>>> >*chars <---> *(chars+0) <---> chars[0]
>>> >
>>> >are analogous. So, chars[0] changes from 1 to R.
>>> >
>> Again. being pedantic here the assignment:
>>
>>    *chars = R;

>>
>> assigns the value of the variable R (which should be a char) to the
>> first byte of the memory area pointed by chars.


Considering what it was supposed to mean, it should have been obvious
that I forgot the quotes. Had R been a variable, its type could have
been any numerical type as the value is to be converted as required (in
fact, the type of 'R' is int, not char).

Some people think this is a useful convenience feature. Others are
convinced that that's a sign of tthe devil's agents being secretly at work to
deprave mankind and an eternity of purgatory among dysfunctional
computer must invariably ... hold a sec ... that already
happened. Despite the "other's opinion" is generally accepted. Hmm ...

----
#include <stdio.h>

char chars[] = "12345";

int main(void)
{
    double R = 82.34;
    0[chars] = R;
    puts(chars);
    return 0;
}
----


Turned into a program which can be compiled to avoid accidental syntax
errors this time.