了解指针的目的 [英] Understanding the purpose of pointers

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

问题描述

我读了一本书上的数据结构和有困难把握指针的概念。只让我preface此说,我没有很多有经验的C.这里去....

I'm reading a book on data structures and having difficulty grasping the concept of pointers. Let me preface this by saying that I don't have a lot of experience with C. But here goes....

如果我做到以下几点:

 int num = 5;
 int *ptrNum;

 ptrNum = #

这是我的理解是,指针储备足够的内存与实际指针所需的内存以及一个32位的int尽管它的值就是变量的内存地址。

It is my understanding that the pointer reserves enough memory for a 32 bit int along with the memory required for the actual pointer although its value is simply the memory address of the variable.

如果相同的内存量保留什么是这样做的目的是什么?为什么要使用指针,而不是变量,NUM?在这里我完全关闭基地?

What is the purpose of doing this if the same amount of memory is reserved? Why would I use the pointer instead of the variable, num? Am I totally off base here?

推荐答案

您使用指针的情况下的值将无法工作。在你的榜样,你是正确的;有没有好处。该archtetypal边境线有用的例子是交换功能:

You use pointers in situations where a value won't work. In your example, you're correct; there's no benefit. The archtetypal border-line useful example is a swap function:

void swap_int(int *i1, int *i2)
{
    int t1 = *i1;
    *i1 = *i2;
    *i2 = t1;
}

调用顺序:

int main(void)
{
    int v1 = 0;
    int v2 = 31;
    printf("v1 = %d; v2 = %d\n", v1, v2);
    swap_int(&v1, &v2);
    printf("v1 = %d; v2 = %d\n", v1, v2);
    return 0;
}

如果你写的,没有使用指针 - 是这样的:

If you write that without using pointers — like this:

void swap_int(int i1, int i2)
{
    int t1 = i1;
    i1 = i2;
    i2 = t1;
}

int main(void)
{
    int v1 = 0;
    int v2 = 31;
    printf("v1 = %d; v2 = %d\n", v1, v2);
    swap_int(v1, v2);
    printf("v1 = %d; v2 = %d\n", v1, v2);
    return 0;
}

然后你只需换在函数两个局部变量没有在调用功能影响的值。使用指针,可以影响调用函数的变量。

then you simply swap two local variables in the function without affecting the values in the calling function. Using pointers, you can affect the variables in the calling function.

另请参阅:


  • scanf()的的功能 - 家庭

  • 的strcpy()

  • scanf()-family of functions
  • strcpy() et al

这是我的理解是,指针储备足够的内存与实际指针所需的内存以及一个32位的int尽管它的值就是变量的内存地址。

It is my understanding that the pointer reserves enough memory for a 32 bit int along with the memory required for the actual pointer although its value is simply the memory address of the variable.

您似乎在描述什么是仿佛:

What you appear to be describing is as if:

int *p1;

做相同的工作为:

does the same job as:

int _Anonymous;
int *p1 = &_Anonymous;

它没有;这是C.创建 P1 分配为指针足够的空间。作为第一个写的,它不初始化它,所以它指向一个不确定的位置(或没有位置)。它(指针)需要在使用前进行初始化。因此:

It doesn't; this is C. Creating p1 allocates enough space for the pointer. As first written, it doesn't initialize it, so it points to an indeterminate location (or no location). It (the pointer) needs to be initialized before it is used. Hence:

int  i1 = 37;
int *p1 = &i1;

P1 只保留足够的空间指针(配置通常情况下,32位的32位编译,64位的64位编译);你必须分配它指向单独的空间,你必须初始化指针。初始化指针的另一种方法是使用动态分配的内存:

But the allocation of p1 only reserves enough space for a pointer (normally, 32-bits for a 32-bit compilation, 64-bits for a 64-bit compilation); you have to allocate the space it points at separately, and you have to initialize the pointer. Another way of initializing pointers is with dynamically allocated memory:

int *p2 = malloc(1000 * sizeof(*p2));

if (p2 != 0)
{
    ...use p2 as an array of 1000 integers...
    free(p2);
}


你有没有覆盖的结构吗?如果不是这样,实施例覆盖结构,例如树木或链表,也无济于事。然而,一旦你已经把结构也一样,你就可以用树木或链接的列表:


Have you covered structures yet? If not, examples covering structures, such as trees or linked lists, won't help. However, once you have covered structures too, you'll be able to use trees or linked lists:

struct list
{
    int data;
    struct list *next;
};

struct tree
{
    int data;
    struct tree *l_child;
    struct tree *r_child;
};

这样的结构严重依赖指向正确连接的条目。

Such structures rely heavily on pointers to connect entries correctly.

这篇关于了解指针的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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