简体   繁体   English

指针,参考,C

[英]Pointers, references, C

My first programming language learned was Actionscript, and I'm having a hard time understanding pointers and references. 我学习的第一门编程语言是Actionscript,而且我很难理解指针和引用。

What is the purpose of pointers when I can just use references? 当我只能使用引用时,指针的目的是什么? I read one use case for pointers is when giving a large variable to another function instead of copying it, but can't I just add a & in front so it acts like a pointer? 我读了一个用例是指针时给出一个大的变量给另一个函数,而不是复制它,但我不能只是添加在前面的&所以它的作用就像一个指针?

(I know how knowing/passing the memory address of something is helpful, but I don't understand why when there is &) (我知道知道/传递某物的内存地址有什么帮助,但是我不明白为什么在出现&时)

EDIT: I mean references as using & 编辑:我的意思是引用使用&

There is no difference in a pointer and the &-Construction. 指针和&-Construction没有区别。 A pointer is a variable which stores the value of &... 指针是存储的&值的变量...

int a;
int *b = &a;

If you haven't already, I recommend checking out the Binking Pointer Fun videos. 如果您还没有,我建议您查看Binking Pointer Fun视频。 They are highly informative in a gentle manner ... and quite amusing. 他们以温柔的方式提供了丰富的信息,而且非常有趣。


C does not have references. C没有参考。 Languages like Java and ActionScript -- although ECMAScript, from which AS is based, does not mention references in the specification -- use the term "reference" to essentially mean "a pointer [value] that always refers to a valid object". 诸如Java和ActionScript之类的语言(尽管AS所基于的ECMAScript 并未在规范中提及引用),但使用术语“引用”实质上是指“始终引用有效对象的指针[值]”。

C does not have references. C没有参考。 It has pointers. 它具有指针。 Pointers are much more basic and primitive and there is no guarantee that they point to a valid object. 指针更加基础和原始, 不能保证它们指向有效的对象。

Also note, that Java and ActionScript (and C) are all call-by-value (although see call-by-object-sharing for a better higher-level term when "passing objects" in Java or when "passing pointers" in C). 还要注意,Java和ActionScript(和C)都是按值 调用 (尽管在Java中“传递对象”或C中“传递指针”时,请参见按对象共享以获取更好的高级术语) )。 In Java when you pass an object to a method the underlying implementation passes the reference value representing the object: in is very much like passing a pointer value to a function in C. 在Java中,当您将对象传递给方法时,底层实现将传递表示该对象的参考值:in非常类似于将指针值传递给C中的函数。

Happy coding. 快乐的编码。

C does not have references, they're a C++ feature. C没有引用,它们是C ++的功能。 C has pointers which are essentially special variables which hold a memory address. C的指针本质上是保存内存地址的特殊变量。 When you dereference a pointer using the * operator then you're telling the compiler you want the value at the address stored in the pointer. 当使用*运算符取消引用指针时,就告诉编译器您希望该值存储在指针中的地址处。 The & operator means 'address of', so &i means the address of the variable i. &运算符表示“的地址”,因此&i表示变量i的地址。

// create an integer variable holding the value 1
int i = 1;

// create a pointer to an integer and set the value of the pointer to the address of i
int * ptr = &i

// print the value at the address pointed to by ptr by dereferencing the pointer
printf("%i\n", *ptr); // prints 1

You can think of a pointer as a reference in that it points to an instance (in the majority of use cases) of a value. 您可以将指针视为引用,因为它指向值的实例(在大多数使用情况下)。 Of course, a pointer can point to any memory address which fits in the number of bits the pointer is composed of, which is what makes C so easy to shoot yourself in the foot. 当然,指针可以指向适合该指针所组成位数的任何内存地址,这使C如此容易用脚射击。

a reference in C is a pointer, so when you pass by reference you give the address of the argument to the function. C语言中的引用是一个指针,因此当您通过引用传递时,会将参数的地址提供给函数。

void foo(mybigStruct *p);

mybigstruct S;

foo( &S ); // passing S by reference which is the same as

mybigstruct *p = &S;

foo( p ); // p points to address of S

as opposed to passing by value which just copies the argument to the function, passing by reference(ptr) allows the function to modify its argument since the function can directly access the original place at a particular memory address where the passed data resides ie S can be modified by foo in the above example. 与仅将参数复制到函数的值传递相反,通过reference(ptr)传递允许函数修改其参数,因为函数可以直接访问传递数据所在的特定内存地址的原始位置,即S在上面的示例中被foo修改。

Within a function, you can increment/decrement a pointer argument; 在一个函数内,您可以增加/减少一个指针参数。 you can't change a reference argument. 您不能更改参考参数。 The latter prevents you from shooting yourself in the foot. 后者阻止您用脚射击。 I suspect the reference (and Java, and C#) were invented when Management realized that large numbers of their programmers were never going to get pointers right, and were doomed to an eternity of chasing memory leaks, using freed pointer bugs, etc. in their applications. 我怀疑引用(以及Java和C#)是在Management意识到他们的大量程序员永远都不会正确指向指针时发明的,并且注定了永恒的追逐内存泄漏,使用释放的指针错误等的情况。应用程序。

References also are a bit easier to read ('ref.field' instead of 'ptr->field'). 引用也更容易阅读(“ ref.field”而不是“ ptr-> field”)。 For this reason I use references instead of pointers wherever possible. 因此,我尽可能使用引用而不是指针。

If a function is allocating memory, a double-pointer seems more natural: 如果函数正在分配内存,则双指针似乎更自然:

void foo(char **ptr)
{   *ptr = (char *)malloc(1000);
}

The reference declaration seems backwards: 参考声明似乎是倒退的:

void bar(char * &ptr)    // I would have expected char &*ptr ?
{    ptr = (char *)malloc(1000);
}

Technically, pointers and references are the same in C++ (C has pointers only). 从技术上讲,指针和引用在C ++中是相同的(C仅具有指针)。 A conceptual difference is that references guarantee to represent a valid object while pointers could represent invalid objects (null-pointers). 概念上的区别在于,引用保证表示有效的对象,而指针可以表示无效的对象(空指针)。 Whether to use a pointer or a reference is very situational and also a matter of taste. 使用指针还是引用是非常有情境的,也是一个问题。

A big difference between pointers and references is that pointers can be null. 指针和引用之间的最大区别是指针可以为null。 This can be used for additional information eg in a function call. 这可以用于其他信息,例如在函数调用中。 A reference always needs a "backup" to which it "refers". 引用始终需要它“引用”的“备份”。 So a pointer would be used if the argument of a function can be null, otherwise a reference would suit better. 因此,如果函数的参数可以为null,则将使用指针,否则引用将更适合。

Another thing is the return value of a function. 另一件事是函数的返回值。 Theoretically you can return a reference to a function-local variable but it is most likely that your program will crash because the function is removed from the stack and the reference will have no more its "backup" available. 从理论上讲,您可以返回对函数局部变量的引用,但是您的程序很可能会崩溃,因为该函数已从堆栈中删除,并且该引用将不再具有其“备份”功能。 In this case a pointer to a new allocated memory has to be used. 在这种情况下,必须使用指向新分配的内存的指针。

Another use case are pointers to pointers eg as a argument to a function. 另一个用例是指向指针的指针,例如作为函数的参数。 The function can then allocate data at the original pointer. 然后,该函数可以在原始指针处分配数据。

In fact pointers in c is a huge topic which pitfalls and best uses are best learned and understood by practice. 实际上,c语言中的指针是一个巨大的话题,陷阱和最佳用法是通过实践获得最佳了解和理解的。

What is the purpose of pointers when I can just use references? 当我只能使用引用时,指针的目的是什么? I read one use case for pointers is when giving a large variable to another function instead of copying it, but can't I just add a & in front so it acts like a pointer? 我读了一个用例是指针时给出一个大的变量给另一个函数,而不是复制它,但我不能只是添加在前面的&所以它的作用就像一个指针?

Ok, here's a "large variable": 好的,这是一个“大变量”:

struct foo {
   int a[10000];
} biggie;

and here we pass it to a function by adding & in front of it: 在这里,我们通过在其前面添加&将其传递给函数:

some_function(&biggie);

but what does the function look like? 但是该函数是什么样的呢?

void some_function(struct foo* ptr)
{
    ...
}

See, &biggie is a literal pointer that is the address of biggie, and ptr is a variable pointer that can point to any struct foo . 请参阅, &biggie是字面指针,是biggie的地址,而ptr是变量指针,可以指向任何 struct foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM