sizeof

C++ simple sizeof difference between char array and char pointer

こ雲淡風輕ζ 提交于 2019-11-28 11:41:28
问题 char * test = "test"; cout << sizeof(test); char test2[] = "test"; cout << sizeof(test2); Running this on visual studio 2010, why is the output 45 ? Shouldn't test be a string literal and the sizeof a string literal be the number of character elements in the string literal including the terminating null character? 回答1: test is a pointer to a string literal, not a string literal (a char[] ): the sizeof(char*) is 4 , relating to test the sizeof(char[5]) is 5 , relating to test2[] hence 45 is

C/C++: Size of builtin types for various compilers/platforms

冷暖自知 提交于 2019-11-28 11:22:42
问题 Where can I go to get information about the size of, say, unsigned int compiling under gcc for Mac OS X (both 32 and 64 bits)? In general I'd love to have a resource I can go to with a compiler/settings/platform/type and be able to look up how big that type will be. Does anyone know of such a thing? Update: Thanks for all the responses. I was hoping to have something more along the lines of a static table somewhere instead of a piece of code I'd have to write and run on every machine. 回答1: If

Sizeof() on a C++ array works in one function, but not in the other

江枫思渺然 提交于 2019-11-28 09:56:26
问题 I'm trying to learn more about arrays since I'm new to programming. So I was playing with different parts of code and was trying to learn about three things, sizeof() . How do I find the length of an array, and also how do I put arrays into functions (as a parameter)? My code is: #include <iostream> using namespace std; void arrayprint(int inarray[]) { for (int n_i = 0 ; n_i < (sizeof(inarray) / sizeof(int)) ; n_i++) { cout << inarray[n_i] << endl; } } int main() { int n_array[5]; for (int n

浅析strlen()和sizeof()

爷,独闯天下 提交于 2019-11-28 09:38:30
  在 C 语言的学习及找工作时的笔试中,经常会遇到strlen()和 sizeof()这一对让人傻傻分不清的双胞胎。那么这对双胞胎有什么区别呢?   1. strlen()求的是字符串的有效长度,针对的对象是字符串;sizeof()求的是大小,针对的是类型。   2. strlen()是函数,而 sizeof()表面看起来是函数,其本质是关键字。   首先先明确 strlen()是一个函数,而 sizeof()是一个关键字,何以表明? #include<stdio.h> void fun () { printf(“This is fun ()!\n”); } void main() { fun (); }   fun 是一个函数,则在调用函数时,函数名一定要加上括号,即使函数没有任何参数,如果不加括号,则无法调动函数的运算。 #include<stdio.h> void fun() { printf ( "this is fun\n" ); } void main() { fun; //此处虽然不会造成程序编译错误,但不能调动函数 fun()的运行 }   在这个程序中,主函数中的fun表示的是函数fun()的地址,所以函数可以正常变异,但不能调动函数fun()。   若一个名字代表的是函数名,则在调用函数时一定要加上括号,那么sizeof()关键字又有哪些特性,是否跟函数一样

C语言中,strlen和sizeof的区别

北城以北 提交于 2019-11-28 09:38:00
本博客整理自网络,仅供学习参考,如有侵权,联系删除。邮箱:rom100@163.com。 首先strlen是一个函数,sizeof是一个单目运算符。 strlen 它用来计算指定字符串 str 的长度,但不包括结束字符(即 null 字符)。其原型如下面的代码所示: size_t strlen(char const* str); char sArr[] = "ILOVEC"; /*用strlen()求长度*/ printf("sArr的长度=%d\n", strlen(sArr)); 很显然,上面示例代码的运行结果为 6(因为不包括结束字符 null)。 关键字 sizeof 是一个单目运算符,参数可以是数组、指针、类型、对象、函数等,如下面的示例代码所示: char sArr[] = "ILOVEC"; /*用sizeof求长度*/ printf("sArr的长度=%d\n", sizeof(sArr)); 相对于函数 strlen,这里的示例代码运行结果为 7(因为它包括结束字符 null)。同时,对 sizeof 而言,因为缓冲区已经用已知字符串进行了初始化,其长度是固定的,所以 sizeof 在编译时计算缓冲区的长度。也正是由于在编译时计算,因此 sizeof 不能用来返回动态分配的内存空间的大小。 例子: char str[20]="0123456789"; int a

关于 strlen 和 sizeof的区别

橙三吉。 提交于 2019-11-28 09:37:43
关于strlen和sizeof的区别 strlen 和 sizeof 自己一直没有注意去研究,一直迷惑, 凡事还需巨细靡遗啊!巨细靡遗! 巨细靡遗! [strlen] c++ reference 中说: The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself). 意思是: c 的字符串类型的长度是由 空字符 决定的。 一个c字符串类型的长度是从字符串开始到空字符之间的字符数量(不包含空的结束符)。 strlen是函数, 运行时,计算实际的长度。 sizeof sizeof 不是函数,是c语言中判断数据类型长度的关键字, 属于操作符。返回值类型为size_t类型的值。 而size_t 在32位系统是4字节, 在64系统是8个字节。 这样利用该类型可以增强程序的可移植性。大部分程序在编译的时候就把sizeof计算好了 直接上程序更清晰: 举例:

c/c++语言中strlen和sizeof的区别

南楼画角 提交于 2019-11-28 09:31:38
关于sizeof的用法,我已经在 c语言操作符详解 和 c语言32个关键字 中详细说明。此篇不再赘述,看过这两篇博客的人肯定已经知道 sizeof 是一个c语言运算符,它可以计算一个变量或者类型的的字节长度。那么它和 strlen()函数 有何区别? 区别: 为了帮助大家理解上面的表格,举几个例子来说明一下, int main ( ) { char a [ ] = "abc\012abc" ; char b [ ] = "abc\0abc" ; printf ( "sizeof(a)=%d\n" , sizeof ( a ) ) ; //\012该字符串为一个八进制数据 // \0后面跟数据表示一个八进制字符,不跟数据说明为字符串结束符\0 printf ( "sizeof(b)=%d\n" , sizeof ( b ) ) ; printf ( "strlen(a)=%d\n" , strlen ( a ) ) ; printf ( "strlen(b)=%d\n" , strlen ( b ) ) ; return 0 ; } 运行结果: 上面例子要 注意如果\0后面跟数字说明表示该数为一个八进制数的字符,\0后不跟数据说明就是字符串结束符’\0’ ;还有就是当 a定义为字符串时,编译器会默认加上\0 那么我们来分析一下答案: sizeof(a)计算的是a数组的空间大小

C语言sizeof和strlen

て烟熏妆下的殇ゞ 提交于 2019-11-28 09:31:03
1、sizeof数据类型占的内存大小 数据类型 32位系统 (字节) 64位系统(字节) char 1 1 short 2 2 int 4 4 long 4 8 double 8 8 long double 12 16 char *p 4 8 数组(参数) 4 8 //数组是函数参数时相当于指针 数组 数组定义长度 数组定义长度 2、strlen占用内存大小 1)strlen计算字符串的长度,计算到字符串结束字符\0之前为止 char *p="hello world"; len=strlen(p); len的值是11 char *p="hello world\n"; len=strlen(p); len的值是12 2)数组中存储的字符串的大小 char buf[128] = {0}; strcpy(buf,"hello world"); len=strlen(buf); len的值是11 len=sizeof(buf); len的值是128 来源: CSDN 作者: 卫水金波 链接: https://blog.csdn.net/fengjunwang1980/article/details/51811514

经典C语言面试题8:sizeof与strlen的区别

微笑、不失礼 提交于 2019-11-28 09:30:06
一、基本定义 1、sizeof是C/C++中的一个运算符,其作用是返回一个对象或者类型在内存中所占用的字节数。 注意:sizeof后面如果是类型则必须加括号,如 sizeof(char);而如果是变量名则可以不加括号,如 sizeof a; 但是建议使用时 均加上括号。sizeof不能返回动态地被分配的数组的大小。 2、strlen是C语言中的库函数,所在头文件为#include <string.h>其函数原型为unsigned int strlen(char *s); 其中s为指定的字符串。 注意:strlen只能用char *作为参数,它求的是字符串的实际长度,方法是从开始到遇到第一个'\0'结束。 二、几个例子 例1: char str[20] = "0123456789"; int a = strlen(str); /*a = 10*/ int b = sizeof(str);/*b = 20*/ 上面结果为a = 10,这是因为strlen计算的是字符串的实际长度,以第一个'\0'为结束符;b = 20,这是因为sizeof计算的是分配的数组str[20]所占的空间大小,不受里面存储内容的影响。 例2: char *ss = "0123456789"; int i = sizeof(ss); /*i = 4*/ int j = sizeof(*ss); /*j = 1*/