Tuesday, February 21, 2012

I understand C pointer syntax, but it doesn't make sense

No, C pointer syntax does not make sense, even if the author of this post understands what it represents. Let me begin by saying that the concepts underlying C pointers are not difficult to understand. You don't even have to know how to program to understand such a simple concept.

The problem is that you have to memorize a bunch of rules that violate good programming language design to use pointers in C. It's such a poor design that anyone with an ounce of programming ability has to say to herself, "That cannot possibly be the correct syntax - you just don't write programs like that!" Then you get used to it, and even though it doesn't make any sense, you're used to it, so you can do it.

What's wrong with float *pf? Well, for one thing, you're all of a sudden using a non-alphanumeric character to name a variable. That's completely inconsistent with the rest of C syntax. It's acceptable for Lisp, but not for C, because * is not acceptable as part of a variable name.

The second thing that's wrong with it is that it implicitly defines another variable, pf. Actually, it also defines **pf and ***pf as well. You can define **pf as (sort of but not really) a matrix, and then that defines *pf for you. One of the first things you learn when programming in C is that you have to define variables before you use them. Except sometimes you don't. They decided to throw type inference into the mix when it comes to pointers. They cleverly hide the type inference part by talking about things like indirection and addresses.

The third thing is that it is just weird to say you can define *pf as a float, and if you drop the first character, you get a variable that's completely different. How about pf for the float and pf.pointer for the pointer?

The fourth thing, which some people think makes sense, is that you use *pf to define a float and then *pf to dereference a pointer. Yeah, that makes sense. They've overloaded the (potentially non-alphanumeric) first character of a variable's name. Bjarne Stroustrup's proposal for overloading whitespace was a joke. C pointer notation is much worse, and unfortunately it was not a joke.

C pointer syntax is Hungarian notation taken to an extreme. There is nothing good about it. If you're able to use C pointer syntax correctly after seeing it for the first time, you're just memorizing language rules without making any attempt to understand the underlying concepts, and you should think seriously about whether programming is for you.

Clearly the author of the blog post I linked above does have programming ability. That's why he struggled for so long. He now understands something that makes no sense.

8 comments:

Anonymous said...

And in addition you use a `->' to dereference a field in a struct when using a struct pointer.

Unknown said...

I'm not sure if you're joking...

pf, *pf, and **pf are not different variables, and '*' is not part of the variable name.

'*' either (informally) an operator, or a modifier to the variable type.

You wouldn't say that 'int a' gives you another variable '-a'.

lmf said...

@mbp

I think you'd agree that it would be confusing if

int a = 4;

implicitly defined -a as a string holding "big red bus". It's no different from using a single statement to define both an integer and a memory address.

Further, an operator is something that you apply after you've defined something. It's insane to use the operator in the definition - which is why I said it's part of the name. To use your example,

int -a = -4;

would be a strange way to define a variable. If it's part of the definition, it really is part of the name, because again, operators don't go in definitions.

This whole discussion could be avoided if they'd just done something reasonable like address(x) to refer to the address of an object.

Unknown said...

int *a;

doesn't define an integer, only a pointer to an integer.

When I was first learning C this confused me, but later I became more comfortable with the essence of pointers: they really are just machine addresses, plus typing in the compiler.

You can write

#define ADDRESS_OF(x) (&(x))
#define CONTENTS_OF(x) (*(x))

but heaven help anyone else who has to maintain that code.

'*' in an expression is a unary prefix operator meaning "contents of".

'*' in a declaration is a part of the declarator, specifying that the thing being declared is a pointer. We're not using an operator in a declaration; we're using the same character which has a different meaning in different places. Nor is it part of the name: a declaration statement contains the name and type and initializer information.

The C99 standard is on the web and it's pretty clear that '*' is not part of the name. Thinking of it that way will just confuse you.

lmf said...

@mbp

As the title indicates, I understand how pointer syntax works, it just happens to be one of the most worst design mistakes in the history of programming languages. To a large extent it is a matter of preference.

Bjarne Stroustrup has a discussion on his Style and Technique FAQ:

A C programmer writes "int *p" and "the * binds to the name p in the grammar." A C++ programmer writes "int* p" because "the type of p is int*." I am in full agreement with Stroustrup's interpretation. The thing is, there's nothing to gain from this confusion. Why should someone learning C have to figure out what it means to say "the * binds to the name p in the grammar"?

I'm also not sure why it would be hard to maintain code that says address(x) or value(x). If you do that using a macro, sure, but if it were part of the language I don't see the problem.

Anonymous said...

[TITLE]Christian Louboutin Uk Shoes Show Women Stylishness[/TITLE]
The 1st step for securing an adverse credit score fast bank money is usually to compete out what sort including collateral you will often be heading to work while having. The much better your collateral then ones quicker christian louboutin jogging shoes you are able that will help safe your financial borrowing.

Quite a number of importantly, provide your kid with actions at home, too as outdoors home. They may possibly not want to get familiar with all of them, yet it is important for your child to be treated as if they may be involved. Do not go away your kid property when heading out for supper. Get your autistic kid with your just be sure get to a household christian louboutin shoes kind restaurant so when your child does misbehave through manifeste, it isn't this kind of a huge deal. Offer your child through an environment filled with different colors and textures. This can help to keep their consideration on particular strategies and in the incidence you give them while chance for social interaction, this could definitely promote them inside the long.

Most obvious suggestion first step in guidlines for finding journey job of one's goals is to have a tour instruction program. Instruction is readily meant for tour guides, tour directors, as well as professional tour management. Suffering from tour coaching, you'll be aware about security techniques to suit numerous travel strategies on top of that locations. You may learn about working with suppliers, handling emergencies, documentation, delivering instructive narration, and a lot more.

Based to the blueprint "torque = force ?time" torque arm, in absolute circumstances, to abrasion top heels, accomplish a hotlink in annex has increased, which the accessory will be caused to abate the intensity. Therefore, one within . heels to admission some of the braking force on that this agnate decrease.

The famous host oprah wore a classic of various inspired suit in his or her groundbreaking interview with Eliza Jackson. The tux blazer Oprah is having on in this interview is regarded as hottest fashion trends. (Even though Lady O wore this trend to incorporate financing 1993!)This blazer and be sure you dress can be worn enhance both dressy or typical outfits, and is undoubtedly a look that could go from day to party.

Also, visiting different discussion snowboards that cater for the niche you're especially planning to pursue in could enable you actually to assume of the new good deal far very much better ideas on how if you want to do your personal region creation.

Right as your kid has been diagnosed with autism, you may at seriously first go via some normal kind of panic mode. One outside of the very first pitfalls you may want so that you can do is setup offering their advice to for yourself to aid to you deal with nearly any damaging emotions you are going to be having about your kid. This probably will enable you to among this overwhelming time. You might also hunger for to do some research on your personal with regard to Autism, as it may possibly enable you to so as to know what you could possibly anticipate along with your son or daughter.

Anonymous said...

For more interview questions and c puzzles refer the below link:
http://tecz.org/interview-questions.html
http://tecz.org/puzzles.html

Anonymous said...

Umm.. I'm not even sure where to begin. I assume you've never programmed in assembly or attempted to write a compiler. I would suggest trying both because while you say you understand C/C++ pointers, you certainly don't come across that way in this post.