sizeof和strlen举例

代码:

// SizeofTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "memory.h"
#include "stdlib.h"
#include "string.h"
typedef struct{
 int x;
 int data[100];
 double *pData;
}TestSTR,*pTestSTR;
int main(int argc, char* argv[])
{
    int a=12;
    printf("\nint a=12;sizeof(a)=%d\n",sizeof(a));
    int b[10]={0};
    printf("\nint b[10]={0};sizeof(b)=%d\n",sizeof(b));
    double *p;
    printf("\ndouble *p;sizeof(p)=%d\n",sizeof(p));
    p=new double[200];
    printf("\ndouble *p;p=new double[200];sizeof(p)=%d,sizeof(*p)=%d\n",sizeof(p),sizeof(*p));
    TestSTR varSTR;
    printf("\nstruct sizeof(TestSTR)=%d\n",sizeof(TestSTR));
    printf("\nTestSTR varSTR; sizeof(varSTR)=%d\n",sizeof(varSTR));
    varSTR.pData=new double[200];
    printf("\nTestSTR varSTR; sizeof(varSTR)=%d\n",sizeof(varSTR));
    printf("\nstruct point sizeof(pTestSTR)=%d\n",sizeof(pTestSTR));
    pTestSTR var_pTestSTR;
    var_pTestSTR=&varSTR;
    printf("\npTestSTR var_pTestSTR; sizeof(var_pTestSTR)=%d\n",sizeof(var_pTestSTR));
    printf("\npTestSTR var_pTestSTR; sizeof(*var_pTestSTR)=%d\n",sizeof(*var_pTestSTR));
    memset(varSTR.pData,0,200*sizeof(double));
    printf("\nTestSTR varSTR; sizeof(varSTR)=%d\n",sizeof(varSTR));
    printf("-----------------------------------------------------\n");
    char str[15]="nuptboyzhb";
    char *pChar;
    pChar=str;
    printf("\nstrlen(str)=%d,sizeof(str)=%d\n",strlen(str),sizeof(str));
    printf("\nstrlen(pChar)=%d,sizeof(pChar)=%d\n",strlen(pChar),sizeof(pChar));
    return 0;
}



运行结果:


int a=12;sizeof(a)=4

int b[10]={0};sizeof(b)=40

double *p;sizeof(p)=4

double *p;p=new double[200];sizeof(p)=4,sizeof(*p)=8

struct sizeof(TestSTR)=408

TestSTR varSTR; sizeof(varSTR)=408

TestSTR varSTR; sizeof(varSTR)=408

struct point sizeof(pTestSTR)=4

pTestSTR var_pTestSTR; sizeof(var_pTestSTR)=4

pTestSTR var_pTestSTR; sizeof(*var_pTestSTR)=408

TestSTR varSTR; sizeof(varSTR)=408
-----------------------------------------------------

strlen(str)=10,sizeof(str)=15

strlen(pChar)=10,sizeof(pChar)=4
Press any key to continue


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值