?

Log in

No account? Create an account
Lindsey's concise guide to pointer types in C - Lindsey Kuper [entries|archive|friends|userinfo]
Lindsey Kuper

[ website | composition.al ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Lindsey's concise guide to pointer types in C [May. 31st, 2010|08:43 pm]
Lindsey Kuper
[Tags|]

Between work and class, I'm finally starting to get my C legs.1 For your edification, then, I present here a useful guide:

pointer to int
Haha. Ahahahahahah. Heh. *snort* Oh man. Okay. By "pointer to int", we mean "variable containing an address containing something that has type int". Except...no, wait, that's not it either. That's just what we want you to think. What we actually mean is "variable containing an address whose contents could be anything at all, but which we currently plan on interpreting as an int, not to say that we don't fully intend to change our minds in the future, possibly several times, and not to say that said contents themselves won't change, possibly several times."
pointer to char
See "pointer to int", mutatis mutandis.
pointer to long
See above.
pointer to void
Um...anything whatsoever. Seriously, this is just whatever the hell you want it to be.2

The rest is basically the same, so I'll just stop there. Enjoy!


  1. Ba-dum-ch!
  2. Come on! I mean, why have a type system at all? What is wrong with you people?
LinkReply

Comments:
[User Picture]From: oniugnip
2010-06-01 12:53 am (UTC)
The type system is there to make pointer arithmetic easier!

(and when you're casting things willy-nilly and the program asplodes at run time, well, we know whose fault it is.)
(Reply) (Thread)
[User Picture]From: lindseykuper
2010-06-01 01:53 am (UTC)
The language's fault? OH BURN

(To be totally honest, I kind of enjoy C. You just have to not come into it with any expectation of anything making any kind of type-theoretical sense at all, and then you'll be fine.)
(Reply) (Parent) (Thread) (Expand)
(Deleted comment)
[User Picture]From: oniugnip
2010-06-01 02:17 am (UTC)

Re: Drive by 1990s-ing

Except that happened, by and large! Look out the window and see all the Pythonistas and Rubyfolk with hip haircuts and fixed-gear bikes!
(Reply) (Parent) (Thread) (Expand)
(Deleted comment)
[User Picture]From: keturn
2010-06-01 03:07 am (UTC)

it took me a minute but that was a good pun

Sometimes I forget how many roads there are to being a software geek; I was working with C long enough ago that it never occurred to me that you could be doing the work you're doing, working with the people you're working with, without some C legs. And yet, you are, and the textbooks by Tanenbaum and Aho, Hopcroft & Ullman that have been sitting on the bookshelf next to me for the last decade1 remain unread.

The language I've been trying to establish a working relationship with for the last month, in the name of providing the world with yet another piece of project management software, is Javascript. Whose idea was this anyway?




1. I can talk about stuff in terms of "decade" now. Geez I'm old.
(Reply) (Thread)
[User Picture]From: lindseykuper
2010-06-01 04:48 am (UTC)

Re: it took me a minute but that was a good pun

The trick is to learn the exotic languages first. Then folks just assume you know the usual ones.

Edited at 2010-06-01 05:06 am (UTC)
(Reply) (Parent) (Thread)
(Deleted comment)
[User Picture]From: lindseykuper
2010-06-01 04:58 am (UTC)
That's probably the right way to think of it. Then "int" or whatever just becomes a mnemonic.

I realized yesterday that for a long time I'd been thinking of pointers as boxes that hold things, but that I should really be thinking of them as differently-sized frames that slide around over data. With a box, you just open it by...opening it, but the way you dereference a pointer depends on what it's pointing to.

Edited at 2010-06-01 04:58 am (UTC)
(Reply) (Parent) (Thread) (Expand)
(Deleted comment)
[User Picture]From: poodleface
2010-06-02 02:57 am (UTC)
I <3 C :-D
(Reply) (Thread)
[User Picture]From: idealisms
2010-06-02 02:52 pm (UTC)
It's actually getting better. Newer version of gcc will yell at you if you violate strict aliasing rules, so it's getting harder to write code where your pointer keeps changing types.
http://en.wikipedia.org/wiki/Aliasing_(computing)#Conflicts_with_optimization

(Reply) (Thread)
[User Picture]From: lindseykuper
2010-06-03 04:12 pm (UTC)
According to that article, strict aliasing both enables and interferes with loop optimization. Seems hard to even tell if you'd want it in a given situation.

P.S. sekrit backchannel bug report!: I had this problem this morning (even on Ubuntu 10.04 LTS). (But it went away. If it happens again, I'll see if I can add a useful comment.)
(Reply) (Parent) (Thread)
[User Picture]From: jes5199
2010-06-03 09:49 pm (UTC)
You're the only person I know who still uses livejournal, so I didn't see this.

Next you should try assembler. It's just C without functions.
(Reply) (Thread)
[User Picture]From: lindseykuper
2010-06-03 11:02 pm (UTC)
You should just subscribe to the feed, Jes.
(Reply) (Parent) (Thread) (Expand)
From: boojum
2010-06-08 05:45 am (UTC)
I find it most helpful to think about pointers as boxes containing arrows. Arrows to where varies, and god help you if your box is labeled "arrow to cookies" and the arrow actually ends in dirty diapers (Prof. Kuenning had a very small child at the time of that memorable lecture.). Of course, sometimes you're looking at a perfectly good number and just interpreting it as an arrow, or you've got this arrow that looks like 712342344 if you squint right.

Subtle thing that causes tricksy and evil bugs if you're coming from a language that's not older than me: "int* a, b;" defines one pointer-to-int and one int. Every other language (other than C++, because C++ stuck too closely to C) gets it right and makes "*" a modifier of "int", but in C it's a modifier of "a". It's for the same reason that the freeway onramps on the 110 are horrible: if there's no prior art, you don't have any bad examples to avoid.

Pointer-to-void is fully as evil as you think it is. It's also the only way to get large swathes of anything done in C. C is beautifully brain-breaking that way. Overall C is typed about as strongly as Perl is typed, but in a differently broken way.

If you spend any amount of time in C / C++, you will want to know a good debugger. gdb may (sadly) still be state of the art. Sorry.
(Reply) (Thread)
[User Picture]From: lindseykuper
2010-06-11 12:15 am (UTC)
The "int* a, b;" silliness gets warned against so frequently and so vehemently, I can't believe anyone still actually writes it. I don't think I've encountered it in code that wasn't example-of-what-not-to-do code.

I dunno; I like the frame idea better than the box idea for pointers, probably because my box-and-arrow metaphor is already in use.
(Reply) (Parent) (Thread) (Expand)