本文介绍了sizeof的和在C strlen的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  Different从strlen的和sizeof为指针和放的答案;字符串的基于阵列初始化

任何人可以帮助我理解差异的的sizeof 方式和的strlen 方式在C编程?

Can anybody help me in understanding difference between sizeof method and strlen method in c programming?

推荐答案

的strlen()用于获取字符/字符串数组的长度。

strlen() is used to get the length of an array of chars / string.

的sizeof()用于获取字节任何类型的数据的实际大小。

sizeof() is used to get the actual size of any type of data in bytes.

此外,的sizeof()是一个编译时前pression给你一个类型或一个变量的类型的大小。它不关心变量的值。

Besides, sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable.

的strlen()是需要一个指向一个字符,并引导内存从这个角色上,寻找一个NULL字符的功能。它计算的字符数找到的空字符之前。换句话说,它给你一个C风格的空值终止字符串的长度。

strlen() is a function that takes a pointer to a character, and walks the memory from this character on, looking for a NULL character. It counts the number of characters before it finds the NULL character. In other words, it gives you the length of a C-style NULL-terminated string.

这两个几乎是不同的。在C ++中,你并不需要或者非常多,strlen的()是C风格的字符串,这应该由C ++所取代 - 风格的std ::字符串,而主申请的sizeof()在C是作为一个参数的功能,如的malloc()的memcpy() memset的(),所有这些你不应该用C使用++(使用新的, STD: :复制()的std ::填写()构造)。

The two are almost different. In C++, you do not need either very much, strlen() is for C-style strings, which should be replaced by C++-style std::strings, whereas the primary application for sizeof() in C is as an argument to functions like malloc(), memcpy() or memset(), all of which you shouldn't use in C++ (use new, std::copy(), and std::fill() or constructors).

这篇关于sizeof的和在C strlen的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 22:47