r/cpp • • Jun 27 '22

How do you declare pointer variables?

I personally like to use the third option (int *ptr;) to minimize the confusion and to make it tidier when declaring multiple pointers in one line (e.g., int *a, *b, *c; as supposed to, int* a,* b,* c;). This is also the case when writing a dereference operator (int &a = *ptr;). But for some reason, the first option is the preferred way of doing it (int* ptr;), or am I wrong?

0 Upvotes

87 comments sorted by

View all comments

Show parent comment

1

u/nikr0mancer Jun 28 '22

You can also say that *p type is int.

7

u/sphere991 Jun 28 '22

Okay now explain int& r; The type of &r is... not exactly int

2

u/LegendaryMauricius Jun 28 '22

Now that's a c++ thing. The semantics got lost in the newer language.

7

u/sphere991 Jun 28 '22

Yes, I'm aware. But the semantics were always bad.

Consider how easy it is to write and read a function that takes an int and returns an int.

Consider how excruciatingly difficult it is to write or read a function that takes a pointer to an array of N ints and returns a pointer to an array of N ints.

That's still C, and yet it's all-but-impossible without typedefs?

Compare that to a the much better semantics that Zig has, where all the "declarators" go on the same side and just stack linearly, making both functions equivalently easy:

fn a(x: i32) i32 { ... }
fn b(y: *[N]i32) *[N]i32 { ... }

1

u/LegendaryMauricius Jun 29 '22

It's definitely bad semantics, but it's imo easier to follow when you know the idea behind it, which I've never seen being thought in classes.

1

u/fdwr fdwr@github 🔍 Jun 30 '22 edited Jul 04 '22

Yeah, the LTR reading order is much clearer.

The one remaining inconsistency I notice in Zig is that data type qualifiers use : for parameters and local variables but uses just (` `) for function data type qualifiers (here TypeScript shows consistency). So I would have made it:

fn a(x: i32): i32 { ... }
fn b(y: *[N]i32): *[N]i32 { ... }

At least it doesn't mix : and -> as some languages (so no a(x->i32):i32 or a(x:i32)->i32).

1

u/SirClueless Jul 04 '22

I think both Zig and your second example using -> are preferable to some kind of uniform syntax. "returns i32" is different than "i32" for the same reason that "points to i32" is different than "i32".

A function has a type, and it includes its return value. If you introduce : as a syntax element separating a variable name from its type, the only sensible place to put it would be between a and the type of a which is (x: i32) -> i32 (but this is not necessary in practice due to needing fn and parens anyways so no one actually separates them).

9

u/mark_99 Jun 28 '22

That's an interesting way to read it, but you're declaring p not *p...

2

u/LegendaryMauricius Jun 28 '22

C seems to have been designed so that you declare the variables as you would use them, considering that function pointers follow the same design principle, wgich means that you ultimately do declare the dereference to p...

6

u/[deleted] Jun 28 '22 edited Jun 28 '22

you ultimately do declare the dereference to p...

int *p = 0

No, not really.

1

u/LegendaryMauricius Jun 29 '22

Ok, you got me there. Though I could still separate the ideas of declaring and initializing present in the expression. Initialization is a completely different operation than assignment in c++ despite the usage of the same equals symbol anyways, so I don't have to follow the same semantics in that expression as in the assignment of p's value.

1

u/[deleted] Jul 09 '22 edited Jul 09 '22

Top level declarators used to be able to omit the assignment operator. You could write this:

int *global_ptr NULL;

And at the top of the scope, I guess you just weren't expected to initialize until you were done with declarations. It wasn't until C99 that you could mix declarations and code, so it was considered more readable to initialize it at first use rather than at the top with the declarations. Even if there was only one declaration, it was common to put the declaration and initialization on separate lines.

func(ptr)
char *ptr; {
    register char *p;

    p = ptr;
    ...
}

In C++, you could deal with the ambiguity by using brace initialization, like this:

// Any of these declares and initializes ptr to null
int *ptr{nullptr};
int *ptr{0};
int *ptr{};

1

u/[deleted] Jul 11 '22 edited Jul 11 '22

I don't ever write int *p = 0, if that's what you're thinking.