strlen

strlen sometimes equal to sizeof for null-terminated strings

我们两清 提交于 2019-12-25 14:01:07
问题 I know that strlen counts the number of characters up until (and excluding) the null character '\0' (or 0 ) and that sizeof gives the amount of space needed to store the string including the null character, but am confused with the output of my code. Question: I expect the result of strlen to be consistently 1 less than the result of sizeof because my strings are null-terminated, but it only seems to be the case for the string of length 4 and 8, excluding '\0' (i.e. the 3rd and 5th results

Socket网络编程--简单Web服务器(2)

*爱你&永不变心* 提交于 2019-12-24 22:25:09
  上一小节通过阅读开源的Web服务器--tinyhttpd。大概知道了一次交互的请求信息和应答信息的具体过程。接下来我就自己简单的实现一个Web服务器。   下面这个程序只是实现一个简单的框架出来。这次先实现能够Accept客户端的请求。   简单创建web服务器   webserver.h 1 #include <iostream> 2 #include <string> 3 #include <string.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <errno.h> 7 #include <sys/types.h> 8 #include <sys/socket.h> 9 #include <netinet/in.h> 10 #include <arpa/inet.h> 11 #include <unistd.h> 12 #include <pthread.h> 13 #include <thread>//使用c++11的多线程 14 15 using namespace std; 16 17 class WebServer 18 { 19 public: 20 WebServer(); 21 ~WebServer(); 22 int ServerInit(u_short port); 23 int

PHP实现各种经典算法

橙三吉。 提交于 2019-12-24 13:43:52
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 二分查找(数组里查找某个元素) function bin_sch($array, $low, $high, $k){ if ( $low <= $high){ $mid = intval(($low+$high)/2 ); if ($array[$mid] == $k){ return $mid; }elseif ( $k < $array[$mid]){ return bin_sch($array, $low, $mid-1, $k); }else{ return bin_sch($array, $mid+ 1, $high, $k); } } return -1; } 顺序查找(数组里查找某个元素) function seq_sch($array, $n, $k){ $array[$n] = $k; for($i=0; $i<$n; $i++){ if( $array[$i]==$k){ break; } } if ($i<$n){ return $i; }else{ return -1; } } 线性表的删除(数组中实现) function delete_array_element($array , $i) { $len = count($array); for ($j= $i; $j<$len; $j

PHP压缩与解压Zip(PHPZip类)

≯℡__Kan透↙ 提交于 2019-12-24 07:00:06
<?php class PHPZip { private $ctrl_dir = array (); private $datasec = array (); /********************************************************** * 压缩部分 **********************************************************/ // ------------------------------------------------------ // // #遍历指定文件夹 // // $archive = new PHPZip(); // $filelist = $archive->visitFile(文件夹路径); // print "当前文件夹的文件:<p>\r\n"; // foreach($filelist as $file) // printf("%s<br>\r\n", $file); // ------------------------------------------------------ // var $fileList = array (); public function visitFile( $path ) { global $fileList ; $path = str

sizeof 和strlen的区别

只愿长相守 提交于 2019-12-24 06:24:09
一、sizeof sizeof(...) 是运算符,在头文件中typedef 为unsigned int ,其值在编译时即计算好了,参数可以是数组、指针、类型、对象、函数等。 它的功能是:获得保证能容纳实现所建立的最大对象的字节大小。 由于在编译时计算,因此sizeof 不能用来返回动态分配的内存空间的大小。实际上,用sizeof 来返回类型以及静态分配的对象、结构或数组所占的空间,返回值跟对象、结构、数组所存储的内容没有关系。 具体而言,当参数分别如下时,sizeof 返回的值表示的含义如下: 数组—— 编译时分配的数组空间大小; 指针—— 存储该指针所用的空间大小(存储该指针的地址的长度,是长整型,应该为4 ); 类型—— 该类型所占的空间大小; 对象—— 对象的实际占用空间大小; 函数—— 函数的返回类型所占的空间大小。函数的返回类型不能是void 。 二、strlen strlen(...) 是函数,要在运行时才能计算。参数必须是字符型指针(char* )。当数组名作为参数传入时,实际上数组就退化成指针了。 它的功能是:返回字符串的长度。该字符串可能是自己定义的,也可能是内存中随机的,该函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符NULL 。返回的长度大小不包括NULL 。 来源: https://www.cnblogs.com/marvin

Strlen Function behavior on single character

馋奶兔 提交于 2019-12-23 23:08:08
问题 Here is my code: void func(char c) { char * ptr = &c; size_t len = strlen(ptr); printf("len - %d\n", len); } len is always printed as 1. strlen(..) determines the length of a char array by finding the null character ( \0 ) at the end of it. Here ptr is initialized with just the address of a single character ( c ). c does not contain any null characters. How does ptr get the length? 回答1: You cannot use strlen() on a pointer that does not point to a null-terminated array. It invokes undefined

How to determine the length of a string (without using strlen())

陌路散爱 提交于 2019-12-23 22:28:29
问题 size_t stringlength(const char *s) Using this function, how could find the length of a string? I am not referring to using strlen() , but creating it. Any help is greatly appreciated. 回答1: Cycle/iterate through the string, keeping a count. When you hit \0 , you have reached the end of your string. The basic concepts involved are a loop, a conditional (to test for the end of string), maintaining a counter and accessing elements in a sequence of characters. Note : There are more idiomatic

PHP String Length Without strlen()

a 夏天 提交于 2019-12-23 17:07:28
问题 Just browsing over the latest release of the PHP coding standards, and something caught my eye: http://svn.php.net/viewvc/php/php-src/trunk/CODING_STANDARDS?revision=296679&view=markup Coding standard #4 states that "When writing functions that deal with strings, be sure to remember that PHP holds the length property of each string, and that it shouldn't be calculated with strlen()..." I've ALWAYS used strlen, and maybe it's just late, but how do you access the built-in length property of a

strlen in assembly

吃可爱长大的小学妹 提交于 2019-12-23 10:28:09
问题 I made my own implementation of strlen in assembly, but it doesn't return the correct value. It returns the string length + 4. Consequently. I don't see why.. and I hope any of you do... Assembly source: section .text [GLOBAL stringlen:] ; C function stringlen: push ebp mov ebp, esp ; setup the stack frame mov ecx, [ebp+8] xor eax, eax ; loop counter startLoop: xor edx, edx mov edx, [ecx+eax] inc eax cmp edx, 0x0 ; null byte jne startLoop end: pop ebp ret And the main routine: #include <stdio

Warning: strlen() expects parameter 1 to be string, array given [duplicate]

孤街浪徒 提交于 2019-12-23 09:42:17
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: mysql_fetch_array() expects parameter 1 to be resource, boolean given in select I' moving my website to a new host. The previous php version was 5.2 and now is 5.3. After I changed the php version, it shows the warning almost every page: strlen() expects parameter 1 to be string, array given The error line is the third line in this function: function implodestr($arr,$field) { unset($out_str); if (!is_array($arr)