Live data from Hacker News

Everything you need to know about pointers in C (2010)

boredzo.org

1–10 of 25 posts

Re: Everything you need to know about pointers in C (2010)

#2
> A pointer is a memory address.

This is an amazingly wrong statement. In assembly you deal with memory addresses. Pointers in C are a much higher level abstraction.

> On current mainstream Intel processors, it occupies four bytes of memory (because an int is four bytes wide).

This depends on the compiler as well as the processor.

Re: Everything you need to know about pointers in C (2010)

#3
post #2

> A pointer is a memory address. This is an amazingly wrong statement. In assembly you deal with memory addresses. Pointers in C are a much higher level abstraction. > On current mainstream Intel processors, it occupies four bytes of memory (because an int is four bytes wide). This depends on the compiler as well as the processor.

>This depends on the compiler as well as the processor.

Yes. On x64 compilers targeting 64bit cpus, the following prints 8 instead of 4:

  #include 
  int main () {
      std::cout 

Re: Everything you need to know about pointers in C (2010)

#4
post #2

> A pointer is a memory address. This is an amazingly wrong statement. In assembly you deal with memory addresses. Pointers in C are a much higher level abstraction. > On current mainstream Intel processors, it occupies four bytes of memory (because an int is four bytes wide). This depends on the compiler as well as the processor.

The Standard disagree.

C11 6.5.3.2p3 “The unary & operator yields the address of its operand. If the operand has type ‘type’, the result has type ‘pointer to type’.”

I understand the intention to warn about the abstraction C introduces, but you’ve confused things.

Pointers and addresses are perfectly covered.

What you really want to bring is what the Standard calls the “C abstract machine”, for which the memory model can be surprising.

Re: Everything you need to know about pointers in C (2010)

#6
post #3
post #2

> A pointer is a memory address. This is an amazingly wrong statement. In assembly you deal with memory addresses. Pointers in C are a much higher level abstraction. > On current mainstream Intel processors, it occupies four bytes of memory (because an int is four bytes wide). This depends on the compiler as well as the processor.

>This depends on the compiler as well as the processor. Yes. On x64 compilers targeting 64bit cpus, the following prints 8 instead of 4: #include int main () { std::cout

Posting C++ in a thread about C is odd. In C it's:

    #include 

    int main(void)
    {
      printf("%zu\n", sizeof (int *));
      return 0;
    }

Re: Everything you need to know about pointers in C (2010)

#7
post #5

> void pointers are incremented or decremented by 1 byte. No, void pointer arithmetic is not allowed by the C standard: https://stackoverflow.com/questions/3523145/pointer-arithmet...

An unfortunate typo there: void pointer arithmetic is not allowed.

Re: Everything you need to know about pointers in C (2010)

#8
post #5

> void pointers are incremented or decremented by 1 byte. No, void pointer arithmetic is not allowed by the C standard: https://stackoverflow.com/questions/3523145/pointer-arithmet...

An unfortunate typo there: void pointer arithmetic is not allowed.

Oops! Fixed, thanks.

Re: Everything you need to know about pointers in C (2010)

#9
post #6
post #3

Earlier quoted context omitted.

>This depends on the compiler as well as the processor. Yes. On x64 compilers targeting 64bit cpus, the following prints 8 instead of 4: #include int main () { std::cout

Posting C++ in a thread about C is odd. In C it's: #include int main(void) { printf("%zu\n", sizeof (int *)); return 0; }

There's a double-quote character missing.

Re: Everything you need to know about pointers in C (2010)

#10
post #5

> void pointers are incremented or decremented by 1 byte. No, void pointer arithmetic is not allowed by the C standard: https://stackoverflow.com/questions/3523145/pointer-arithmet...

That answer does mention that GCC permits it as an extension. If the author uses GCC, maybe that’s where the confusion comes from?
Post reply on HN