为什么星号在变量名之前而不是类型之后? [英] Why is the asterisk before the variable name, rather than after the type?

查看:100
本文介绍了为什么星号在变量名之前而不是类型之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么大多数C程序员都这样命名变量:

Why do most C programmers name variables like this:

int *myVariable;

而不是这样:

int* myVariable;

两者均有效.在我看来,星号是类型的一部分,而不是变量名称的一部分.谁能解释这个逻辑?

Both are valid. It seems to me that the asterisk is a part of the type, not a part of the variable name. Can anyone explain this logic?

推荐答案

它们完全等效. 但是,在

They are EXACTLY equivalent. However, in

int *myVariable, myVariable2;

很明显,myVariable的类型为 int * ,而myVariable2的类型为 int . 在

It seems obvious that myVariable has type int*, while myVariable2 has type int. In

int* myVariable, myVariable2;

似乎两者都属于 int * 类型,但这是不正确的,因为myVariable2具有类型为 int .

it may seem obvious that both are of type int*, but that is not correct as myVariable2 has type int.

因此,第一种编程风格更加直观.

Therefore, the first programming style is more intuitive.

这篇关于为什么星号在变量名之前而不是类型之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆