Why do you put an asterisk before the name, not after the type, when declaring the C pointer?

Asked 2 years ago, Updated 2 years ago, 112 views

Why do you put an asterisk before the name, not after the type, when declaring the C pointer? It's usually declared

int* myVariable; instead of int *myVariable;

Why do you put an asterisk in front of your name like the last one when you can use both?

c naming-convention pointer variable

2022-09-21 20:01

1 Answers

You can use both as you said, but it's because the readability and the feeling of receiving are different.

int* myVariable1, myVariable2

How does it feel? I think the asterisk is one set with the type, so myVariable2 also has a pointer value.

But actually myVariable2 is just int type.

int *myVariable1, myVariable2

Now we think of myVariable1 as a pointer, and myVariable2 as int.

In situations where multiple variables are used, the latter is more communicative Typically, you display a pointer before the variable name.


2022-09-21 20:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.