News for hackers. Stuff that matters.
Re:Massacring C Pointers (2018) by susam on Saturday September 05, 2020 @08:01AM (#24382961)
Fortunately, 18 years ago, I began learning C from the book The C Programming Language, 2nd ed. written by Brian Kernighan and Dennis Ritchie.

The subject of pointers was generally believed to be scary among fellow students and many of them bought pretty fat books that were dedicated solely to the topic of pointers.

However, when I reached Chapter 5 of the The C Programming Language (K&R), I found that it did a wonderful job at teaching pointers in merely 31 pages. The chapter opens with this sentence:

> A pointer is a variable that contains the address of a variable.

The exact point at which the whole topic of pointers became crystal clear was when I encountered this sentence in § 5.3 Pointers and Arrays:

> Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as * (a+i).

Indeed, it was easy to confirm that by compiling and running the following program:

  #include <stdio.h>
  
  int main() {
      int a[] = {2, 3, 5, 7, 11};
      printf("%d\n", *(a + 2));
      printf("%d\n", a[2]);
      printf("%d\n", 2[a]);
      return 0;
  }
The output is:

  5
  5
  5
C was the first serious programming language I was learning back then and at that time, I don't think I could have come across a better book than K&R to learn this subject. Like many others, I too feel that this book is a model for technical writing. I wish more technical books were written like this with clear presentation and concise treatment.
Re:Massacring C Pointers (2018) by disown on Saturday September 05, 2020 @12:35PM (#24384833)
K&R is easily one of the best CS/programming books. Short, concise and to the point.

Another way to learn pointers is via assembly. Either learn assembly or just look at the disassembled code. Look under the hood and it'll become clearer.

Of course the best way is to write your own C compiler... but that may be overkill for most.

Re:Massacring C Pointers (2018) by microtherion on Saturday September 05, 2020 @03:54PM (#24386418)
> Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as * (a+i)

I did not find that all that surprising, but the first time I saw that a[i] can also be written as i[a], I was very irritated.

Re:Massacring C Pointers (2018) by diehunde on Friday September 11, 2020 @10:51AM (#24443448)
I don't know. I read the pointers chapter at least 3 times, and I still get confused when I see pointer stuff in real code. The other day, I read the famous regex post by Russ Cox[1], and it took me a couple of hours to understand the pointer parts of the code even though the algorithm behind it was relatively simple. I guess pointers are just one of those things that for some people make a lot of sense, whereas others struggle with.

[1] https://swtch.com/~rsc/regexp/regexp1.html

Re:Massacring C Pointers (2018) by reader_1000 on Saturday September 05, 2020 @09:43AM (#24383458)
>> A pointer is a variable that contains the address of a variable.

Well, it is a little bit incomplete I think. A pointer contains both the address of a variable and a type. That is why pointer arithmetic works. Without type of pointer, p++ is not calculatable.

Re:Massacring C Pointers (2018) by csboyer on Saturday September 05, 2020 @09:51AM (#24383515)
The pointer variable does not contain the type - it only stores the address. Sizeof will return whatever the machine address width is.

The type is implicitly stored as the pointers type. Casting a pointer can change a piece of memory to be interpreted as any type you want. Although the cast might not make logical sense or may even caused undefined behavior in C.

Pointer arithmetic works off of whatever the current type is, as the compiler needs to know how to the memory offsets.

Re:Massacring C Pointers (2018) by reader_1000 on Saturday September 05, 2020 @10:30AM (#24383791)
I actually meant that pointer type is also important to understand the concept. I may not have stated it well. Yes, it does not store the type of the variable it points to but type of the pointer matters and it is significant because many operations depends on it.
Re:Massacring C Pointers (2018) by cassepipe on Saturday September 05, 2020 @12:02PM (#24384544)
C beginner here. I think you stated it well : "and a type" The post you responded to on the contrary misses your point that in memory a pointer is a variable that stores both an address and type information to what it points to.
Re:Massacring C Pointers (2018) by js2 on Saturday September 05, 2020 @03:10PM (#24386090)
No, it does not. The compiler knows the pointer type so that it can generate correct assembly, but the pointer type is not something stored in memory past the compilation phase.
Re:Massacring C Pointers (2018) by eru on Saturday September 05, 2020 @09:22AM (#24383329)
> A pointer is a variable that contains the address of a variable.

Given the way the C-standard is relatively carefully written, that explanation is a useful lie, but not necessarily true in every implementation of C.

Re:Massacring C Pointers (2018) by enriquto on Saturday September 05, 2020 @10:25AM (#24383752)
Sure. When you study physics you first learn Newtonian gravitation; and later, once you really master it, you learn that it is actually false and proceed to learn general relativity. In a similar way, it is very useful for people who learn C to start with the idea that "a pointer is an int". Then, they can discover that pointers are not really ints, but not before they have mastered working with pointers as if they were ints.
Re:Massacring C Pointers (2018) by jhardy54 on Saturday September 05, 2020 @11:21AM (#24384200)
Could you explain (or link to a good resource about) what pointers really are? I'm surprised that they aren't just ints that point to the address of a variable.
Re:Massacring C Pointers (2018) by enriquto on Saturday September 05, 2020 @11:56AM (#24384486)
In all modern implementations that I know of, pointers are effectively ints. But the formal definition of the C language takes great care in not saying that, allowing for implementation to chose how to implement pointers. In some older systems, for example in 16-bit MS-DOS with "high memory", pointers were not really ints, but pairs of ints (called the segment and the offset, if I recall correctly) whose mapping to memory positions was overlapping, i.e., non unique, you could have very different representations for the same memory position. I guess the gcc port to MSDOS by DJ Delorie had to deal with these issues somehow.

But it is possible to imagine completely exotic CPU architectures, where there is a separate type of register that can only be used for pointers and it is impossible to convert to int; you could still write C compilers for that architecture and be 100% conforming to the standard.

Re:Massacring C Pointers (2018) by Gibbon1 on Saturday September 05, 2020 @04:24PM (#24386594)
If I remember 16 bit C compilers let you choose a couple of different memory models. I remember 3, data and code fits in 16bits. data is 16 bits and code is segment and offset. And both pointers are segment and offset. I think there were more, especially for systems with 'high memory'

Most processors a pointer just gets stuffed into a register and then that's used by a assembly instruction to read/write a memory location. But it's certainly possible to have systems where it's not so simple.

Re:Massacring C Pointers (2018) by saagarjha on Saturday September 05, 2020 @12:07PM (#24384581)
They are variables that store an address. Usually they are of integer-ish thing internally, but this is in no way required, just like C has no need to run on a machine with linear memory. You could have a variable to memory hash table backing all your objects, for example, with a pointer that is a bucket or something.
Re:Massacring C Pointers (2018) by m463 on Saturday September 05, 2020 @12:58PM (#24385013)
This is the kind of thing that you need a significant other to straighten out.

"Hey!"

(smacks on head)

"It's an int that points to a variable."

"Now get off the computer and take out the trash without talking about garbage collection!"

"but it could be an int32_t and sometimes..."

(smack)

Re:Massacring C Pointers (2018) by steveklabnik on Saturday September 05, 2020 @06:13PM (#24387275)
This is what you want to read: https://blog.regehr.org/archives/1621
Re:Massacring C Pointers (2018) by saagarjha on Saturday September 05, 2020 @12:03PM (#24384558)
No, it’s correct. The statement makes no claims about how the pointer is stored, but semantically it is a variable with the information in it to locate another variable.
Re:Massacring C Pointers (2018) by tom_mellior on Saturday September 05, 2020 @12:15PM (#24384656)
This is true, as long as your definition of "another variable" stretches far enough to include memory regions allocated by malloc. And (leaving the C standard), also things allocated through other means, like memory-mapped files, and things like like memory-mapped peripherals.

In other words, the statement is incorrect.

Re:Massacring C Pointers (2018) by saagarjha on Saturday September 05, 2020 @12:46PM (#24384925)
I was specifically responding to the comment above, which had clearly conflated the standard not specifying the representation of a pointer with “anything anyone says about pointers isn’t actually correct”. Keeping the standard in mind the definition of “variable” generally extends to “any typed memory regardless of whether it has a name or not”, and the pointers K&R are talking about are fairly clear to mean this in the sense of the pointer being dereferenceable to a specific type. That being said, you are correct that there are other pointers not mentioned in that statement that are an entirely separate class from normal data pointers: untyped buffers, usually specified by void * but (for legacy reasons) often char * as well. And of course, there’s also function pointers that you dereference only to call them.
Re:Massacring C Pointers (2018) by grayhatter on Saturday September 05, 2020 @12:38PM (#24384861)
Um, why wouldn't it be correct to call any/all of thoes examples a variable?
Re:Massacring C Pointers (2018) by saagarjha on Saturday September 05, 2020 @12:49PM (#24384945)
They’re not variables until they’ve been specified a type, which is at the point you first assign to the memory in a typed fashion I believe. It’s significantly more pedantic than the first line in an introductory text will go into, though ;)
Re:Massacring C Pointers (2018) by grayhatter on Saturday September 05, 2020 @06:36PM (#24387401)
Nah, that's wrong. A variable doesn't need a type. Case in point; void.
Re:Massacring C Pointers (2018) by icedchai on Saturday September 05, 2020 @07:32PM (#24387694)
All variables have types in C. You can't declare a "void" variable. You can declare a void function. Try assigning it to a variable. You can't. void pointers (void*) are a different story.
Re:Massacring C Pointers (2018) by eru on Sunday September 06, 2020 @05:41AM (#24390179)
So just the opposite of what the comment you replied to suggested:

'void' is a type, but in C values of the type void are not first class. Ie a function can return a value of type void, but you can't assign it to a variable.

Re:Massacring C Pointers (2018) by saagarjha on Saturday September 05, 2020 @07:24PM (#24387647)
I'm not sure what you mean, could you explain a bit more?
Re:Massacring C Pointers (2018) by eru on Sunday September 06, 2020 @05:39AM (#24390170)
Are you saying void is not a type?
Re:Massacring C Pointers (2018) by vanviegen on Saturday September 05, 2020 @08:47AM (#24383147)
It is a good book. But to be fair, not many modern languages are as simple (low number of concepts) as C.
Re:Massacring C Pointers (2018) by Yajirobe on Saturday September 05, 2020 @08:54AM (#24383172)
> A pointer is a variable that contains the address of a variable.

Doesn't it technically also contain its own address? So it contains the address of another variable and the address of itself. Pointers are hard...

Re:Massacring C Pointers (2018) by jstimpfle on Saturday September 05, 2020 @09:10AM (#24383260)
A variable "contains" only its (current) value. That's the only use of "contains" that makes sense to me.

You can "get" the address of a variable (or more generally, of any l-value) with the & operator. That operator is nothing else but an "escape" character that says "Dear C, I don't want you to load the current value of that variable. Give me just the address where it lives so I can load it later".

(Same applies to storing instead of loading)

Re:Massacring C Pointers (2018) by nnq on Saturday September 05, 2020 @09:07AM (#24383242)
> Doesn't it technically also contain its own address

Huh? Then you probably don't understand the meaning of "contains" here. Or what variables are in general. Variables are hard...

Re:Massacring C Pointers (2018) by Yajirobe on Saturday September 05, 2020 @01:31PM (#24385314)
Well, the way I see it is this: you, as a person, contain your thoughts and possessions and so on - call it your value. But you also exist in space and time, you have a defined location. You don't carry it in your pocket, it's something you implicitly contain.
Re:Massacring C Pointers (2018) by phaemon on Saturday September 05, 2020 @11:54AM (#24384464)
Nope. And they're not really. Imagine a simple computer with, say, 32 bytes of ram.

All our bytes of ram are numbered. We read a value from byte number 8 and that value is 17.

"Byte number 8" is a pain to remember or calculate so we give it a label and let the computer keep track. That's a "variable".

What does that 17 mean though? Is it a value we want to use later on? It might be. Or it could be referring to Byte 17 in our ram. Byte 17 happens to have the value 42 so if we use Byte 8 as a pointer that's the value we end up with.

But it's entirely our choice. It's just a number and we can treat it as a value or a pointer to another memory address as we like.

Re:Massacring C Pointers (2018) by pps43 on Saturday September 05, 2020 @10:23AM (#24383740)
No, that would be a pointer to a pointer.