为什么指针声明中需要数据类型?

时间:2022-07-28 16:29:43

As far as I know about Data type, while declaring a variable, we need to declare its data type, which tells the compiler to reserve the number of bytes in the memory accordingly.

就我所知的数据类型而言,在声明变量时,我们需要声明它的数据类型,它告诉编译器相应地保留内存中的字节数。

But in case of pointers, we know that their size is always of 2 bytes (in Turbo Compiler) always irrespective of the data type of the variable it is pointing.

但是在指针的情况下,我们知道它们的大小总是2个字节(在Turbo编译器中)总是与它指向的变量的数据类型无关。

My question is, if the pointers always take 2 bytes, then what is the need of mentioning the data type while declaring them? OR My understanding about pointers is wrong?

我的问题是,如果指针总是占用2个字节,那么在声明数据类型时,有什么必要提及数据类型呢?或者我对指针的理解是错误的?

8 个解决方案

#1


30  

The Data type is needed when dereferencing the pointer so it knows how much data it should read. For example dereferencing a char pointer should read the next byte from the adress it is pointing to while an int pointer should read 2 bytes.

当取消引用指针时需要数据类型,以便它知道应该读取多少数据。例如,取消对字符指针的引用应该从它所指向的地址读取下一个字节,而int指针应该读取2个字节。

#2


6  

Data type of a pointer is needed in two situations:

在两种情况下需要指针的数据类型:

  1. Deferencing the pointer
  2. 顺从的指针
  3. Pointer arithmetic
  4. 指针的算术

How it is used in dereferencing the pointer?
Consider the following example:

如何在取消指针时使用它?考虑下面的例子:

    {
        char *k; //poniter of type char
        short j=256;
        k=&j;    // Obviously You have to ignore the warnings
        printf("%d",*k)
    }

Now because k is of type char so it will only read one byte. Now binary value of 256 is 0000000100000000 but because k is of type char so it will read only first byte hence the output will be 0.
Note: if we assign j=127 then output will be 127 because 127 will be hold by first byte.

Now come to pointer arithmetic:
Consider the following example:

因为k是char类型,所以它只读取一个字节。现在256的二进制值是0000000100000000但是因为k是char类型的,所以它将只读取第一个字节,因此输出将是0。注意:如果我们分配j=127,那么输出将是127,因为127将被第一个字节保存。现在来看指针算法:考虑以下示例:

    {
        short *ptr;
        short k=0;
        ptr=&k;
        k++;
        ptr++;// pointer arithmetic
    }

Are statements k++ and ptr++ are same thing? No, k++ means k=k+1 and ptr++ means ptr=ptr+2. Because the compiler "knows" this is a pointer and that it points to an short, it adds 2 to ptr instead of 1, so the pointer "points to" the next integer.

For more info refer second chapter of this tutorial.

表述k++ +和ptr++是一回事吗?不,k++意味着k=k+1,而ptr++意味着ptr=ptr+2。因为编译器“知道”这是一个指针,它指向一个短的,它将2添加到ptr而不是1,所以指针“指向”下一个整数。更多信息请参考本教程的第二章。

#3


5  

First of all the size and representation of the pointers themselves aren't always the same for different types. It's just something that happens on many implementations.

首先,指针本身的大小和表示对不同类型来说并不总是相同的。它只是在许多实现中发生的事情。

Second, when using pointers you don't care about the size of the pointers themselves. You need the size of the pointed type.

第二,当使用指针时,您不关心指针本身的大小。您需要尖型的大小。

For example, try this:

例如,试试这个:

int var[5];
char *c = (char *)var;
int  *x = var;

printf("%p\n%p\n", p + 1, x + 1);

You'll see pointer arithmetic strongly depends on the size of the pointed type.

您将看到指针算法非常依赖于指定类型的大小。

#4


5  

The problem is not about pointer size but pointer dereferencing. (wether in C or C++)

问题不在于指针大小,而在于指针去引用。(无论是C还是c++)

Say you have:

你有说:

int* someint;
float* somefloat;

*someint references a memory size of sizeof(int), whereas *somefloat references a memory size of sizeof(float) which are different.

*someint引用的内存大小为sizeof(int),而*somefloat引用的内存大小为sizeof(float),它们是不同的。

#5


2  

Data types are needed simply for type checking.

只需要数据类型来进行类型检查。

#6


1  

What size a pointer needs depends on the system you are using. On a x86_64 system the pointer size might by 64 bit.

指针的大小取决于您使用的系统。在x86_64系统上,指针大小可能是64位。

The reason why you need the data type for pointers is because the compiler has to know what the size of the memory cell is, among others, the pointer is pointing to. Also type safety cannot be ensured w/o the type. Also, you would have to typecast every pointer when accessing structures from the pointer.

之所以需要指针的数据类型,是因为编译器必须知道指针指向的内存单元格的大小。也不能保证类型安全。此外,当您从指针访问结构时,您必须对每个指针进行类型转换。

You also could use a void pointer and do everything by hand. But why should you want that?

您还可以使用一个void指针,并手动执行所有操作。但你为什么要那样做呢?

#7


1  

Assume that this code compiles without error (as you would like):

假设此代码编译无误(如您所愿):

int a;
int b = 42;
void * d = &b;

a = *d;

What should be the value of a?

a的值应该是多少?

Now with this one:

现在这一个:

int a;
float b = 42.0;
void * d = &b;

a = *d;

What do you expect in a?

你对a的期望是什么?

Actually the type specifies how should the pointed area be interpreted. You should specify int * in the first example and float * in the second one, instead of void *.

实际上,类型指定了应该如何解释指定的区域。您应该在第一个示例中指定int *,在第二个示例中指定float *,而不是void *。

#8


0  

it is the concept of a strong typing used in c++. the size of the pointer may be the same but the size of the pointed type may differ. you can always cast a pointer of one type into a pointer of another type however.

这是c++中使用的强类型的概念。指针的大小可能是相同的,但尖型的大小可能不同。但是,您总是可以将一个类型的指针转换为另一个类型的指针。

#1


30  

The Data type is needed when dereferencing the pointer so it knows how much data it should read. For example dereferencing a char pointer should read the next byte from the adress it is pointing to while an int pointer should read 2 bytes.

当取消引用指针时需要数据类型,以便它知道应该读取多少数据。例如,取消对字符指针的引用应该从它所指向的地址读取下一个字节,而int指针应该读取2个字节。

#2


6  

Data type of a pointer is needed in two situations:

在两种情况下需要指针的数据类型:

  1. Deferencing the pointer
  2. 顺从的指针
  3. Pointer arithmetic
  4. 指针的算术

How it is used in dereferencing the pointer?
Consider the following example:

如何在取消指针时使用它?考虑下面的例子:

    {
        char *k; //poniter of type char
        short j=256;
        k=&j;    // Obviously You have to ignore the warnings
        printf("%d",*k)
    }

Now because k is of type char so it will only read one byte. Now binary value of 256 is 0000000100000000 but because k is of type char so it will read only first byte hence the output will be 0.
Note: if we assign j=127 then output will be 127 because 127 will be hold by first byte.

Now come to pointer arithmetic:
Consider the following example:

因为k是char类型,所以它只读取一个字节。现在256的二进制值是0000000100000000但是因为k是char类型的,所以它将只读取第一个字节,因此输出将是0。注意:如果我们分配j=127,那么输出将是127,因为127将被第一个字节保存。现在来看指针算法:考虑以下示例:

    {
        short *ptr;
        short k=0;
        ptr=&k;
        k++;
        ptr++;// pointer arithmetic
    }

Are statements k++ and ptr++ are same thing? No, k++ means k=k+1 and ptr++ means ptr=ptr+2. Because the compiler "knows" this is a pointer and that it points to an short, it adds 2 to ptr instead of 1, so the pointer "points to" the next integer.

For more info refer second chapter of this tutorial.

表述k++ +和ptr++是一回事吗?不,k++意味着k=k+1,而ptr++意味着ptr=ptr+2。因为编译器“知道”这是一个指针,它指向一个短的,它将2添加到ptr而不是1,所以指针“指向”下一个整数。更多信息请参考本教程的第二章。

#3


5  

First of all the size and representation of the pointers themselves aren't always the same for different types. It's just something that happens on many implementations.

首先,指针本身的大小和表示对不同类型来说并不总是相同的。它只是在许多实现中发生的事情。

Second, when using pointers you don't care about the size of the pointers themselves. You need the size of the pointed type.

第二,当使用指针时,您不关心指针本身的大小。您需要尖型的大小。

For example, try this:

例如,试试这个:

int var[5];
char *c = (char *)var;
int  *x = var;

printf("%p\n%p\n", p + 1, x + 1);

You'll see pointer arithmetic strongly depends on the size of the pointed type.

您将看到指针算法非常依赖于指定类型的大小。

#4


5  

The problem is not about pointer size but pointer dereferencing. (wether in C or C++)

问题不在于指针大小,而在于指针去引用。(无论是C还是c++)

Say you have:

你有说:

int* someint;
float* somefloat;

*someint references a memory size of sizeof(int), whereas *somefloat references a memory size of sizeof(float) which are different.

*someint引用的内存大小为sizeof(int),而*somefloat引用的内存大小为sizeof(float),它们是不同的。

#5


2  

Data types are needed simply for type checking.

只需要数据类型来进行类型检查。

#6


1  

What size a pointer needs depends on the system you are using. On a x86_64 system the pointer size might by 64 bit.

指针的大小取决于您使用的系统。在x86_64系统上,指针大小可能是64位。

The reason why you need the data type for pointers is because the compiler has to know what the size of the memory cell is, among others, the pointer is pointing to. Also type safety cannot be ensured w/o the type. Also, you would have to typecast every pointer when accessing structures from the pointer.

之所以需要指针的数据类型,是因为编译器必须知道指针指向的内存单元格的大小。也不能保证类型安全。此外,当您从指针访问结构时,您必须对每个指针进行类型转换。

You also could use a void pointer and do everything by hand. But why should you want that?

您还可以使用一个void指针,并手动执行所有操作。但你为什么要那样做呢?

#7


1  

Assume that this code compiles without error (as you would like):

假设此代码编译无误(如您所愿):

int a;
int b = 42;
void * d = &b;

a = *d;

What should be the value of a?

a的值应该是多少?

Now with this one:

现在这一个:

int a;
float b = 42.0;
void * d = &b;

a = *d;

What do you expect in a?

你对a的期望是什么?

Actually the type specifies how should the pointed area be interpreted. You should specify int * in the first example and float * in the second one, instead of void *.

实际上,类型指定了应该如何解释指定的区域。您应该在第一个示例中指定int *,在第二个示例中指定float *,而不是void *。

#8


0  

it is the concept of a strong typing used in c++. the size of the pointer may be the same but the size of the pointed type may differ. you can always cast a pointer of one type into a pointer of another type however.

这是c++中使用的强类型的概念。指针的大小可能是相同的,但尖型的大小可能不同。但是,您总是可以将一个类型的指针转换为另一个类型的指针。