为什么变量指针包含相同数据类型的地址? [英] Why do variable pointers contain the address of the same data type?

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

问题描述

指针声明的常规语法:data-type *pointer_name;

General syntax of pointer declaration: data-type *pointer_name;

指针是一个变量,其值是另一个变量的地址,即存储位置的直接地址.像任何变量或常量一样,必须先声明一个指针,然后才能使用它存储任何变量地址.指针的数据类型必须与指针所指向的变量相同.

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The data type of pointer must be same as the variable, which the pointer is pointing.

为什么指针变量应包含相同数据类型的变量的地址很重要?

Why is it important that a pointer variable should contain the address of a variable of the same data type?

由于指针与另一个变量的值无关,为什么整数指针不能具有float数据类型变量的地址?

As a pointer has nothing to do with the value of another variable, why can't an integer pointer have the address of float data type variable?

正确的形式:

int a = 10;

int a = 10 ;

int * ptr =& a;

int *ptr = &a ;

错误,键入不匹配

float a;

float a;

int * ptr; ptr =&a;

int *ptr; ptr = &a;

推荐答案

因为当您增加指针时

ptr++;

它将指向一个地址,该地址乘以数据类型的大小.如果您这样做

it will point to an address that is one multiplied with the size of the data type. If you do

ptr+=2;

并且数据类型占用4个字节,如果指针被声明为

and the data type is occupying 4 bytes which would be the case if the pointer was declared as

float *ptr;

指针将增加8个字节的位置.

the pointer will increase with 8 byte positions.

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

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