为什么但为什么我们需要在C / C ++中使用指针? [英] Why but why do we ever need to use pointers in C/C++?

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

问题描述

我是一名攻读学位的学生。我的毕业之路仍然是一个相当大的交易。但是,我想确保我尽我所能超越所有其他cs学生。



无论如何,我正试图解决为什么我们使用指针或者为什么要在我的代码中使用它们。我知道指针指向一个地址。该地址演示了可变内存位置。



从本质上讲,我可以确定变量的地址。但是,假设每个用户都在使用计算机,那么特定变量的地址是否会发生变化,因为它是动态值(由硬件等分配)?



我觉得我需要它的唯一一次是在某种情况下,我想传递特定的对象作为参数,但唯一的解决方法是通过指针(还没有,这就是为什么我可以说它会是什么)。



如果有人想详细说明,请做。我想了解内存管理,以及它在我们的应用程序中的应用。



谢谢,

Jonathan V.



什么我试过了:



I'm a cs student working towards my degree. My road to graduation is still a pretty far deal. However, i want to make sure that im doing everything i can to surpass all the others cs students.

Anyways, i'm trying to wrap my head around why we use pointers or why ever bother using them in my code. I understand that a pointer points to an address. That address demonstrates the variable memory location.

So in essence, i can determine what address my variable have. However, assuming every user is using a computer, wouldn't the address change for that specific variable, since its a dynamic value (assigned by hardware etc.)?

The only time i feel i will need it is in a circumstance were i want to pass specific objects as parameters, but the only work around is through pointers (not there yet, thats why i can say what it would be).

Please if anyone wants to elaborate, please do. I want to understand memory management, and how it applies in our applications.

Thank You,
Jonathan V.

What I have tried:

unsigned short shortVar = 5;
std::cout << "shortVar: " << shortVar << std::endl;
std::cout << "Address of shortVar is: " << &shortVar << std::endl;

//OUTPUT
/*
shortVar: 5
Address of shortVar is: 0x7fff51986b2a
*/

推荐答案

在C中,传递地址是通过引用传递参数到函数的唯一方法,以便您可以直接修改变量。与传递值相比,您只修改变量的副本,原始变量保持不变。



对于大型结构,通过地址传递更快。
In C, passing an address is the only way to pass a parameter by reference to a function so that you can modify the variable directly. Contrast to passing by value, you are only modifying a copy of the variable, the original variable remains unchanged.

And for a big structure, passing by address is faster.


因为C / C ++旨在公开指向程序员的指针,这些指针负责处理它们。

其他语言也在使用指针但它对程序员来说是隐藏的。



以下是语言作者对C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2。 pdf [ ^ ]

http://www.ime.usp。 br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]



C ++编程语言 [ ^ ]
Because C/C++ are designed to expose the pointers to programmer that are responsible of their handling.
Other languages are using pointers too but it is hidden to programmers.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]


计算机程序中项目的地址与程序相关部分的开头有关。这可以是存储指令的CODE部分,也可以是存储大部分数据的DATA部分。加载程序后,硬件会将这些相对地址转换为绝对地址。有关计算机体系结构的基础书将为您解释详细信息。
Addresses of items in a computer program are relative to the start of the relevant section of the program. This could be the CODE section, where the instructions are stored, or the DATA section, where most of the data is. The hardware translates these relative addresses to absolute after the program is loaded. A basic book on computer architecture will explain the details for you.


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

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