什么是指针变量的数据类型? [英] What is the data type of pointer variables?

查看:167
本文介绍了什么是指针变量的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查阅了以下连结:

Link1
链接2

在上面的link1中,在回答中提到 Pointers是指针类型

In the above link1 it was mentioned in answer that "Pointers are of pointer type".

我只需要知道指针是否是数据类型。

I just need to know is pointer is a data type or not.

没有人回答这个问题在单字。它是数据类型还是不是?

No one has answered that question in single word. It is datatype or not?

这是我所说的第二个链接,

This is the second link which i refered says that


指针只是一个保存地址的变量,因此可以认为指针是数据类型,但它并不是定义为数据类型(根据C编程语言,Kernighan& Ritchie)。


Pointers are simply a variable that hold an address so one could argue that a pointer is a data type, but it is not defined as a data type (per "The C Programming Language". Kernighan & Ritchie).


推荐答案

是的,指针是一种数据类型。其中最纯粹的形式(主要在这里谈论C)是 void * 。 A void * 可以传递内存地址(这是一个指针),但它不能被取消引用。 Derefernecign一个指针是你做什么来获取这个指针指向的数据,这意味着你知道你从内存中读取什么类型的数据。该类型确定将要读取多少内存,并且因为 void nothing。可以将 void * 设置为指向任何可以包含任何类型的内存块,以便可以转换 void * 到任何其他指针类型(例如 int * ),并解除引用。

Yes, a pointer is a data type. The purest form of which (mainly talking about C here) is void *. A void * can be to pass a memory address around (which is what a pointer is), but it can't be dereferenced. Derefernecign a pointer is what you do to get at the data this pointer is pointing at, which implies that you know what type of data you're reading from the memory. The type determines how much memory will be read, and because a void is "nothing". A void * can be set to point at any block of memory, that can contain any type, so you can cast a void * to any other pointer type (int *, for example), and dereference that, instead.

我们使用每个类型来存储一个特定的数据(值),我们使用 char 来存储单个字符, int 来存储整数, double 来存储双精度小数等等。除了指针,这些类型都不用于存储在存储器中的位置。因此,与其他类型一样,指针用于存储特定的数据。所有指针的母亲都是 void *

Each type we have is used to store a specific piece of data (value), we use a char to store a single character, an int to store an integer, double to store double precision decimals and so on. None of these types are used to store locations in memory, apart from pointers. So just like the other types, a pointer is used to store a specific piece of data. And the mother of all pointers is void *.

可悲的是,这个 void * / code>是相当受限:你不能解引用它,你不能使用它的指针算术(不是根据标准反正)。所以C为你提供了一系列的派生指针类型,使生活更容易: char * int * code> double * 等等。

它们是真正的短手:(char *)void * my_ptr ;

Sadly, this void * is rather restricted: you can't dereference it, you can't use it for pointer arithmetic (not according to the standard anyway). So C provides you with a series of derived pointer types, that make life easier: char *, int *, double * and so on.
What they are, really, is short-hand for: (char *) void * my_ptr;

指针有自己的大小,不管它们说的是什么类型:

Pointers have their own size, irrespective of the type they're said to point at:

char a_character = 'a';//type: a char
char *a_char_ptr = &a_character;//memory address, in this case, the one holding a_charachter

通过查看这两个变量的大小可以最好地区分这些区别:

The distinction is probably best seen by looking at the sizes of both these vars:

printf("%zu <> %zu\n", sizeof a_character, sizeof a_char_ptr);

上述代码会提供类似1<> 8

指针也有自己的<$ p>

Pointers also have their own printf format specifier: %p:

printf("%c is the value storred at %p\n", *a_char_ptr, (void *) a_char_ptr);

要打印实际的内存地址(指针的 actual 值) ,您需要必需将指针转换为通用的 void * 类型。 void指针是一种通用指针,它是一个指针,它不指向它所指向的数据的假设。这是 malloc calloc realloc 通用指针,可以设置为指向任何其他类型...所以什么是 char * ?它是一个通用指针类型,设置为指向大小为1的内存块( sizeof(char))。从某种意义上说,一个类型化的指针是一个派生类型,但是这样想: char * (char *)的缩写void * my_ptr;

To print the actual memory address (the actual value of a pointer), you are required to cast the pointer to the generic void * type. A void pointer is sort of the generic pointer, it's the pointer that makes no assumptions as to the data it is pointing at. This is what malloc, calloc and realloc return, a generic pointer, that can be set to point at any other type... So what is a char *? It's a generic pointer type, set to point at blocks of memory of size 1 (sizeof(char)). In a sense, a typed pointer, then, is a derived type, but think of it like this: char * is short for (char *) void *my_ptr;

但真的,什么是类型?它的要点是一个类型是一个代表特定事物的变量。类型 char 的变量表示一个字符。类型 int 的变量表示整数。这同样适用于指针: char * x 不是类型 char ,它是 char * (指向char的指针)。这意味着 char * x 本身是内存中的一个位置,可以用来读取一个或多个 char 值。

But really, what is a type? The gist of it is that a type is a variable that represents a specific thing. a variable of the type char represents a character. A variable of the type int represents an integer. Same applies to pointers: char *x is not of the type char, it's of the type char * (pointer to char). This means that char *x itself is a location in memory, that we can use to read one or more char values.

我可以等一会儿,但是TL:TR:

I could rant on for a while but TL;TR:

是的,指针是一个数据类型( void * 以其最纯粹的形式)。纯形式是非常不可用的(因为你不能解引用)。而不是必须在每次决定使用它时投入指针,C提供了派生指针类型的便利(如 char * int * 等)。但真的,他们是指针,因此一个数据类型在自己的权利。

Yes, a pointer is a data type (void * in its purest form). The pure form is quite unusable (because you can't dereference it). Instead of having to cast the pointer every time you decide to use it, C offers the convenience of derived pointer types (like char *, int * and so on). But really, they're pointers, and therefore a data-type in their own right.

这篇关于什么是指针变量的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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