RE: gcc not compiling

From: STYMA, ROBERT E (ROBERT) (stymar_at_lucent.com)
Date: 10/31/05

  • Next message: William Hooper: "Re: up2date failing to connect to RHN"
    To: "'For users of Fedora Core releases'" <fedora-list@redhat.com>
    Date: Mon, 31 Oct 2005 15:32:01 -0600
    
    

    > On Mon, Oct 31, 2005 at 01:30:44PM -0600, STYMA, ROBERT E (ROBERT) wrote:
    > > The syntax:
    > > int main(int argc, char **argv)
    > > works, but most of the C books I have seen recommend
    > > the *argv[] version.
    >
    > I meant to comment that these two different notations are functionally
    > identical; an array name is nothing more than a pointer. For example,
    > if we have the following code:
    >
    > #include <stdio.h>
    > void main (void){
    >
    > char foo[] = "This is a string\n";
    > char *bar = foo;
    >
    > /* these expressions all print "T\n" */
    > printf("%c\n", foo[0]);
    > printf("%c\n", *bar);
    > printf("%c\n", bar[0]);
    > printf("%c\n", *foo);
    >
    > /* these expressions all print "i\n" */
    > printf("%c\n", foo[2]);
    > printf("%c\n", *(bar + 2));
    > printf("%c\n", bar[2]);
    > printf("%c\n", *(foo + 2));
    > }
    >
    > The results may surprise programming students:
    >
    > $ gcc -o ptr ptr.c
    > ptr.c: In function `main':
    > ptr.c:2: warning: return type of 'main' is not `int'
    > [ddm@archonis ~]
    > $ ./ptr
    > T
    > T
    > T
    > T
    > i
    > i
    > i
    > i
    >
    >
    > In reality, foo[x] is just "syntactic sugar" for *(foo + x). This
    > is called pointer math, and works properly regardless of the size and
    > type of foo, so long as it was declared properly before being used.
    >
    > [I'm very bored today...]

    BTW, this is only for the benefit of the original poster. Maybe he
    will remember this tidbit when the prof gets to parameters and arrays.

    I agree, char **a, and char *a[] are equivalent. It only a style
    issue. Students sometimes take this too literally can can get in
    trouble. The equivalency is really only in parameter passing because
    arrays are passed as the address of the array. That effectively
    results in pass by reference instead of pass by value for the array.
    >From a lecture I used to give (titled: C Gun, Bullet, and Foot):

    file1.c

    char external_str[20] = "hello world";

    file2.c

    extern char *external_str;

    "If you were to link these two files together and then access
    via the reference in file2.c, the generated code would take
    the first 4 bytes of the string, assume they were and address,
    and attempt to go there and get the data. And that would be
    exactly where your program would go." That last line would always
    get a chuckle after about 5 seconds.

    Bob Styma

    -- 
    fedora-list mailing list
    fedora-list@redhat.com
    To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
    

  • Next message: William Hooper: "Re: up2date failing to connect to RHN"

    Relevant Pages

    • Re: VB-101: Passing Arrays ByVal vs ByRef
      ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
      (microsoft.public.dotnet.languages.vb)
    • Re: Need help with textboxes
      ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
      (comp.lang.javascript)
    • Re: Garbage Collection Issues in long-standing services
      ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: How to give selective access to the methods in a class?
      ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
      (comp.lang.java.programmer)
    • Re: Perlish map() function
      ... the reference material, the Array constructor was introduced in JavaScript ... evaluation would result in a ReferenceError. ... identifier shows that it is true. ...
      (comp.lang.javascript)