如何在C中接近指针? [英] How to Approach Pointers in C?

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

问题描述

我是C语言的新手,在学习指针时遇到一些问题.我尝试了交换,这就是我可以做的所有事情:)我知道每个变量在存储单元中都有其自己的地址(这是我的讲师告诉我们的),并且可以通过转到其关联地址来获取每个变量的值,然后获取存储在其中的值.我看过一些函数标头,例如:

I am very new to C and I have some problems learning about pointers. I experimented swapping and that's all what I can do with them :) I know that every variable has its own address in memory cells (this is what my lecturer told us) and every variable's value can be obtained by going to its associated address and then fetching the value stored in it. I've seen some function headers such as:

int doSomething(char **hihi);

我的头很困惑.我知道指针也是一个变量,它仅在其存储单元中存储地址信息.我读到它们与数组密切相关

my head is so confused. I know that pointer is a variable too and it only stores address information in its memory cell. I read that they are closely related to arrays

arr = &arr[0];

这就是我对指针的全部了解,我想知道如何才能加深对指针的了解.我在网上搜寻,但找不到任何有用的速查表来涵盖指针.而且我还想知道为什么它们如此重要,并且有什么方法可以在不使用printf()打印其地址(p)和值(\*p)??

That's all what I know about pointers and I wonder how I can deepen my vision upon pointers. I searched the net and I could not find any useful cheatsheet covering pointers. And I also want to know why they are so important and is there any way to understand actually what is going on without using printf() to print their addresses(p) and values(\*p)??

推荐答案

打印地址和值是查看它们的一种合理方法.但是,如果您可以启动并运行调试器,那就更好了,因为您可以更快地跟踪指针,观察指针的变化,等等.

Printing addresses and values is a reasonable way to look at them. But if you can get a debugger up and running, that's much better, because you can follow pointers faster, watch them change as you step, and so on.

如果您熟悉Windows中的快捷方式"或linux文件系统中的软链接,那么在您入门时,将指针视为指向另一个对象的快捷方式(软链接)可能会有所帮助(该对象是否是结构,内置类型,另一个指针等).

If you're familiar with "shortcuts" in Windows, or soft links in linux filesystems, then it might help, just as you're getting started, to think of a pointer as a shortcut (softlink) to another object (whether that object is a struct, a built-in type, another pointer, etc).

快捷方式仍然是文件.它在磁盘驱动器上占据了自己的空间,它引用了另一个文件,可以对其进行修改以引用与以前不同的文件文件.同样,C语言中的指针是一个占用内存的对象,包含另一个内存位置的地址,并且可以通过分配给它来更改为包含另一个地址.

A shortcut is still a file. It takes up its own space on the disk drive, it refers to another file, and it can be modified to refer to a different file file from what it used to. Similarly, a pointer in C is an object which occupies memory, contains the address of another memory location, and can be changed to contain a different address just by assigning to it.

一个区别是,如果双击快捷方式,则其行为就像您双击它所指向的东西一样.指针不是这种情况-您始终必须使用"*"或->"显式地取消引用指针才能访问其指向的对象.另一个区别是,具有指向C语言中某些内容的指针的指针很常见.

One difference is if you double-click a shortcut, it behaves as if you'd double-clicked the thing it points to. That's not the case with pointers - you always have to explicitly dereference a pointer with "*" or "->" in order to access the thing it points to. Another difference is that it's quite common to have pointers to pointers to something in C.

至于术语,您只需不幸地学习它即可. "int doSomething(char ** hihi)"的意思是一个称为doSomething的函数,该函数返回一个整数,并将指向char指针的指针作为参数".关键点是"char ** hihi"的意思是指针到字符的指针.我们将其称为指针到字符的hihi".您说hihi的类型"是char **,而* hihi的类型"(取消引用指针时得到的结果)是char *,而hihi的类型是char.

As for the jargon, you just have to learn it unfortunately. "int doSomething(char **hihi)" means "a function called doSomething, which returns an integer, and takes as a parameter a pointer to pointer a char". The crucial point is that "char ** hihi" means "a pointer-to-pointer-to-char. We will call the pointer-to-pointer-to-char hihi". You say that the "type" of hihi is char**, and that the "type" of *hihi (what you get when you dereference the pointer) is char*, and the type of **hihi is char.

在C语言中,指向char的指针通常表示一个字符串(换句话说,它是指向NUL终止数组中第一个char的指针).因此,"char *"通常表示字符串",但并非必须如此.它可能只是意味着指向一个字符的指针.有点像Windows中1字节文件的快捷方式(无论如何,还是使用FAT32),指向C中char的指针实际上比它指向的要大:-)

Frequently in C, a pointer to a char means a string (in other words, it's a pointer to the first char in a NUL-terminated array). So often "char *" means "string", but it doesn't have to. It might just mean a pointer to one char. A bit like a shortcut to a 1-byte file in Windows (well, with FAT32 anyway), a pointer to a char in C is actually bigger than the thing it points to :-)

与之类似,char **通常不仅意味着指向一个字符串指针的指针,而且还指向数组字符串指针的指针.可能不会,但是如果这样做,以下图片可能会有所帮助:

Likewise, a char** often means not just a pointer to one string-pointer, but to an array of string-pointers. It might not, but if it does then the following little picture might help:


hihi
 ____            ____                     ________     _________      _______
|____|   -----> |____|  *hihi       ---> |___A____|   |___B_____|    |___C___|
                |____|  *(hihi+1)   ------------------^              ^
                |____|  *(hihi+2)   ---------------------------------|
                | ...|    etc.

hihi指出了高塔工作量,这是我表示指针数组的方式.正如您已经指出的,我本可以用hihi [0]代替* hihi,用hihi [1]代替*(hihi + 1),依此类推.

hihi points to the tower-block effort, which is my way of representing an array of pointers. As you already noted, I could have written hihi[0] in place of *hi hihi[1] in place of *(hihi+1), and so on.

这是一个连续的内存块,每个指针大小的块都包含(即指向")另一个内存块的地址,该内存块不在善意知道的地方,包含一个或多个字符. .因此,hihi [0]是字符串A的第一个字符的地址,hihi [1]是字符串B的第一个字符的地址.

This is a contiguous block of memory, and each pointer-sized chunk of it contains the address of (that is, it "points to") another block of memory, off goodness-knows-where, containing one or more chars. So, hihi[0] is the address of the first char of string A, hihi[1] is the address of the first char of string B.

如果hihi不指向数组,而只是指向单个指针,那么塔楼就是平房.同样,如果* hihi不指向字符串,仅指向一个字符,则长而细的块是一个正方形.您可能会问:我怎么知道塔楼有几层?".在C编程中,这很重要-通常要么函数文档会告诉您(它可能会说"1"或"12",或者足够您告诉我要做的事情"),否则您将通过作为额外参数的楼层数,否则文档将告诉您该数组是"NULL终止"的,这意味着它将一直读取直到看到地址/值NULL,然后停止.第二和第三件事-argc包含参数的数量只是为了安全起见,argv是NULL终止的.

If hihi doesn't point to an array, just a single pointer, then the tower block is a bungalow. Likewise if *hihi doesn't point to a string, just one char, then the long thin block is a square. You might ask, "how do I know how many floors the tower block has?". That's a big deal in C programming - usually either the function documentation would tell you (it might say "1", or "12", or "enough for the thing you're telling me to do", or else you would pass the number of floors as an extra parameter, or else the documentation would tell you that the array is "NULL terminated", meaning that it will keep reading until it sees the address/value NULL, and then stop. The main function actually does both the second and third thing - argc contains the number of arguments, and just to be on the safe side argv is NULL-terminated.

因此,每当看到指针参数时,都必须查看该函数的文档,以查看该函数是否期望指向数组的指针,如果需要,则数组必须有多大.如果您对此不小心,则会创建一种称为缓冲区溢出"的错误,其中一个函数期望指向大数组的指针,将其赋予指向小数组的指针,并最终乱写你给它的东西,并开始破坏内存.

So, whenever you see a pointer parameter, you have to look at the documentation for the function to see whether it expecting a pointer to an array, and if so how big the array has to be. If you aren't careful about this, you will create a kind of bug called "buffer overflow", where a function is expecting a pointer to a large array, you give it a pointer to a small array, and it scribbles off the end of what you gave it and starts corrupting memory.

这篇关于如何在C中接近指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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