C needs (or may need) help

So, for work, I may have to brush up on my programming skills in C. C is a relatively powerful language. In fact, it’s powerful enough to really screw up the world if you want.

I don’t object to C because it’s powerful though; I object to it because of its syntax:



How it works now:

int *ptr;
*ptr = 45;
int y = 96;
ptr = &y;

What’s happening here?
In line 1, we create a pointer named ptr, which points to an address.
In line 2, we say 45 lives at that address.
In line 3, we create a number y and set it equal to 96.
In line 4, we set the address of the pointer ptr to the address of y. Think of &y as y’s street address. 96 lives at &y, so when we say that ptr is taking y’s address, it means that ptr now points to 96.


What it means:

You’ll notice that there are only two types of values. There are the left values, the addresses of numbers in memory, and the right values, the actual values of the numbers. When we said ptr = &y, we were setting the left value (the address) of ptr to the same location as the left value of y, so ptr no longer points to 45, it points to 96.

The problem with this is that there are two different ways to specify left values. If we’re using a regular variable, like y, we use the address-of symbol, &. If we’re using a pointer, we use no symbol at all.


Possible solution:
Wouldn’t it be more convenient to just use one symbol whenever we’re referring to the address, and no symbol when we’re referring to what lives at the address?

Right now left values can be represented in 2 ways. But ideally, we could make the left value – the address – be specified by one symbol, *, and have the right value use no symbol at all:

int *ptr;
ptr = 45;
int y = 96;
*ptr = *y;

Notice what’s going on here. We start again by declaring a pointer, ptr.
In line 2, we set its right value to 45. Since this is a right value, we don’t use an *.
Then in line 4, we set the address of ptr to the address of y, so now ptr points to 54. Here we only use * when we’re referring to addresses. There is no need for & at all.

Why isn’t it this way? (this isn’t a rhetorical question)


Examples of translation:

Pages: 1 2

Comments

  1. Tim says:

    For one thing, semantically, the value of ptr is an address, so doing an assignment to a pointer variable should be assigning an address. What would be the syntax for assigning an explicit address (yes, there are purposes for that even today)

    Secondly, what happens to your notation when you get multiple levels of indirection?

    Thirdly, what you’ve proposed, while it seems as if it simplifies matters, actually complicates certain things. In the expression “*ptr = *y”, what is the datatype of the subexpressions “*ptr” and “*y”? How would you get the address of a pointer variable? (In your syntax, the answer is, you can’t)

    In my opinion, the only thing that doesn’t make sense in C is that the same symbol is used for declaring a pointer and dereferencing a pointer: ‘*’. The fact that there are two different symbols for increasing your level of indirection and decreasing your level of indirection makes perfect sense. If anything, there should be three symbols instead of two, not one instead of two.

    ~Tim

  2. Chris says:
    1. In response to the syntax for assigning an address comment:

      We know that ptr refers to the actual value of the variable (in this case, 45). This is represented by “ptr=45″. Furthermore, we know that the address of ptr comes from the l-value operator, *ptr. So to explicitly set the address to say, 0, we could write “*ptr=0″.

    2. I’m not sure what sort of indirection you’re referring to. The first type of indirection I would associate with your question is your next point.
    3. In the syntax *ptr = *y, we assign ptr’s address to be y’s address. Therefore, the datatype of each expression must be some form of addressing or pointing.

      So, a good question would be: Where is the address of the addresses? This is the sort of thing I feel doesn’t need to be dealt with for someone seeking a basic knowledge of the language. However, it is a useful consideration. My first inclination would be to say that the address of a pointer would be as follows: **ptr. It would be pretty unambiguous that this could mean little else, since we made the address-of operator unidirectional.

    4. You are correct that having separate increase/decrease indirection operators makes good sense. Naturally, with only one direction, you’re limited in the sense that you could not say:

      int y = memory address
      int b = what's located at y

      and I suppose that could be a problem in certain circumstances. (Can you actually do that in C?)

    My difficulty, of course, that I’m not trying to establish what makes sense, but rather what’s simple. With computers, nothing is simple or everything is simple, depending on who you are.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>