PDA

View Full Version : instructor situation.



NeonBlack
03-13-2008, 07:05 PM
As far as I know, the following 2 prototypes


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.

Dave_Sinkula
03-13-2008, 07:12 PM
I don't have K&R, but this (http://c-faq.com/aryptr/aryptrparam.html) 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

Sang-drax
03-13-2008, 08:46 PM
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.

Mario F.
03-13-2008, 08:57 PM
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.

tabstop
03-13-2008, 09:07 PM
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):


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.

NeonBlack
03-13-2008, 09:14 PM
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.

tabstop
03-13-2008, 09:35 PM
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.

vart
03-14-2008, 01:06 AM
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 ;)

CornedBee
03-14-2008, 01:11 AM
yeah, then show me where it says that in K&R
What does that have to do with ANSI?

whiteflags
03-14-2008, 01:50 AM
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.

Elysia
03-14-2008, 02:47 AM
I think it's more important to flush out bad practices such as fflush(stdin).

Sang-drax
03-14-2008, 10:42 AM
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.

whiteflags
03-14-2008, 10:53 AM
I think it's more important to flush out bad practices such as fflush(stdin).

I haven't said anything different? :confused:

matsp
03-14-2008, 10:54 AM
I haven't said anything different? :confused:

And I don't think Elysia was commenting on YOUR post in particular, but rather generally on the bad habits of instructors.

--
Mats

Elysia
03-14-2008, 10:55 AM
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*.

abachler
03-14-2008, 04:27 PM
using brackets in the argument list is pretty non-standard, even if it will compile and is technicalyl correct. Your professor is wrong, it is ansi, but it does present a readability issue, since most people trying to read yoru code would be confused for a few milliseconds at what you are doing. The real test is whether -



void foo(int []);

void main(void){ // i love you ***** :D
int* bar = NULL;

foo(bar);
}


will compile. if you can't use [] in a forward declaration without the variable name, and if the compiler wont accept it as synonymous rather than an overloaded function, then [] is incorrect.

Dave_Sinkula
03-14-2008, 04:34 PM
Are you seeing diagnostic messages with that code?

Mario F.
03-14-2008, 04:40 PM
i can't believe I'm seeing that again.

tabstop
03-14-2008, 08:38 PM
Are you seeing diagnostic messages with that code?

Of course he does:


temp.c: In function `main':
temp.c.4: error: `NULL' undeclared (first use in this function)
temp.c.4: error: (Each undeclared identifier is reported only once
temp.c.4: error: for each function it appears in.)
temp.c.3: warning: return type of `main' is not `int'

:)

Mario F.
03-15-2008, 05:33 AM
Yeah, copy-paste is a mother.

novacain
03-16-2008, 07:52 PM
If your boss (head developer / technical lead) told you to use the pointer declaration (or to do some other task their way), would you question their ability / understanding? [I hope not.]

Sometimes I want something done a particular way (and have a very good reason) but don't want to spend the 15-30 minutes explaining the specifics it to a new staff member who is not experienced in real world systems/conditions.



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 agree with Mario.

I do not use the array declaration and I have not seen many professional C programmers that do.

tabstop
03-16-2008, 08:55 PM
If your boss (head developer / technical lead) told you to use the pointer declaration (or to do some other task their way), would you question their ability / understanding? [I hope not.]

Sometimes I want something done a particular way (and have a very good reason) but don't want to spend the 15-30 minutes explaining the specifics it to a new staff member who is not experienced in real world systems/conditions.


And fair enough, but would you give an obviously false reason and expect it to go by?

NeonBlack
03-25-2008, 08:40 PM
It's cool now- I was working today and he just happened to be passing by as the computer froze. The computer just completely locked up for some reason. He asked me 'What the hell happened?' I told him I didn't know. 'Probably because of the damn array notation in your prototypes.' We both had a good laugh and he went on his way.

He must have been having a bad day.