sizeof

strlen和sizeof的区别与总结

人走茶凉 提交于 2019-11-28 09:29:49
strlen 是用来计算字符串的长度,遇到第一个NULL('\0')为止,不包括‘\0’。 sizeof 是用来计算变量或者对象、类型所占字节的多少。 首先来看一个例子: char s1[] = "hello"; char* s2 = "hello"; char s3[10] = ''hello"; sizeof(s1) = 5 strlen(s1) = 5 sizeof(s2) = 4 (在32位系统是4,在64系统是8) strlen(s2) = 5 sizeof(s3) = 10 strlen(s3) = 5 上面这个例子,根据strlen和sizeof的定义,难以理解的,大概是sizeof(s1)这个值了。首先要明白 s2是一个指针,对于指针,sizeof所得值,在32位系统是4,在64系统是8 对于数组,sizeof是计算该 数组所占字节数,而不是数组元素个数。 strlen而言,不管是数组还是指针,只要遇到第一个‘\0’就为止,hello字符串是这样的{‘h’, ‘e’,‘l’,‘l’,‘o’,‘\0’}的所以strlen(“hello”) = 5 sizeof可以有这些用法,sizeof(int)、sizeof(2)、sizeof(2+1)、 sizeof(f()) (int f(),f是一个函数,sizeof是返回值类型的大小,返回值是void

c语言strlen()和sizeof()的区别

微笑、不失礼 提交于 2019-11-28 09:29:37
sizeof(type a)输出结果是 type的长度。 strlen()是不包括字符串末尾’\0’的长度。 note: char str[20]=”hello”; str 是char【20】型的 所有占20个字节。 char * p =str;//p是指针,char*型的指针。指向的内容是str字符串。 所以sizeof(p)在32位的操作系统中是4位的,在64位的操作系统中是8位的。 但是strlen(p)是从指针开始的位置向下找 直到’\0’为止的长度。 这里写代码片 #include <stdio.h> #include <stdlib.h> #include <string.h> void test( char *test1) ; int main() { char str [ 100 ]= "/home/princess1/thirdweek/ans3/filefor1/" ; char *p= str; //printf("main中指针char *p的长度%d\n",p) ; printf ( "main中指针 *p的长度%d\n" , sizeof (p)) ; printf ( "main中指针 strlen(p)的长度%d\n" , strlen (p)) ; char str1[ 100 ]; int a= 0 ; int *p_a=&a; printf (

C语言中strlen和sizeof

廉价感情. 提交于 2019-11-28 09:28:59
定义 sizeof是C/C++中的一个运算符(关键字),计算对象或者类型在内存中所占用的字节数。 strlen是C语言中的库函数,计算字符串长度,不包括尾0。 引用头文件#include<stdio.h>。 库函数原型:size_t strlen(const char *s) 注:sizeof是关键字,不是函数。strlen是函数。 举例 char test[20] = “hello”; char *test1 = “hello!”; char test2[] = “hello!!”; strlen(test) //5 strlen计算的是字符串的实际长度,不包括尾0,以第一个’\0’为结束符。 sizeof(test) //20 定义的char型数组,占用了20个字节。 strlen(test1) //6 trlen计算的是字符串的实际长度,不包括尾0,以第一个’\0’为结束符。 sizeof(test1) // 8 计算了指针类型所在的字节数,取决于32/64位机,32位占4字节,64位占8字节。 strlen(test2) //7 strlen计算的是字符串的实际长度,不包括尾0,以第一个’\0’为结束符。 sizeof(test2) // 8 定义的char型数组,占用了8个字节,其中包括了’\0’为结束符。 strlen的返回值为unsigned int类型,为无符号。

C语言中关于 strlen 和 sizeof 的用法及区别(含例题及解析)

本秂侑毒 提交于 2019-11-28 09:28:42
一、前言 首先我们需要知道的是,sizeof既是一个单目操作符,也是一个关键字,其作用是求操作数的 类型长度 (以字节为单位)。 而strlen是一个字符串操作函数,是一个 参数为指针类型 返回值为size_t(unsigned int)的函数,求的是 字符串 的长度。 所以现在我们知道 sizeof是一个求操作数类型长度的操作符(关键字),而strlen是一个求字符串长度的字符串操作函数 。 二、sizeof和strlen的用法 2.1 sizeof操作符在简单变量中的用法 int a = 10; char c = 'c'; printf("%d\n",sizeof(a)); //答案是4(操作数a的类型为整型,32位机器中占4个字节,64位机器中占8个字节) printf("%d\n",sizeof(int)); //答案是4 printf("%d\n",sizeof a); //答案是4(求变量的大小时可以去掉括号) printf("%d\n",sizeof int); //错误(求类型的大小时不能去掉括号) printf("%d\n",sizeof(c)); //答案是1(操作数c的类型为字符型,占1个字节) printf("%d\n",sizeof(char)); //答案是1 printf("%d\n",sizeof c); //答案是1(求变量的大小时可以去掉括号)

C语言中strlen()和sizeof()的区别

假如想象 提交于 2019-11-28 09:28:21
C语言中strlen()和sizeof()的区别 先看下面的一个程序 # include <stdio.h> # include <string.h> # define PRAISE "My name is Bai Jiangwei" int main ( void ) { char name [ 40 ] = "BaiJiangWei" ; printf ( "%u %u \n" , strlen ( name ) , sizeof ( name ) ) ; printf ( "%u %u \n" , strlen ( PRAISE ) , sizeof ( PRAISE ) ) ; } 程序的结果如下 11 40 23 24 Press any key to continue 结论 对于一个字符串变量,strlen()的结果是字符串本身的长度,在本例中BaiJiangWei长度为11;而sizeof(name)的结果是该字符串变量在定义时的长度,在本例中char name[40],故sizeof(name)结果为40 对于一个字符串常量,我们定义时并没有定义其长度,所以strlen()的结果就是字符串本身长度,而sizeof()的结果是strlen()+1,多出来的长度1,实际上就是\0 来源: CSDN 作者: 纪晓岚爱coder 链接: https://blog.csdn

C语言中的strlen与sizeof的区别

我怕爱的太早我们不能终老 提交于 2019-11-28 09:28:03
sizeof与strlen是有着本质的区别,sizeof是求数据类型所占的空间大小,而strlen是求字符串的长度,字符串以/0结尾。区别如下: (1) sizeof是一个C语言中的一个单目运算符,而strlen是一个函数,用来计算字符串的长度。 (2)sizeof求的是数据类型所占空间的大小,而strlen是求字符串的长度 实例1: printf("char=%d/n",sizeof(char)); //1 printf("char*=%d/n",sizeof(char*)); //4 printf("int=%d/n",sizeof(int)); //4 printf("int*=%d/n",sizeof(int*)); //4 printf("long=%d/n",sizeof(long)); //4 printf("long*=%d/n",sizeof(long*)); //4 printf("double=%d/n",sizeof(double)); //8 printf("double*=%d/n",sizeof(double*)); //4 可以看到,char占1个字节,int占4个字节,long点4个字节,而double占8个字节。但 char*,int*,long*,double*都占4个字节的空间。 这是为什么呢? 在C语言中,char,int,long

memcpy(), what should the value of the size parameter be?

删除回忆录丶 提交于 2019-11-28 09:04:21
I want to copy an int array to another int array. They use the same define for length so they'll always be of the same length. What are the pros/cons of the following two alternatives of the size parameter to memcpy()? memcpy(dst, src, ARRAY_LENGTH*sizeof(int)); or memcpy(dst, src, sizeof(dst)); Will the second option always work? Regardless of the content? One thing that favors the last one is that if the array were to change, it'll be some house-keeping to update the memcpy()'s. Thanks Timbo As long as dst is declared as an array with a size, sizeof will return the size of that array in

Do parentheses make a difference when determining the size of an array?

两盒软妹~` 提交于 2019-11-28 08:58:23
The following program prints the same number twice on gcc 4.8.2: #include <stdio.h> int main() { char a[13]; printf("sizeof a is %zu\n", sizeof a ); printf("sizeof(a) is %zu\n", sizeof(a)); } According to this reddit post , gcc is not standard-conformant in this respect, because a parenthesized expression is not on the list of exceptions for when array-to-pointer decay does not happen. Is this guy correct? Here is the relevant standard quote: Except when it is the operand of the sizeof operator or the unary & operator, or is a character string literal used to initialize an array of character

基于Socket的UDP和TCP编程介绍

自古美人都是妖i 提交于 2019-11-28 08:52:12
一、概述   TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议。   TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流,TCP套 接口 是字节流套接口( ST ream socket )的一种。   UDP:用户数据报协议。UDP是一种无连接协议。UDP套接口是数据报套接口(datagram socket)的一种。   二、TCP和UDP介绍   1)基本TCP客户— 服务器 程序设计基本框架   说明:(三路握手)   1.客户端发送一个SYN段(同步序号)指明客户打算连接的服务器端口,以及初始化序号(ISN) 。   2.服务器发回包含服务器的初始序号的SYN报文段作为应答。同时,将确认序号(ACK)设置为客户的ISN加1以对客户的SYN 报文段进行确认。一个SYN将占用一个序号。   3.客户必须将确认序号设置为服务器的ISN加1以对服务器的SYN报文段进行确认。   2) 基本TCP客户—服务器程序设计基本框架流程图   3) UDP和TCP的对比:   从上面的流程图比较我们可以很明显的看出UDP没有三次握手过程。   简单点说。UDP处理的细节比TCP少。UDP不能保证消息被传送到(它也报告消息没有传送到)目的地。UDP也不保证数据包的传送顺序

基于socket的TCP和UDP编程

十年热恋 提交于 2019-11-28 08:51:04
一、概述   TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议。   TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流,TCP套 接口 是字节流套接口( ST ream socket )的一种。   UDP:用户数据报协议。UDP是一种无连接协议。UDP套接口是数据报套接口(datagram Socket )的一种。   二、TCP和UDP介绍   1)基本TCP客户— 服务器 服务器   服务器是指在网络环境下运行相应的应用软件,为网上用户提供共享信息资源和各种服务的一种高性能计算机,英文名称叫做Server。 [全文] 程序设计基本框架   说明:(三路握手)   1.客户端发送一个SYN段(同步序号)指明客户打算连接的 服务器 服务器   服务器是指在网络环境下运行相应的应用软件,为网上用户提供共享信息资源和各种服务的一种高性能计算机,英文名称叫做Server。 端口,以及初始化序号(ISN) 。   2.服务器发回包含服务器的初始序号的SYN报文段作为应答。同时,将确认序号(ACK)设置为客户的ISN加1以对客户的SYN 报文段进行确认。一个SYN将占用一个序号。   3.客户必须将确认序号设置为服务器的ISN加1以对服务器的SYN报文段进行确认。   2) 基本TCP客户