指针和C中的全局变量之间有什么区别? [英] What's the difference between Pointers and Global Variables in C?

查看:1785
本文介绍了指针和C中的全局变量之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 C书,尝试在C中获得更好的基础。虽然我认为我通常会得到指针的概念,但有一点需要强调的是,它似乎将所指向的任何东西泛化为全局变量(例如,使用指针从 void functions),这自然会带来所有伴随的危险,我假设。



除了指针指向特定变量或索引在一个数组中,指针和全局变量之间的区别?

解决方案

他们是完全不同的野兽。为了更好地解释,让我定义两者。



指针:
$ b 变量拥有一些数据。指针是一种引用另一块内存的数据。把它想象成一个标志,指出在那里---->指向某种物体。例如,C中的字符串只是一个指向字符的指针,按照惯例,您知道它后面有更多的字符,直到 \ 0 字符。 C广泛使用指针,因为没有其他机制可以在程序的各个部分之间共享信息,除了....



全局变量:



在程序中,每个函数都有变量。这些可以是函数的参数,也可以是内部定义的参数。同样,你也有所谓的全局变量。这些变量存储文件中所有功能都可以访问的信息。这可以用来传递像全局状态或配置这样的事情。例如,在打印某些消息或存储全局状态对象(例如视频游戏中的分数)之前,您可能会有一个名为 debug 的代码。



我认为令您困惑的是:两者都可用于在部分代码之间共享信息。由于函数参数在C中被值传递,函数不能修改调用它的变量。有两种方法可以解决这个问题。第一种(也是正确的)方法是将一个指向变量的指针传递给函数。这样,函数知道在哪里修改父变量。
另一种方法是使用全局变量。这样,不用传递指针,而是直接编辑全局变量。



所以你可以使用它们来完成同样的事情,但它们的工作方式是相当单独。实际上,全局变量可以是一个指针。


I'm reading The C Book to try and get a better foundation in C. While I think I'm generally getting the concept of pointers, one thing sticks out to me is that it seems like it's generalizing whatever it's pointing to into a global variable (e.g. the ability to use pointers to return values from void functions), which naturally carries with it all the attendant dangers, I assume.

Aside from the fact that a pointer references a specific variable or index in an array, what is the difference between a pointer and a global variable?

解决方案

They're quite different beasts. To better explain, let me define both.

Pointers:

A variable holds some piece of data. A pointer is a type of data that refers to another piece of memory. Think of it as a sign that says "Over there ---->" pointing at an object of some sort. For example, strings in C are just a pointer to a character, and by convention, you know there's more characters following it until a \0 character. C uses pointers extensively, since there's no other mechanism for sharing common information between parts of the program, except for....

Global Variables:

In a program, you have variables in each function. These can be the parameters to the function, and ones defined inside. As well, you have what are known as global variables. These variables store information that all the functions in a file can access. This can be useful to pass things like a global state around, or configuration. For example, you might have one called debug that your code checks before printing some messages, or to store a global state object, like the score in a video game.

What I think is confusing you: Both can be used to share information between parts of code. Because function arguments are passed by value in C, a function can't modify the variables of what calls it. There are two ways to "fix" that problem. The first (and correct) way is to pass a pointer to the variable into the function. That way, the function knows where to modify the parent's variable. Another approach is to just use a global variable. That way, instead of passing around pointers, they just edit the global variables directly.

So you can use both of them to accomplish the same thing, but how they work is quite seperate. In fact, a global variable can be a pointer.

这篇关于指针和C中的全局变量之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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