Page 1 of 3 << [1] [2] [3] >>

(no subject)

Date: 2006-04-11 09:01 am (UTC)
simont: A picture of me in 2016 (Default)
From: [personal profile] simont
Declaration mimics use. "int * x" means that "*x" is an "int", not that "x" is an "int *". If it meant the latter, then "int* x,y" would declare two pointers. It's all in K&R, all in K&R; bless me, what do they teach them at these schools?

I had to double-check that C++ references worked the same way; since Stroustrup believes in the wrong notation for pointers, I had a nasty feeling he might have built the wrong semantics into his bit of the language. But no; "int&x,y" declares y to be an int, not a reference, so regardless of Stroustrup's own silly spacing convention the same rule applies for sane people.

(no subject)

Date: 2006-04-11 09:02 am (UTC)
ext_8103: (Default)
From: [identity profile] ewx.livejournal.com
Absolutely, yes. I've just seen quite a bit of the wrongly-spaced form lately from otherwise-sensible people...

(no subject)

Date: 2006-04-11 09:05 am (UTC)
fanf: (Default)
From: [personal profile] fanf
The int* x form is standard C++ style ("RP" if you like, since it's what Bjarne uses.)

http://www.csse.monash.edu.au/~damian/papers/HTML/ModestProposal.html

(no subject)

Date: 2006-04-11 09:09 am (UTC)
From: [identity profile] filecoreinuse.livejournal.com
I generally strive for int* x; with the self-imposed restriction that I don't have multiple variable declarations per line (which gets around the gotcha). I've no justification for this other than it is more pleasing for me. I would kill anyone who did int a, *b, c; because I, at least, would miss that one :).

More generally I like a 'minimal criminal' form of Hungarian notation where I slap a 'p' to the front for each layer of indirection when it is known (int* pX; int** ppX; etc.).

(no subject)

Date: 2006-04-11 09:10 am (UTC)
From: [identity profile] uisgebeatha.livejournal.com
Good God, you're starting to put me off C for life here! What on earth is that supposed to be, a variable declaration? I'm sure Java does it in a more fluffy way. We loves the languages that hide low-level weirdness, we does. :P

(no subject)

Date: 2006-04-11 09:11 am (UTC)
From: [identity profile] filecoreinuse.livejournal.com
Oh, and pointer arithmetic for teh w1n :).

(no subject)

Date: 2006-04-11 09:13 am (UTC)
From: [identity profile] filecoreinuse.livejournal.com
Java doesn't have pointers and pretty much everything is a reference rather than a value type (except where it isn't[1]) so it neatly gets around the problem by not having to have a notation for it :).

[1] Modulo auto-boxing weirdness.

(no subject)

Date: 2006-04-11 09:16 am (UTC)
From: [identity profile] duncanneko.livejournal.com
I prefer int* x; as it mimics the mental description I use - I read that as "int-pointer x". Correctness of C/C++ style is probably not very high on my list of Important Life Skills, it must be said... (nor, for that matter, is 'Ability to write C++')

At least I'm in the majority on int &x; ^_^;

(no subject)

Date: 2006-04-11 09:16 am (UTC)
emperor: (Default)
From: [personal profile] emperor
Yes. While int x; declares x to be an int (i.e. an integer), int *x; declares x to be a pointer to an integer. Strictly, it declares that *x is an int. Is that clear? :)

FTR *y means "value pointed to by y", similarly &z means "address of z".

(no subject)

Date: 2006-04-11 09:20 am (UTC)
From: [identity profile] duncanneko.livejournal.com
Of course, if I sit and think about it, this means my mental model is slightly flawed - I still read * as 'pointer to' rather than 'dereference of' when I'm not paying attention, and thus lose the subleties.

And with that, I'm back to programming in Java. Have fun ^_^

(no subject)

Date: 2006-04-11 09:21 am (UTC)
simont: A picture of me in 2016 (Default)
From: [personal profile] simont
The part of this I have trouble with is reconciling "self-imposed restriction" with "would kill anyone who [violated it]". Either it's a principle everyone should adhere to, or it's a principle you personally choose to adhere to, but I don't think you can have it both ways. Surely?

I suppose it would work if you were operating in a closed community of C programmers who had all adopted the same coding style, but this is the real world and like everybody else you have to read code written in a wide variety of styles. Does it not therefore make sense to go with rather than against the intended usage of the language, so that code written in any style is just as legible to you?

(no subject)

Date: 2006-04-11 09:23 am (UTC)
From: [identity profile] uisgebeatha.livejournal.com
I see! I don't think I've come across this modulo auto-boxing weirdness yet. I think I'm starting to appreciate Java a whole lot more now... (Spotted that I'm a trainee geek yet? Damn, rumbled! :P)

(no subject)

Date: 2006-04-11 09:24 am (UTC)
simont: A picture of me in 2016 (Default)
From: [personal profile] simont
... but (I'm sure you know this, but for the edification of anyone reading who doesn't) that & is not the same as the one used in C++ reference declarations, so that while "int *z" means the value pointed to by z is an int, "int &z" does not mean the address of z is an int.

If anyone thinks this is barking mad and annoyingly inconsistent, I wouldn't argue. Stroustrup had to re-use some punctuation symbols when bolting ridiculous extras on to the side of a previously reasonably clean language, and all blame points to him. It made perfect sense when Kernighan and Ritchie last saw it.

(no subject)

Date: 2006-04-11 09:25 am (UTC)
From: [identity profile] kjaneway.livejournal.com
*wibble*

This is why, whenever I get to the "pointers" section while trying to learn C or C++, m brain explodes.

I think I'll stick to perl and shell.

(no subject)

Date: 2006-04-11 09:25 am (UTC)
From: [identity profile] uisgebeatha.livejournal.com
o.O *smiles and nods* This seems more complicated than it should be really! Why can't all languages be nice and easy for me to learn, like Java? And why do we need to know where z lives anyway?

Long is my path to ultimate geeky wisdom, it seems. -_-

(no subject)

Date: 2006-04-11 09:34 am (UTC)
From: [identity profile] filecoreinuse.livejournal.com
My restriction is multiple declarations per-line. The example of kill-worthy actions is a subset of that where not only are there multiple declarations (which is a personal choice) but mixing pointer and non-pointer declarations which I think is really hard to notice and is prone to generating buggy code.

Does it not therefore make sense to go with rather than against the intended usage of the language, so that code written in any style is just as legible to you?

Yes unless that usage is completely confusing. Doing
if(...)
  foo();
  bar();

is valid in the language but I'd never do that 'cos of the confusion it'd generate.

(no subject)

Date: 2006-04-11 09:34 am (UTC)
emperor: (Default)
From: [personal profile] emperor
Thankfully, I never have to write C++ :)

(no subject)

Date: 2006-04-11 09:36 am (UTC)
From: [identity profile] filecoreinuse.livejournal.com
In this case I used the word 'modulo' kinda means 'excepting' so it should have read "everything is a reference rather than a value type (except where it isn't, except auto-boxing)". Auto boxing is when a primitive type like int automagically gets converted to an object type (like Math.Int if my memory of Java holds up) when it is used like an object. See http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html.

(no subject)

Date: 2006-04-11 09:37 am (UTC)
From: [identity profile] cartesiandaemon.livejournal.com
I don't really mind. "*x is an integer" makes sense, but not when you start dealing with &s. "int* " would make sense if the language did it like that but it doesn't. Calling it something other than x would be most nice :)

(no subject)

Date: 2006-04-11 09:38 am (UTC)
emperor: (Default)
From: [personal profile] emperor
You need to know where z lives so you can tell bits of your program. C does call-by-value (see here for a bit of tutorial), rather than call-by-reference, so if I have an integer x and want a function foo to be able to modify it in my scope (rather than that of foo), I have to pass in a pointer to x instead:

void foo(int *value){*value*=2;}
....
int x; 
foo(&x);
/*x now twice as big*/

(no subject)

Date: 2006-04-11 09:39 am (UTC)
From: [identity profile] cartesiandaemon.livejournal.com
I do quite like perl, but I wouldn't have said it was better than C in terms of how putting punctuation symbols in the wrong places can create ambiguous code :)

(no subject)

Date: 2006-04-11 09:40 am (UTC)
emperor: (Default)
From: [personal profile] emperor
Hm. maybe using *= in that example wasn't as helpful as it might have been.

Autoboxing

Date: 2006-04-11 09:41 am (UTC)
emperor: (Default)
From: [personal profile] emperor
Ewww. Couldn't they just have made the primitive types proper objects and had done with it?

(no subject)

Date: 2006-04-11 09:44 am (UTC)
From: [identity profile] kjaneway.livejournal.com
True. but I *know* how it works in perl.

Re: Autoboxing

Date: 2006-04-11 09:44 am (UTC)
From: [identity profile] duncanneko.livejournal.com
Given the memory overhead on objects in Java (~4-8 bytes, plus the pointer to the object, depending on your JVM), I'm somewhat glad they didn't.

Of course, this can lead to the "why are you writing memory-optimised code in Java" argument, but I'm ignoring that for now.
Page 1 of 3 << [1] [2] [3] >>

November 2025

S M T W T F S
      1
2345678
91011121314 15
1617 181920 2122
23242526272829
30      

Most Popular Tags

Expand Cut Tags

No cut tags