Question about C - Minix

This is a discussion on Question about C - Minix ; I hope this is not too off-topic, but where else can I find a group of C experts? I want to make sure I understand the difference between arrays and pointers in C. char a[5] = "Hello"; char * p; ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Question about C

  1. Question about C

    I hope this is not too off-topic, but where else can I find a group of
    C experts? I want to make sure I understand the difference between
    arrays and pointers in C.

    char a[5] = "Hello";
    char * p;
    p = &a[0];

    As I understand it, there is only one rule:

    RULE: In an expression, 'a' evaluates to &a[0]. Always.

    This is the only rule. Though this rule has several consequences.

    1) Arrays and pointers feel much the same. You can write *(a+5) and
    you can write p[5]. The square brackets are just syntactic sugar.
    Instead of 'p = &a[0]' I could have written 'p = a'. If I pass the
    array 'a' to a function ("foo(a)") what the function receives is a
    pointer, not an array. Specifically, it receives &a[0].

    2) The PRACTICAL DIFFERENCE between an array and a pointer is that you
    can change the value of a pointer, but not the array. I can write 'p+
    +' and 'p = b', but I cannot do that with 'a'.

    Am I right?

    Thanks for the help.
    Daniel.

  2. Re: Question about C

    On Thu, 28 Aug 2008 06:34:45 -0700 (PDT)
    Daniel Carrera wrote:

    > I hope this is not too off-topic, but where else can I find a group of
    > C experts? I want to make sure I understand the difference between
    > arrays and pointers in C.


    comp.lang.c? There are many very knowledgeable people, and I've found
    them exceedingly helpful if you ask the right question.

    Mind out for the flamewars, though.

    B.


  3. Re: Question about C

    Daniel Carrera wrote:
    > I hope this is not too off-topic, but where else can I find a group of
    > C experts? I want to make sure I understand the difference between
    > arrays and pointers in C.


    > char a[5] = "Hello";
    > char * p;
    > p = &a[0];


    > As I understand it, there is only one rule:


    > RULE: In an expression, 'a' evaluates to &a[0]. Always.


    > This is the only rule. Though this rule has several consequences.


    > 1) Arrays and pointers feel much the same. You can write *(a+5) and
    > you can write p[5]. The square brackets are just syntactic sugar.
    > Instead of 'p = &a[0]' I could have written 'p = a'. If I pass the
    > array 'a' to a function ("foo(a)") what the function receives is a
    > pointer, not an array. Specifically, it receives &a[0].


    > 2) The PRACTICAL DIFFERENCE between an array and a pointer is that you
    > can change the value of a pointer, but not the array. I can write 'p+
    > +' and 'p = b', but I cannot do that with 'a'.


    > Am I right?


    AFAIK, spot on. Another small difference is that

    sizeof p

    evaluates to 4 on 32-bit machines, while

    sizeof a

    evaluates to 5, as you declared a to be an array of 5 characters. However,
    keep in mind that this is determined compile-time and cannot be used to
    determine the size of an array passed to a function (which is always
    passed as a pointer anyway).

    Regards,

    Jens


    --
    Jens de Smit
    Student Computer Science | Vrije Universiteit Amsterdam
    jfdsmit@few.vu.nl | http://www.few.vu.nl/~jfdsmit
    "[In the end, people] get furious at IT that the goddamn magic isn't working"
    -- Stewart Dean

  4. Re: Question about C

    On Aug 28, 3:49*pm, "J.F. de Smit" wrote:
    > AFAIK, spot on. Another small difference is that
    >
    > sizeof p
    >
    > evaluates to 4 on 32-bit machines, while
    >
    > sizeof a
    >
    > evaluates to 5, as you declared a to be an array of 5 characters. However,
    > keep in mind that this is determined compile-time and cannot be used to
    > determine the size of an array passed to a function (which is always
    > passed as a pointer anyway).


    Ah, thanks. I didn't think of sizeof.

    Thanks.
    Daniel.

  5. Re: Question about C

    Daniel Carrera escreveu:

    > I hope this is not too off-topic, but where else can I find a group of
    > C experts? I want to make sure I understand the difference between
    > arrays and pointers in C.


    You have comp.lang.c, but take care when asking questions because those guys
    are very strict about ANSI C (89, for some of them).

    > RULE: In an expression, 'a' evaluates to &a[0]. Always.
    > (...)
    > 2) The PRACTICAL DIFFERENCE between an array and a pointer is that you
    > can change the value of a pointer, but not the array. I can write 'p+
    > +' and 'p = b', but I cannot do that with 'a'.


    Yes. In C, an array identifier (_a_, in your example) behaves like it was a
    pointer to the first element of the array. As such, arithmetically, it
    behaves as if it was a char* to &a[0].

    The difference (apart from the sizeof one) is that you cannot modify the
    location where the array "begins". If you are used to ASM, you can compare
    an array declaration to something like:
    a db "Hello", 0
    whose "identifier" (label) you cannot modify. OTOH, you can compare a
    pointer to what you obtain when you do:
    mov eax, a
    Of course, this is only a loose comparation. Pointer arithmetic does not
    behave at all the same in ASM and in C, but you understand the analogy...

    > The square brackets are just syntactic sugar.


    Exactly. You can further think of [] as being applied only on pointers and
    objects that behave like pointers.

    --
    João Jerónimo

    "Computer are composed of software, hardware, and other stuff terminated
    in "ware", like firmware, tupperware, (...)" - by JJ.

+ Reply to Thread