指针和分配,它是如何与不同类型的工作? [英] Pointers and assignment, how does it work with different types?

查看:111
本文介绍了指针和分配,它是如何与不同类型的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做学习C艰难地自学从知道一点的Python的到来。我看过几个教程,但我不能让我的身边指针和分配是如何工作的负责人。我明白,如果你取消引用指针,你可以直接把它的值如下所示:

I'm doing "Learn C the hard way" for self-study coming from knowing a bit of Python. I have read several tutorials, but I can't get my head around how pointers and assignment works. I understand that if you dereference the pointer, you can directly give it a value as in:

int *anint = 42;

但是,我们专门引用一个已经创建的变量的存储位置?
具体来说,我尝试:

But what about specifically referencing the memory location of an already-created variable? Specifically, I tried:

char *pointer_to_strlit;
char *strlit = "some stuff";
pointer_to_strlit = &strlit;

为什么以下导致段错误我这样做后:

Why does the following cause a segfault after I do this:

printf("I print strlit: %s\nI print it again by pointing to it: %s\nI print where the pointer is pointing: %p\n", strlit, *pointer_to_strlit, pointer_to_strlit);

在C中的类好像真的很难说,他们将如何表现,以及如何使用指针引用特定类型。有没有专门列出的语法指向每个不同的数据类型(字符 *字符,<$的明确指南C $ C> *的char [] , INT 结构无效,函数等)?即使是一个步骤,这将有助于我理解了规则集列表将是有益的。

The types in C seem really hard tell how they will behave and how to use pointers to reference the specific types. Is there a clear guide that specifically outlines the syntax for pointing to each different datatype (char, *char, *char[], int, struct, void, null, functions, etc.)? Even a list of steps that would help me understand the rule set would be useful.

推荐答案

挂在那里!多练习三分球后,才有意义。但是,当有疑问,尝试推理了每个值表示。使用笔和纸,试图绘制每个内存字节的真正的帮助。

Hang in there! Pointers will make sense after more practice. But when in doubt, try to reason about what each value means. Using pen and paper to try to draw each byte in memory really helps.

的char * pointer_to_strlit; - 在这里你声明一个指针的性格。正如你可能已经知道,在C字符串是一个指向psented该字符串的第一个字符重$ P $。该字符串预计将是空终止。这意味着最终应该有一个ASCII 0 字符表明字符串已经结束了。

char *pointer_to_strlit; - here you declare a pointer to a character. As you probably already know, a string in C is represented by a pointer to the first character of that string. The string is expected to be null-terminated. This means that eventually there should be an ASCII 0 character indicating that the string has ended.

的char * strlit =一些东西; - 程序的内存将包含此字符串中的字符(11个字符是准确的 - 10,你看到的文字, ,1个用于空终止)。在这里,您声明另一个指针,此时距离该字符串指向第一个字符S。

char *strlit = "some stuff"; - your program's memory will contain characters for this string (11 characters to be exact -- 10 for the text you see, and 1 for the null terminator). Here you declare another pointer, this time pointing to first character "s" from that string.

pointer_to_strlit =安培; strlit; - 设置 pointer_to_strlit 的值的地址指针 strlit 。这可能不是你想要的这里。

pointer_to_strlit = &strlit; - this sets the value of pointer_to_strlit to the address of the pointer strlit. This is probably not what you want here.

如果事情变得混乱,尽量想每个指针作为一个普通的老号码 - 这本质上是一个指针是什么,一个庞大的数字再presenting在内存中的地址。让我们来看看再上面:

If things get confusing, try to think of each pointer as a plain old number -- that's essentially what a pointer is, a huge number representing an address in memory. Let's look at the above again:

的char * pointer_to_strlit; - 在这里,因为你没有把它尚未 pointer_to_strlit 的价值是不确定的。

char *pointer_to_strlit; - Here the value of pointer_to_strlit is undefined since you didn't set it yet.

的char * strlit =一些东西; - 比方说第一次S的地址 1234500 strlit 的值将是这个数字, 1234500

char *strlit = "some stuff"; - Let's say the address of the first "s" is 1234500. The value of strlit will be that number, 1234500.

pointer_to_strlit =安培; strlit; - 但是,什么是 strlit 的自己的地址?这是一些其他的价值,比方说 1234600 。值 pointer_to_strlit 现在将 1234600

pointer_to_strlit = &strlit; - But what is the address of strlit itself? It's some other value, let's say 1234600. The value of pointer_to_strlit will now be 1234600.

尝试打印 pointer_to_strlit %S 现在,你的程序将会崩溃 - 在地址 1234600 不是一个字符串的第一个字符,而是另一个号码 - 数量庞大的其中一个字节的指针。在code将试图穿越什么的它认为是一个字符串的寻找空终止,最终崩溃,当它到达无法访问的内存。

Try to print pointer_to_strlit as a %s now, and your program will crash -- at the address 1234600 is not the first character of a string, but another number -- one of the bytes of the huge number, the pointer. The code will try to traverse what it thinks is a string to look for the null-terminator, eventually crashing when it reaches inaccessible memory.

这篇关于指针和分配,它是如何与不同类型的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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