instructor situation.

This is a discussion on instructor situation. within the A Brief History of Cprogramming.com forums, part of the Community Boards category; As far as I know, the following 2 prototypes Code: int function (int array []); int function (int *array); are ...

  1. #1
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435

    instructor situation.

    As far as I know, the following 2 prototypes
    Code:
    int function (int array []);
    
    int function (int *array);
    are identical in C, however I think the vast majority of people (including me) prefer the first. Today, one of my professors was looking at my code, and told me the first was "not ansi" and that I should use the 2nd. I told him they're both the same and it compiles with -ansi. He just said "yeah, then show me where it says that in K&R."

    Should I correct him? He also makes other offenses like fflush(stdin) and main() instead of int main(void). This is not a computer science or programming class, although it is a major component of the course. He has given me a ton of help over the past year and I have a great deal of respect for him. Also, he has been doing this for over 15 years while I only have a couple years of experience with C.

    I would appreciate any advice.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I don't have K&R, but this has the following mentions:
    References: K&R1 Sec. 5.3 p. 95, Sec. A10.1 p. 205
    K&R2 Sec. 5.3 p. 100, Sec. A8.6.3 p. 218, Sec. A10.1 p. 226
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I've seen professors write horribly outdated code, but none of them would ever comment on my code "not being ansi". If they did that, I'd attack their code.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,514
    Regardless I don't think "the vast majority of people" prefer the square brackets notation. But, I don't know that many people.

    As for the ansi, it has nothing to do with it. It's simply a matter of preference being that the pointer is semantically more correct.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think it depends on whether you think he's just mistaken and would change his mind if shown, or is just stubborn.

    If the first, there's no reason to ask K&R, ask ANSI (or ISO, from C99):
    Quote Originally Posted by C99 standard, 6.7.5.3 function declarators
    A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to
    type’’, where the type qualifiers (if any) are those specified within the [ and ] of the
    array type derivation. If the keyword static also appears within the [ and ] of the
    array type derivation, then for each call to the function, the value of the corresponding
    actual argument shall provide access to the first element of an array with at least as many
    elements as specified by the size expression.
    For what it's worth, if it's supposed to be an array, then I use array notation in my prototypes/declaration.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Thanks for the replies guys. Mario, maybe I exaggerated a little with the "vast majority" thing, but it seems like it is more common (among the people I know anyway). I also just read that the bracket thing was borrowed from c++, but that was a very long time ago (I'm guessing c89-ish).
    I could attack his code, as he does many things which are "not ansi" (I gave a few examples). But I don't really care about the difference between the two prototypes. My real concern is whether I should just forget about it, or correct him and defend my honor (as I take a great deal of pride in the quality of my code).

    So obviously I don't want to fight with him or hurt our relationship, and I am not known for being particularly tactful.

    edit: thanks for the source tabstop. I agree that if an arguement is an array it is much clearer to write the prototype as an array even though the pointer notation might be "more correct," depending on whom you ask.
    Last edited by NeonBlack; 03-13-2008 at 09:19 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by NeonBlack View Post
    Thanks for the replies guys. Mario, maybe I exaggerated a little with the "vast majority" thing, but it seems like it is more common (among the people I know anyway). I also just read that the bracket thing was borrowed from c++, but that was a very long time ago (I'm guessing c89-ish).
    Oh it goes way further back than that. If you go to Dennis Ritchie's home page at http://netlib.bell-labs.com/who/dmr/, you will find the C Reference Manual from 1975 (that came with 6th edition Unix). If you look on page 15, you'll find the same reference of arrays being treated as pointers in function declarations.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by tabstop View Post


    For what it's worth, if it's supposed to be an array, then I use array notation in my prototypes/declaration.
    If this is an array I just pass the array size and do not bother with the array notation in prototype
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    yeah, then show me where it says that in K&R
    What does that have to do with ANSI?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    I'd politely correct him (show him he's wrong) and then start using pointer notation from now on. Maybe he'll change his tone in the future, but it's no damage to your pride if you keep him happy to get a decent grade. But teachers, like any authority, should be sensible in what they instruct or do, and healthy skepticism keeps you wise.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    I think it's more important to flush out bad practices such as fflush(stdin).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by CornedBee View Post
    What does that have to do with ANSI?
    I think the 2nd edition of K&R deals with C89. The first edition clearly does not.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    Quote Originally Posted by Elysia View Post
    I think it's more important to flush out bad practices such as fflush(stdin).
    I haven't said anything different?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by citizen View Post
    I haven't said anything different?
    And I don't think Elysia was commenting on YOUR post in particular, but rather generally on the bad habits of instructors.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    No, it wasn't necessarily directed at you.
    I was merely mentioning that array[] vs array* may be a petty argument vs fflush(stdin) and that it might be more important to convince the teacher to stop doing that instead of arguing over array[] and array*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21