指向数组的指针和数组 [英] Pointer to an array and Array of pointers

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

问题描述

由于我只是一个学生,我感到困惑上面的问题。如何是一个指针从指针数组不同的阵列?请解释给我,我会解释给我的老师。谢谢你。

As I am just a learner, I am confused about the above question. How is a pointer to an array different from array of pointers? Please explain it to me, as I will have to explain it to my teacher. Thank you.

推荐答案

想指针作为仅有一个单独的数据类型的。他们有自己的存储需求 - 比如它们的大小 - 他们占据一个x86_64的平台8个字节。这是空指针的情况下无效*

Background

Think of pointers as just a separate data type. They have their own storage requirements -- such as their size -- they occupy 8 bytes on a x86_64 platform. This is the case of void pointers void*.

在这些8个字节存储的信息是另一段数据的存储器地址。

In those 8 bytes the information stored is the memory address of another piece of data.

关于指针的事情是,因为点到另一块的数据,是非常有用的知道数据是什么类型过于这样你就可以正确地处理它(知道它的大小和结构)。

The thing about pointers is that since they "point" to another piece of data, it's useful to know what type that data is too so you can correctly handle it (know its size, and structure).

在其自己的数据类型名称,如指针代替他们构成基础上,他们是指如为int *的数据类型的名称指针为整数。如果不想类型连接到它信息的纯指针,你必须使用的选项无效*

In stead of having their own data type name such as pointer they compose their name based on the data type they refer to such as int* a pointer to an integer. If you want a plain pointer without type information attached to it you have the option of using void*.

所以基本上每个指针(以 INT ,到字符,到双击)仅仅是一个无效* (相同的大小,相同用途),但是编译器知道所指向的数据类型的 INT ,并允许您进行相应处理。

So basically each pointer (to int, to char, to double) is just a void* (same size, same use) but the compiler knows the data being pointed to is of type int and allows you to handle it accordingly.

/**
 *  Create a new pointer to an unknown type.
 */
void* data;

/**
 *  Allocate some memory for it using malloc
 *  and tell your pointer to point to this new
 *  memory address (because malloc returns void*).
 *  I've allocated 8 bytes (char is one byte).
 */
data = malloc(sizeof(char)*8);

/**
 *  Use the pointer as a double by casting it
 *  and passing it to functions.
 */
double* p = (double* )data;
p = 20.5;
pow((double* )data, 2);

指针数组

如果您有值的数组(比方说整数),在内存的某个地方,一个指向它包含它的地址的一个变量。

Pointer to array

If you have an array of values (let's say integers) somewhere in memory, a pointer to it is one variable containing its address.

您可以通过先取消引用指针,然后阵列和它的价值上运行一些工作访问该数组的值。

You can access this array of values by first dereferencing the pointer and then operating some work on the array and its values.

/**
 *  Create an array containing integers.
 */
int array[30];
array[0] = 0;
array[1] = 1;
...
array[29] = 29;

/**
 *  Create a pointer to an array.
 */
int (*pointer)[30];

/**
 *  Tell the pointer where the data is.
 */
pointer = &array;

/**
 *  Access the data through the pointer.
 */
(*pointer)[1] = 999;

/**
 *  Print the data through the array.
 *  ...and notice the output.
 */
printf("%d", array[1]);

指针数组

如果你有指针值的数组,指针的整个阵列是一个变量和阵列中的每个指针是指在一个值位于存储器别处

Array of pointers

If you have an array of pointers to values, the entire array of pointers is one variable and each pointer in the array refers to somewhere else in the memory where a value is located.

您可以访问此数组,里面的指针不提领,但为了达到从中一定值时,你将不得不取消引用数组中的指针之一。

You can access this array and the pointers inside it without dereferencing it but in order to reach a certain value from it you will have to dereference one of the pointers inside the array.

/**
 *  Create an array containing pointers to integers.
 */
int *array_of_pointers[30];
array_of_pointers[0] = 0;
array_of_pointers[1] = 1;
...
array_of_pointers[29] = 29;

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

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