有关sizeof的使用

#include <stdio.h>
int main()
{
 int a=0;
 int arr[10]={0};
 printf("%d\n",sizeof(a));          //结果为4
 printf("%d\n",sizeof(arr));        //结果为40
 printf("%d\n",sizeof(arr[0]));     //结果为4
 printf("%d\n",sizeof(arr[10]));    //结果为4
 printf("%d\n",sizeof(&arr));       //结果为40
 printf("%d\n",sizeof(&arr[0]));    //结果为4
 printf("%d\n",sizeof(&arr[10]));   //结果为4
 printf("%d\n",sizeof(&a));         //结果为4
 printf("-----%d\n",sizeof(++a));   //结果为-----4
 printf("+++++%d\n",a);             //结果为+++++0
 return 0;
}
时间: 2024-08-03 07:14:27

有关sizeof的使用的相关文章

关于malloc和sizeof的用法

问题1: 1.L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); 2.newbase = (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); 其中L是已经定义的线性表,LIST_INIT_SIZE是线性表存储空间的初始分配量,listsize是当前分配的存储容量(以sizeof(ElemType)为单位) 解释: 第一个句子:用ma

sizeof strlen strncpy

sizeof测类型(数组名除外) strlen测实际长度 strncpy返回指针类型 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 int main() 5 { 6 char *p="wangddd"; 7 printf("%d\n",sizeof(p));//输出4,指针类型 8 9 char x[8]; 10 printf("%d

关于sizeof的那些事

一 结构体 先上代码: struct A { int a; double d; char b; short c; }; 以上代码的sizeof(struct A)为24.分析: a占用4字节,由于d占用8字节,且d需要在8字节处对齐,因此a不仅有4字节存放内容,并且占另外的4个字节使得d位于8字节处.由于c需要在2字节处对齐,所以b占用了2个字节,c占用两个字节,目前总共是20字节,但是整个结构体A根据最大字节对齐,因此为8字节对齐,为了使得类型位struct A的数组的下一个元素处于正确的对齐

C++字符串使用sizeof时注意

char tmp1[20] = {"hello,你好"}; char tmp2[] = {"hello,你好"}; char *tmp3 = new char[20]; sprintf(tmp3,"%s","hello,你好"); string tmp4 = "hello,你好"; printf("%d %d %d %d\n",sizeof(tmp1),sizeof(tmp2),size

含有虚函数的类sizeof大小

#include <iostream> using namespace std; class Base1{ virtual void fun1(){} virtual void fun11(){} public: virtual ~Base1(); }; class Base2{ virtual void fun2(){} }; class DerivedFromOne: public Base2 { virtual void fun2(){} virtual void fun22(){} }

程序猿之---C语言细节24(段错误、类型提升、sizeof &#39;A&#39;)

主要内容:段错误.类型提升.sizeof  'A' #include <stdio.h> int main() { union test{ char a[10]; int b; }u; int *p = (int *)&(u.a[1]); // 没有引起总线错误 *p = 17; printf("%d\n",*p); #if 0 int *q = 0; // 引起段错误,在linux中运行可看到段错误,在windows下运行时直接出错 *q = 1; #endif

sizeof 和类继承 虚继承 求类大小

代码: #include <iostream> using namespace std; /* class a{ float k; // 4字节 virtual void foo(){} //有一个4字节的指针指向自己的虚函数表 }; class b : virtual public a{ virtual void f(){} }; 有这样的一个指针vptr_b_a,这个指针叫虚类指针,也是四个字节:还要包括类a的字节数,所以类b的字节数就求出来了. 运行结果: 8 16 */ /* clas

宽字符std::wstring的长度和大小问题?sizeof(std::wstring)是固定的32,说明std::wstring是一个普通的C++类,而且和Delphi不一样,没有负方向,因为那个需要编译器的支持

std::wstring ws=L"kkkk";    int il=ws.length();    int ia=sizeof(ws);    int ib=sizeof("dddd");    int ic=sizeof(L"kkkk");输出为    il=4,ia=32,ib=5,ic=10为什么ia=32 ?wstring到底对L"kkkk"做了什么? http://www.debugease.com/vc/2171

C++中sizeof(struct)怎么计算?(转)

struct为空时,大小为1. 1. sizeof应用在结构上的情况 请看下面的结构: 1 struct MyStruct 2 { 3 double dda1; 4 char dda; 5 int type; 6 }; 对结构MyStruct采用sizeof会出现什么结果呢?sizeof(MyStruct)为多少呢?也许你会这样求: sizeof(MyStruct)=sizeof(double) sizeof(char) sizeof(int)=13 但是当在VC中测试上面结构的大小时,你会发现

C基础题-sizeof

1.#include "stdio.h" int main() {    char str[] = "hello";    char *s = "hello";    int a[] = {3, 5, 7};    printf("%d\n%d\n%d\n", sizeof(str), sizeof(s), sizeof(a));    return 0;}//输出6, 4, 12sizeof(str)=6   str是数组,