为什么在C中使用指针? [英] Why Use Pointers in C?

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

问题描述

我仍然想知道为什么在C语言中不能简单地使用普通变量将某些东西设置为另一种东西.变量本身就是指向数据的指针,不是吗?那么,当您可以简单地使用原始变量时,为什么要使指针指向变量中的数据呢?是否可以访问所述变量中的特定数据位(或字节,我想)?

I'm still wondering why in C you can't simply set something to be another thing using plain variables. A variable itself is a pointer to data, is it not? So why make pointers point to the data in the variable when you can simply use the original variable? Is it to access specific bits (or bytes, I guess) of data within said variable?

我确定这是合乎逻辑的,但是我从未完全理解这个概念,并且在阅读代码时看到*pointers总是让我失望.

I'm sure it's logical, however I have never fully grasped the concept and when reading code seeing *pointers always throws me off.

推荐答案

变量本身就是指向数据的指针

A variable itself is a pointer to data

不,不是.变量表示对象,即 lvalue . lvalue 的概念与 pointer 的概念从根本上不同.您似乎在混合两者.

No, it is not. A variable represents an object, an lvalue. The concept of lvalue is fundamentally different from the concept of a pointer. You seem to be mixing the two.

在C语言中,无法重新绑定"左值以使其指向"内存中的其他位置.左值及其存储位置之间的绑定在编译时确定并固定.它并不总是100%特定的(例如,在编译时不知道局部变量的绝对位置),但它的特定性足以使其在运行时无法由用户调整.

In C it is not possible to "rebind" an lvalue to make it "point" to a different location in memory. The binding between lvalues and their memory locations is determined and fixed at compile time. It is not always 100% specific (e.g. absolute location of a local variable is not known at compile time), but it is sufficiently specific to make it non-user-adjustable at run time.

指针的整体思想是,其值通常在运行时确定,并且可以使其在运行时指向不同的内存位置.

The whole idea of a pointer is that its value is generally determined at run time and can be made to point to different memory locations at run time.

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

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