News for hackers. Stuff that matters.
Re:Everything you need to know about pointers in C (2010) by slmjkdbtl on Tuesday September 15, 2020 @07:36AM (#24480048)
The time [0] I found out about ptr_b in

  int* ptr_a, ptr_b;
isn't actually initialized as a pointer I changed all my C code to marking * before the variable name, fun times. It's helpful to think int as the base type and * the indicator of functionality like [] and ().

Also just found out the author was the developer of Adium [1], which is one of my favorite softwares (best logo) on macOS.

[0] http://c-faq.com/decl/charstarws.html

[1] https://adium.im/

Re:Everything you need to know about pointers in C (2010) by sai_c on Tuesday September 15, 2020 @08:02AM (#24480216)
Besides writing

  "int *p"
 
instead of

  "int* p"
 
I also always use one line per variable declaration, which should avoid confusion when the first style is used. But that's my personal preference.

Edit: corrected code quotation.

Re:Everything you need to know about pointers in C (2010) by ogogmad on Tuesday September 15, 2020 @08:04AM (#24480237)
When quoting code, leave a line and indent by two spaces.
Re:Everything you need to know about pointers in C (2010) by sai_c on Tuesday September 15, 2020 @09:03AM (#24480724)
Sorry, will do so in the future. Thx.
Re:Everything you need to know about pointers in C (2010) by darkhstarh on Tuesday September 15, 2020 @07:25PM (#24487878)
"int* p" really grinds my gears
Re:Everything you need to know about pointers in C (2010) by AdmiralGinge on Tuesday September 15, 2020 @09:54AM (#24481206)
I used to use that years ago! I had to stop when everyone moved over to Facebook Chat (what became Messenger) and eventually made a breaking change Adium couldn't support.
Re:Everything you need to know about pointers in C (2010) by jwilk on Tuesday September 15, 2020 @06:14AM (#24479605)
> 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...

Re:Everything you need to know about pointers in C (2010) by colejohnson66 on Tuesday September 15, 2020 @06:46AM (#24479747)
That answer does mention that GCC permits it as an extension. If the author uses GCC, maybe that’s where the confusion comes from?
Re:Everything you need to know about pointers in C (2010) by jwilk on Tuesday September 15, 2020 @09:59AM (#24481241)
But then he also writes that "sizeof(void) is illegal".

OTOH, GCC documentation says "sizeof is also allowed on void and on function types, and returns 1".

Re:Everything you need to know about pointers in C (2010) by MaxBarraclough on Tuesday September 15, 2020 @06:30AM (#24479673)
An unfortunate typo there: void pointer arithmetic is not allowed.
Re:Everything you need to know about pointers in C (2010) by jwilk on Tuesday September 15, 2020 @06:36AM (#24479692)
Oops! Fixed, thanks.
Re:Everything you need to know about pointers in C (2010) by weltensturm on Tuesday September 15, 2020 @06:58AM (#24479811)
By C half-assedly trying to make "* " part of the name, just so you can use "* ptr" everywhere like it is an actual variable name, you get confusions such as this.

First "foo_ptr's type is int * ", but then "The pointer has a type, too, by the way. Its type is int.".

I also refuse to agree with "An int * * 's type is int * ". :)

But I guess "the type of `* ptr` is int, the type of `ptr` is int * " would be even more confusing.

Re:Everything you need to know about pointers in C (2010) by MaxBarraclough on Tuesday September 15, 2020 @07:10AM (#24479875)
It's a pretty bad syntax.

A demonstration of a declaration statement where the asterisk operator binds to an identifier rather than to a type:

    int my_int, *my_ptr, my_other_int;
A demonstration of a different declaration where the asterisk operator does not bind to an identifier, as an identifier isn't even needed:

    extern void my_function(int*, char*);
So the real answer is that it depends on what you're doing. Terribly clunky.

Interestingly the D programming language mostly keeps C's syntax, but handles multiple declarations differently. https://dlang.org/spec/declaration.html#declaration_syntax

Re:Everything you need to know about pointers in C (2010) by 0x6862 on Tuesday September 15, 2020 @08:44AM (#24480554)
int array[] = {...}

1[array] == array[1]

Now add in some poorly named variables for some real fun

https://github.com/Droogans/unmaintainable-code#cs-eccentric...

Re:Everything you need to know about pointers in C (2010) by ChrisSD on Tuesday September 15, 2020 @05:35AM (#24479415)
> 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) by nitrix on Tuesday September 15, 2020 @06:11AM (#24479590)
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) by pjc50 on Tuesday September 15, 2020 @07:04AM (#24479851)
> Pointers in C are a much higher level abstraction.

Not really; almost all programmers treat them as addresses and almost all compilers represent them that way. Regardless of what the standard actually says. Leading to surprises when this doesn't hold true, and awkwardnesses of the 16-bit x86 "near/far" pointers.

I mean, that's the only reason that "array[index]" and "index[array]" are interchangeable; you don't see that in other languages.

Re:Everything you need to know about pointers in C (2010) by amelius on Tuesday September 15, 2020 @07:57AM (#24480187)
What higher level abstraction do pointers bring to the table over plain memory addresses other than pointer-arithmetic?
Re:Everything you need to know about pointers in C (2010) by jasode on Tuesday September 15, 2020 @05:59AM (#24479531)
>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 <iostream>
  int main () {
      std::cout << sizeof(int*) << std::endl;
  }
Re:Everything you need to know about pointers in C (2010) by unwind on Tuesday September 15, 2020 @06:16AM (#24479611)
Posting C++ in a thread about C is odd. In C it's:

    #include <stdio.h>

    int main(void)
    {
      printf("%zu\n", sizeof (int *));
      return 0;
    }
Re:Everything you need to know about pointers in C (2010) by MaxBarraclough on Tuesday September 15, 2020 @06:40AM (#24479711)
There's a double-quote character missing.
Re:Everything you need to know about pointers in C (2010) by unwind on Tuesday September 15, 2020 @10:20AM (#24481461)
Thanks. I suck at posting from my phone. Edited.
Re:Everything you need to know about pointers in C (2010) by flohofwoe on Tuesday September 15, 2020 @06:53AM (#24479789)
The article talks about the size of the pointee in memory though, not the size of the pointer.

An 'int' is usually 4 bytes wide when compiling for 64-bit ISAs (at least I haven't seen situations yet where this isn't the case, my experience is limited to x86 and ARM though). Modern C fixes this ambiguity with sized integer types (e.g. int32_t vs int64_t).

Re:Everything you need to know about pointers in C (2010) by jasode on Tuesday September 15, 2020 @08:06AM (#24480250)
>The article talks about the size of the pointee in memory though, not the size of the pointer.

Yes, you're right about the article's text. When I read gp ChrisSD's comment in isolation where he quotes ">A pointer is a memory address" -- followed immediately by him quoting ">On current mainstream Intel processors, _it_ occupies four bytes of memory"

... I thought the "_it_" was referring to a "pointer" instead of plain "int". It didn't occur to me to that the actual article has extra text in between those 2 extracted quotes which drastically changes the assumption of what the pronoun "it" means. My mistake.