我知道这是一个非常基本的问题,但在用高级语言编写了几个项目之后,我刚刚开始了一些基本的c++编程。
基本上我有三个问题:
为什么使用指针而不是普通变量? 我应该在何时何地使用指针? 如何在数组中使用指针?
我知道这是一个非常基本的问题,但在用高级语言编写了几个项目之后,我刚刚开始了一些基本的c++编程。
基本上我有三个问题:
为什么使用指针而不是普通变量? 我应该在何时何地使用指针? 如何在数组中使用指针?
当前回答
在某些情况下,需要函数指针来使用共享库(. dll或.so)中的函数。这包括跨语言执行工作,其中通常提供DLL接口。 使编译器 制作科学计算器,你有一个数组或向量或字符串映射函数指针? 尝试直接修改显存-制作自己的图形包 做一个API! 数据结构-用于您正在制作的特殊树的节点链接指针
使用指针有很多原因。如果您想要保持跨语言兼容性,那么在dll中使用C名编辑尤其重要。
其他回答
关于你的第二个问题,通常你在编程时不需要使用指针,但是有一个例外,那就是当你创建一个公共API时。
The problem with C++ constructs that people generally use to replace pointers are very dependent on the toolset that you use which is fine when you have all the control you need over the source code, however if you compile a static library with visual studio 2008 for instance and try to use it in a visual studio 2010 you will get a ton of linker errors because the new project is linked with a newer version of STL which is not backwards compatible. Things get even nastier if you compile a DLL and give an import library that people use in a different toolset because in that case your program will crash sooner or later for no apparent reason.
因此,为了将大型数据集从一个库移动到另一个库,如果你不想强迫其他人使用与你使用的相同工具,你可以考虑给一个指向应该复制数据的函数的数组指针。这样做的好处是它甚至不必是一个c风格的数组,你可以使用std::vector并通过给出第一个元素&向量[0]的地址来给出指针,并使用std::vector在内部管理数组。
Another good reason to use pointers in C++ again relates to libraries, consider having a dll that cannot be loaded when your program runs, so if you use an import library then the dependency isn't satisfied and the program crashes. This is the case for instance when you give a public api in a dll alongside your application and you want to access it from other applications. In this case in order to use the API you need to load the dll from its' location (usually it's in a registry key) and then you need to use a function pointer to be able to call functions inside the DLL. Sometimes the people that make the API are nice enough to give you a .h file that contain helper functions to automate this process and give you all the function pointers that you need, but if not you can use LoadLibrary and GetProcAddress on windows and dlopen and dlsym on unix to get them (considering that you know the entire signature of the function).
这里有一个略有不同,但有深刻见解的观点,为什么C的许多特性是有意义的:http://steve.yegge.googlepages.com/tour-de-babel#C
基本上,标准的CPU体系结构是Von Neumann体系结构,在这样的机器上,能够引用内存中数据项的位置并对其进行运算是非常有用的。如果您了解汇编语言的任何变体,您将很快看到这在低级别上是多么重要。
c++让指针有点令人困惑,因为它有时会为你管理指针,并以“引用”的形式隐藏它们的效果。如果你直接使用C语言,对指针的需求就更加明显了:没有其他方法可以实现引用调用,它是存储字符串的最佳方式,是迭代数组的最佳方式,等等。
Pointers allow you to refer to the same space in memory from multiple locations. This means that you can update memory in one location and the change can be seen from another location in your program. You will also save space by being able to share components in your data structures. You should use pointers any place where you need to obtain and pass around the address to a specific spot in memory. You can also use pointers to navigate arrays: An array is a block of contiguous memory that has been allocated with a specific type. The name of the array contains the value of the starting spot of the array. When you add 1, that takes you to the second spot. This allows you to write loops that increment a pointer that slides down the array without having an explicit counter for use in accessing the array.
下面是一个C语言的例子:
char hello[] = "hello";
char *p = hello;
while (*p)
{
*p += 1; // increase the character by one
p += 1; // move to the next spot
}
printf(hello);
打印
ifmmp
因为它取每个字符的值并加1。
以下是我的答案,我不会保证自己是专家,但我在我正在编写的一个库中发现了很棒的指针。在这个库中(它是一个带有OpenGL的图形API:-)),你可以创建一个带有顶点对象的三角形。draw方法取这些三角形对象,然后。根据我创建的顶点对象绘制它们。嗯,没关系。
但是,如果我改变顶点坐标呢?移动它或者在顶点类中使用moveX() ?好吧,现在我必须更新三角形,添加更多的方法和性能是浪费的,因为我必须更新三角形每次顶点移动。虽然不是什么大事,但也没那么好。
现在,如果我有一个有大量顶点和三角形的网格,网格在旋转,移动,等等。我必须更新使用这些顶点的每个三角形,可能在场景中的每个三角形,因为我不知道哪个使用哪个顶点。这需要大量的计算机,如果我在一个景观上有几个网格,天哪!我遇到麻烦了,因为我更新了每个三角形,几乎每一帧,因为这些顶点一直在变化!
对于指针,你不需要更新三角形。
If I had three *Vertex objects per triangle class, not only am i saving room because a zillion triangles don't have three vertex objects which are large themselves, but also these pointers will always point to the Vertices they are meant to, no matter how often the vertices change. Since the pointers still point to the same vertex, the triangles don't change, and the update process is easier to handle. If I confused you, I wouldn't doubt it, I don't pretend to be an expert, just throwing my two cents into the discussion.
为什么使用指针而不是普通变量?
简单的回答是:不要。;-)指针用于不能使用其他任何指针的地方。这要么是因为缺乏适当的功能,缺少数据类型,要么只是为了性能。更多的下面……
我应该在何时何地使用指针?
这里的简短回答是:你不能使用其他任何东西。在C语言中,你不支持复杂的数据类型,比如字符串。也没有办法将变量“通过引用”传递给函数。这就需要用到指针了。你也可以让它们指向任何东西,链表,结构体成员等等。但我们先不谈这个。
如何在数组中使用指针?
毫不费力,却充满了困惑。如果我们谈论简单的数据类型,如int和char,数组和指针之间几乎没有区别。 这些声明非常相似(但不相同——例如sizeof将返回不同的值):
char* a = "Hello";
char a[] = "Hello";
你可以像这样找到数组中的任何元素
printf("Second char is: %c", a[1]);
索引1,因为数组从元素0开始。: -)
或者你也可以这样做
printf("Second char is: %c", *(a+1));
指针操作符(*)是必需的,因为我们要告诉printf我们想打印一个字符。如果没有*,内存地址本身的字符表示将被打印出来。现在我们用字符本身代替。如果我们使用%s而不是%c,我们将要求printf打印'a' + 1所指向的内存地址的内容(在上面的例子中),并且我们不必把*放在前面:
printf("Second char is: %s", (a+1)); /* WRONG */
但这并不只是打印第二个字符,而是在下一个内存地址中打印所有字符,直到找到一个空字符(\0)。这就是事情开始变得危险的地方。如果您意外地尝试使用%s格式化程序打印类型为整型而不是char指针的变量,该怎么办?
char* a = "Hello";
int b = 120;
printf("Second char is: %s", b);
This would print whatever is found on memory address 120 and go on printing until a null character was found. It is wrong and illegal to perform this printf statement, but it would probably work anyway, since a pointer actually is of the type int in many environments. Imagine the problems you might cause if you were to use sprintf() instead and assign this way too long "char array" to another variable, that only got a certain limited space allocated. You would most likely end up writing over something else in the memory and cause your program to crash (if you are lucky).
哦,如果你在声明char数组/指针时没有给它赋一个字符串值,你必须在给它赋值之前给它分配足够的内存。使用malloc, calloc或类似的。这是因为你只在数组中声明了一个元素/一个内存地址。这里有几个例子:
char* x;
/* Allocate 6 bytes of memory for me and point x to the first of them. */
x = (char*) malloc(6);
x[0] = 'H';
x[1] = 'e';
x[2] = 'l';
x[3] = 'l';
x[4] = 'o';
x[5] = '\0';
printf("String \"%s\" at address: %d\n", x, x);
/* Delete the allocation (reservation) of the memory. */
/* The char pointer x is still pointing to this address in memory though! */
free(x);
/* Same as malloc but here the allocated space is filled with null characters!*/
x = (char *) calloc(6, sizeof(x));
x[0] = 'H';
x[1] = 'e';
x[2] = 'l';
x[3] = 'l';
x[4] = 'o';
x[5] = '\0';
printf("String \"%s\" at address: %d\n", x, x);
/* And delete the allocation again... */
free(x);
/* We can set the size at declaration time as well */
char xx[6];
xx[0] = 'H';
xx[1] = 'e';
xx[2] = 'l';
xx[3] = 'l';
xx[4] = 'o';
xx[5] = '\0';
printf("String \"%s\" at address: %d\n", xx, xx);
请注意,在对分配的内存执行free()后,仍然可以使用变量x,但您不知道其中有什么。还要注意,两个printf()可能会给您不同的地址,因为不能保证第二次内存分配是在与第一次相同的空间中执行的。