在 C 中,声明指针的正确语法是什么? [英] In C, what is the correct syntax for declaring pointers?

查看:33
本文介绍了在 C 中,声明指针的正确语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我依稀记得之前在另一个问题的回答中看到过这个,但搜索未能产生答案.

I vaguely recall seeing this before in an answer to another question, but searching has failed to yield the answer.

我不记得声明指针变量的正确方法是什么.是吗:

I can't recall what is the proper way to declare variables that are pointers. Is it:

Type* instance;

或者:

Type *instance;

虽然我知道在大多数情况下两者都会编译,但我相信有一些例子很重要,可能与在同一行上声明多个相同类型的变量有关,因此一个比另一个更有意义.

Although I know both will compile in most cases, I believe there are some examples where it is significant, possibly related to declaring multiple variables of the same type on the same line, and so one makes more sense than the other.

推荐答案

这只是你喜欢如何阅读它的问题.

It is simply a matter of how you like to read it.

有些人这样说的原因:

Type *instance;

是因为它说只有实例是一个指针.因为如果你有一个变量列表:

Is because it says that only instance is a pointer. Because if you have a list of variables:

int* a, b, c;

只有a是一个指针,所以这样更容易

Only a is a pointer, so it's easier like so

int *a, b, c, *d;

其中 a 和 d 都是指针.它实际上没有区别,只是关于可读性.

Where both a and d are pointers. It actually makes no difference, it's just about readability.

其他人喜欢将 * 放在类型旁边,因为(除其他原因外)他们认为它是指向整数的指针",并认为 * 属于类型,而不是变量.

Other people like having the * next to the type, because (among other reasons) they consider it a "pointer to an integer" and think the * belongs with the type, not the variable.

就我个人而言,我总是这样做

Personally, I always do

Type *instance;

但这真的取决于您和您的公司/学校代码风格指南.

But it really is up to you, and your company/school code style guidelines.

这篇关于在 C 中,声明指针的正确语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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