【无标题】

3.8.指针、数组与sizeof运算符

(1) sizeof是C语言的一个运算符(主要sizeof不是函数,虽然用法很像函数),sizeof的作用是用来返回()里面的变量或者数据类型占用的内存字节数。
(2) sizeof存在的价值?主要是因为在不同平台下各种数据类型所占的内存字节数不尽相同(譬如int在32位系统中为4字节,在16位系统中为2字节···)。所以程序中需要使用sizeof来判断当前变量/数据类型在当前环境下占几个字节。

3.8.1、sizeof和strlen

实验1:char str[] = "hello"; sizeof(str), sizeof(str[0]), strlen(str)

#include<stdio.h>
#include <string.h>
int main(void)
{
    char str[] = "hello";
    
    //sizeof(str)、sizeof(str[0])、strlen(str)
    printf("sizeof(str) = %d\n", sizeof(str));          //6
    printf("sizeof(str[0]) = %d\n", sizeof(str[0]));    //1
    printf("strlen(str) = %d\n", strlen(str));          //5
    
    return 0;
}

3.8.2、数组指针和sizeof

实验2:char *p=str; sizeof(p), sizeof(*p), strlen(p)
(1) 32位系统中所有指针的长度都是4,不管是什么类型的指针。
(2) strlen是一个C库函数,用来返回一个字符串的长度(注意,字符串的长度是不计算字符串末尾的’\0’的)。一定要注意strlen接收的参数必须是一个字符串(字符串的特征是以’\0’结尾)

#include<stdio.h>
#include <string.h>
int main(void)
{
    char str[] = "hello";

    char *p = str;
    //sizeof(p), sizeof(*p), strlen(p)
    printf("sizeof(p) = %d\n", sizeof(p));  //在32位OS占4个字节,在64位OS占8个字节
    printf("sizeof(*p) = %d\n", sizeof(*p));//1
    printf("strlen(p) = %d\n", strlen(p));  //5
    
    return 0;
}	

3.8.3、变量类型和变量的长度

实验3:int n=10; sizeof(n)
(1)sizeof测试一个变量本身,和sizeof测试这个变量的类型,结果是一样的。

#include<stdio.h>
#include <string.h>
int main(void)
{
    int n = 10;

    //sizeof(int), sizeof(n)
    printf("sizeof(int) = %d\n", sizeof(int)); //4
    printf("sizeof(n) = %d\n", sizeof(n));     //4
    
    return 0;
}

3.8.4、sizeof(数组名)的长度

实验4:int b[100]; sizeof(b)
(1)sizeof(数组名)的时候,数组名不做左值也不做右值,纯粹就是数组名的含义。那么sizeof(数组名)实际返回的是整个数组所占用内存空间(以字节为单位的)。

#include<stdio.h>
#include <string.h>
int main(void)
{
    int a[100] = {0};
    printf("sizeof(a) = %d\n", sizeof(a)); //100*sizeof(int)=400

    short b[100] = {0};
    printf("sizeof(b) = %d\n", sizeof(b)); //100*sizeof(short)=200

    char c[100] = {0};
    printf("sizeof(c) = %d\n", sizeof(c)); //100*sizeof(char)=100

    double d[100] = {0};
    printf("sizeof(d) = %d\n", sizeof(d)); //100*sizeof(double)=800
    
    return 0;
}

3.8.5、数组和函数传参

void fun(int b[100])
{
	sizeof(b)	   
}					

(1)函数传参,形参是可以用数组的
(2)函数形参是数组时,实际传递是不是整个数组,而是数组的首元素首地址。也就是说函数传参用数组来传,实际相当于传递的是指针(指针指向数组的首元素首地址)。

//很重要的C语言基础
#include<stdio.h>
#include <string.h>

void func(int b[])
{
    //实际计算的是数组首元素首地址的长度,即sizeof(int *)
    printf("sizeof(b) = %ld\n", sizeof(b)); 
    printf("sizeof(int *) = %ld\n", sizeof(int *)); 
}

void func1(int *b)
{
    printf("sizeof(b) = %d\n", sizeof(b)); //计算指针b的长度
}

void func2(int *b, int num)
{
    //在子函数内,b是传进来的数组的指针(首地址)
    //在子函数内,nun是数组元素个数
    printf("sizeof(b) = %d\n", sizeof(b)); //计算指针b的长度
    printf("b = %p\n", b); //打印数组的首元素首地址
    printf("num = %d\n", num);//数组的长度
}

int main(void)
{
    int a[20] = {0};

    func(a); //因为a在函数func内部就是指针,而不是数组

    func1(a); //函数func1和函数func是等同的

    func2(a, sizeof(a)/sizeof(int));
    //sizeof(a)为数组大小, sizeof(a) = 20*sizeof(int)
    //sizeof(a)/sizeof(int)为数组a的元素个数
    printf("a = %p\n", a); //打印数组的首元素首地址
    
    return 0;
}

3.8.6、define和typedef

	#define dpChar char *
	typedef char *tpChar;

	dpChar p1,  p2;			sizeof(p1)	sizeof(p2)
	tpChar p3,  p4;			sizeof(p3)	sizeof(p4)
#include<stdio.h>
#include <string.h>

#define dpChar char*
typedef char * tpChar;

int main(void)
{
    dpChar p1, p2; //展开:char *p1, p2; 相当于char *p1; char p2;
    printf("sizeof(p1) = %d\n", sizeof(p1));
    printf("sizeof(p1) = %d\n", sizeof(p2));

    tpChar p3, p4; //等价于:char *p3; char *p4;
    printf("sizeof(p1) = %d\n", sizeof(p3));
    printf("sizeof(p1) = %d\n", sizeof(p4));
    
    return 0;
}

返回:C语言指针系列目录

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值