sizeof和strlen举例

来源:互联网 发布:遗传算法tsp代码 编辑:程序博客网 时间:2024/06/05 20:00

代码:

// 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


原创粉丝点击