在 c 中获取指针的大小-ag捕鱼王app官网

在 c 中获取指针的大小

作者:迹忆客 最近更新:2023/05/05 浏览次数:

本文演示如何使用 c 编程语言确定指针的大小。


c 编程语言中的指针

称为指针的变量是其值是另一个变量的地址的变量; 换句话说,指针本身包含内存位置的直接地址。 在指针可用于保存变量的位置之前,必须先对其进行声明。

语法:

type *var-name;

这里的type指的是指针的基类型,应该是合法的数据类型,var-name是存放指针的变量名。 请务必注意,用于指示指针的星号 * 与用于乘法的星号相同。

另一方面,这句话中使用星号 * 表示特定变量是指针。 以下是指针可接受的一些不同数据类型声明。

int *integerpointer;
char *characterpointer;
float *floatpointer;
double *doublepointer;

在 c 中使用 sizeof() 方法获取指针的大小

sizeof() 方法只接受一个参数。 它是一个运算符,其值由编译器确定; 因此,它被称为编译时一元运算符。

sizeof() 函数返回无符号整数数据类型。

让我们举一个例子,我们将使用 sizeof() 函数来确定数据类型不同的各种变量的大小。 分别使用数据类型 int、char、float 和 double 为名为 a、b、c 和 d 的四个变量设置初始值。

int a = 10;
char b = 'x';
float c = 10.01;
double d = 10.01;

在初始化名称为 *integerpointer*characterpointer*floatpointer*doublepointer 的四个指针变量后,分别为它们赋值a、b、c 和d。

int *integerpointer = &a;
char *characterpointer = &b;
float *floatpointer = &c;
double *doublepointer = &d;

现在,我们需要一些 printf 命令来输出变量的大小和指针变量。

这些语句需要使用 sizeof 运算符,应该提供给它的参数是 a、b、c、d,以及相应的 *integerpointer*characterpointer*floatpointer*doublepointer

printf("size of (a) = %lu bytes", sizeof(a));
printf("\nsize of (a) pointer = %lu bytes", sizeof(*integerpointer));
printf("\nsize of (b) = %lu bytes", sizeof(b));
printf("\nsize of (b) pointer = %lu bytes", sizeof(*characterpointer));
printf("\nsize of (c) = %lu bytes", sizeof(c));
printf("\nsize of (c) pointer = %lu bytes", sizeof(*floatpointer));
printf("\nsize of (d) = %lu bytes", sizeof(d));
printf("\nsize of (d) pointer = %lu bytes", sizeof(*doublepointer));

使用符号 %lu 是因为 sizeof 方法的结果是无符号整数。

完整的源代码:

#include 
int main() {
    int a = 10;
    char b = 'x';
    float c = 10.01;
    double d = 10.01;
    int *integerpointer = &a;
    char *characterpointer = &b;
    float *floatpointer = &c;
    double *doublepointer = &d;
    printf("size of (a) = %lu bytes", sizeof(a));
    printf("\nsize of (a) pointer = %lu bytes", sizeof(*integerpointer));
    printf("\nsize of (b) = %lu bytes", sizeof(b));
    printf("\nsize of (b) pointer = %lu bytes", sizeof(*characterpointer));
    printf("\nsize of (c) = %lu bytes", sizeof(c));
    printf("\nsize of (c) pointer = %lu bytes", sizeof(*floatpointer));
    printf("\nsize of (d) = %lu bytes", sizeof(d));
    printf("\nsize of (d) pointer = %lu bytes", sizeof(*doublepointer));
    return 0;
}

输出:

size of (a): = 4 bytes
size of (a) pointer: = 4 bytes
size of (b): = 1 bytes
size of (b) pointer: = 1 bytes
size of (c): = 4 bytes
size of (c) pointer: = 4 bytes
size of (d): = 8 bytes
size of (d) pointer: = 8 bytes

在 c 中使用 malloc() 方法获取指针的大小

让我们再看一个指针变量的例子,使用 malloc 技术分配大小。

main() 函数中,初始化一个数据类型为 char 的指针变量,并将其命名为 *cpointer。 分配一个名为 malloc() 的函数,并将 sizeof() 方法作为参数提供给它。

sizeof 方法接受 char * 作为其参数。

int main ()
{
    char *cpointer = malloc(sizeof(char *));
}

此时,我们必须以字节为单位打印 *cpointer 的值。

printf("size of the pointer is %lu bytes.\n", sizeof(*cpointer));

完整的源代码:

#include 
#include 
int main()
{
    char *cpointer = malloc(sizeof(char *));
    printf("size of the pointer is %lu bytes.\n", sizeof(*cpointer));
    return 0;
}

输出:

the size of the pointer is 1 byte.

上一篇:

下一篇:适用于 windows 的 c 编译器

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在c中将整数转换为字符

发布时间:2024/01/03 浏览次数:131 分类:c语言

本教程介绍了在c中将整数转换为字符的不同方法。在c编程语言中,将整数转换为字符在各种情况下都很重要。在c中,字符是以ascii值表示的,因此转换过程相对简单。

发布时间:2023/05/07 浏览次数:364 分类:c语言

本文介绍了如何在 c 语言中使用 typedef enum。使用 enum 在 c 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。

c 语言中的静态变量

发布时间:2023/05/07 浏览次数:167 分类:c语言

本文介绍了如何在 c 语言中使用静态变量。在 c 语言中使用 static 变量在函数调用之间保存变量值

c 语言中生成随机数

发布时间:2023/05/07 浏览次数:158 分类:c语言

本文演示了如何在 c 语言中生成随机数。使用 rand 和 srand 函数在 c 语言中生成随机数

发布时间:2023/05/07 浏览次数:129 分类:c语言

本文演示了如何在 c 语言中使用前缀增量与后缀增量运算符。c 语言中 i 和 i 记号的主要区别

发布时间:2023/05/07 浏览次数:275 分类:c语言

本文演示了如何在 c 语言中获取当前工作目录。使用 getcwd 函数获取当前工作目录的方法

发布时间:2023/05/07 浏览次数:177 分类:c语言

本文介绍了如何在 c 语言中使用位掩码。使用 struct 关键字在 c 语言中定义位掩码数据

发布时间:2023/05/07 浏览次数:212 分类:c语言

本文演示了如何在 c 语言中使用标准库排序函数。使用 qsort 函数对 c 语言中的整数数组进行排序

c 语言中的 extern 关键字

发布时间:2023/05/07 浏览次数:131 分类:c语言

本文介绍了如何在 c 语言中使用 extern 关键字。c 语言中使用 extern 关键字来声明一个在其他文件中定义的变量

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图