strlen

char指针

喜夏-厌秋 提交于 2019-11-27 01:18:36
1、在C语言中,没有字符串类型,因此使用char指针表示字符串。 2、那么问题来了,使用char* 表示字符串,到哪里是结尾呢?因此需要一个特殊的字符作为哨兵,类似迭代器中的end(),这个哨兵就是'\0',注意不是字符0. 3、对于文本字符串,编译器会自动添加\0。 4、对于字符数组的赋值,如果不使用文本字符串,需要用户在尾部添加字符\0。对于其他类型的数组,没有\0的概念。 5、对于char指针,C语言提供了一系列方法,如strlen,strcmp。这些方法有个特点,都是假定最后一个字符为\0。因此对于char指针,我们要保证最后一个字符是\0。 6、考虑sizeof,对于指针,取值为4。对于数组名,虽然数组名也可以认为是指针,但是sizeof(数组名) 取值是整个数组占用的内存,而不是4。思考为什么?   int a[] = {1,3,5,7,}; 我怎么知道a中的元素个数。总不能让我数吧?   我知道每个int占用内存,要想知道元素个数,我需要知道整个数组占用的内存。因此,我需要sizeof(a)返回整个数组占用的内存大小。 7、char指针指向文本字符串,可以认为char指针是指向const对象的指针;char数组名,可以认为是常量指针,不允许修改指向。 8、sizeof(char数组名) :返回整个数组占用多少个字节,包括\0。   sizeof(char指针):返回4

字符串封装

前提是你 提交于 2019-11-27 00:42:20
1、main 调用自定义字符串函数,只需要include “MyString.h”即可 #include "myString.h" void main(){ // myString *str; 这个语句错了,我现在是要创建一个myString的结构体变量,而不是结构体指针!!!! myString str; initString(&str); initWithLength(&str,5); initWithString(&str,"China is great"); // addCharAtEnd(&str,'!'); //addStrAtEnd(&str,"luoxu"); //deleteFirstChar(&str,'i'); deleteFirstStr(&str,"is "); printString(&str); // initWithLength(str,10); } 2、myString.h //自定义字符串函数声明 // // Created by luoxu on 8/10/2019. // #include <stdio.h> #include <stdlib.h> #include <string.h> struct CString{ char *p; //first address of a string int length; //the length

Will strlen be calculated multiple times if used in a loop condition?

半腔热情 提交于 2019-11-27 00:08:24
问题 I'm not sure if the following code can cause redundant calculations, or is it compiler-specific? for (int i = 0; i < strlen(ss); ++i) { // blabla } Will strlen() be calculated every time when i increases? 回答1: Yes, strlen() will be evaluated on each iteration. It's possible that, under ideal circumstances, the optimiser might be able to deduce that the value won't change, but I personally wouldn't rely on that. I'd do something like for (int i = 0, n = strlen(ss); i < n; ++i) or possibly for

Is using strlen() in the loop condition slower than just checking for the null character?

隐身守侯 提交于 2019-11-26 23:34:26
问题 I have read that use of strlen is more expensive than such testing like this: We have a string x 100 characters long. I think that for (int i = 0; i < strlen(x); i++) is more expensive than this code: for (int i = 0; x[i] != '\0'; i++) Is it true? Maybe the second code will not work in some situation so is it better to use the first? Will it be better with the below? for (char *tempptr = x; *tempptr != '\0'; tempptr++) 回答1: for (int i=0;i<strlen(x);i++) This code is calling strlen(x) every

strlen: how does it work?

你说的曾经没有我的故事 提交于 2019-11-26 20:37:22
问题 How does strlen() work internally? 回答1: strlen usually works by counting the characters in a string until a \0 character is found. A canonical implementation would be: size_t strlen (char *str) { size_t len = 0; while (*str != '\0') { str++; len++; } return len; } There is no inherent bug in the function, it works exactly as documented. That's not to say it doesn't have problems, to wit: if you pass it a "string" that doesn't have a \0 at the end, you may run into problems but technically,

strlen not checking for NULL

邮差的信 提交于 2019-11-26 20:23:02
问题 Why is strlen() not checking for NULL? if I do strlen(NULL) , the program segmentation faults. Trying to understand the rationale behind it (if any). 回答1: The rational behind it is simple -- how can you check the length of something that does not exist? Also, unlike "managed languages" there is no expectations the run time system will handle invalid data or data structures correctly. (This type of issue is exactly why more "modern" languages are more popular for non-computation or less

C++笔试题汇总【转载】

痴心易碎 提交于 2019-11-26 18:36:40
转自: http://www.cnblogs.com/wangkangluo1/archive/2011/07/22/2114006.html 一.找错题 试题1: void test1() {   char string [ 10 ];   char * str1 = " 0123456789 " ;  strcpy( string , str1 ); } 试题2: void test2() {   char string [ 10 ],str1[ 10 ];   int i;   for (i = 0 ; i < 10 ; i ++ )  {   str1 = ' a ' ;  }  strcpy( string , str1 ); } 试题3: void test3( char * str1) {   char string [ 10 ];   if ( strlen( str1 ) <= 10 )  {   strcpy( string , str1 );  } } 解答:   试题1字符串str1需要11个字节才能存放下(包括末尾的’\0’),而string只有10个字节的空间,strcpy会导致数组越界;   对试题2,如果面试者指出字符数组str1不能在数组内结束可以给3分;如果面试者指出strcpy(string,str1

strlen - the length of the string is sometimes increased by 1

浪子不回头ぞ 提交于 2019-11-26 16:59:48
问题 I'm doing some C puzzle questions. In most cases, I am able to find the right answer, but with that one I am having problems. I know the right answer by using the compiler, but I don't know the reason. Have a look at the code: char c[] = "abc\012\0x34"; What would strlen(c) return, using a Standard C compiler? My compiler returns 4 when what I expected was 3. What I thought is strlen() would search for the first occurrence of the NULL character but somehow the result is one more than I

linux c 字符串函数 replace indexOf substring 实现

╄→гoц情女王★ 提交于 2019-11-26 16:53:56
c语言没有像java那么丰富的字符串操作函数,很多有用的函数得自己写,搞了一天,写了几个常用函数,留着以后有用。 mystr.c: #include <string.h> #include <stdio.h> /*将str1字符串中第一次出现的str2字符串替换成str3*/ void replaceFirst(char *str1, char *str2, char *str3) { char str4[strlen(str1) + 1]; char *p; strcpy(str4, str1); if ((p = strstr(str1, str2)) != NULL)/*p指向str2在str1中第一次出现的位置*/ { while (str1 != p && str1 != NULL)/*将str1指针移动到p的位置*/ { str1++; } str1[0] = '\0';/*将str1指针指向的值变成/0,以此来截断str1,舍弃str2及以后的内容,只保留str2以前的内容*/ strcat(str1, str3);/*在str1后拼接上str3,组成新str1*/ strcat(str1, strstr(str4, str2) + strlen(str2));/*strstr(str4,str2)是指向str2及以后的内容(包括str2),strstr(str4

C++中的#,##,和&quot;

别说谁变了你拦得住时间么 提交于 2019-11-26 14:04:18
原文地址: https://blog.csdn.net/mzlogin/article/details/40957939 本文主页链接:C++中的#,##,和" 想要灵活应用宏,离不开#和##。 " 在学习#和##之前,先来看一个关于"的例子: #include <stdio.h> #include <string.h> int main() { const char* p1 = "Hello," "World!"; // 一个空格 const char* p2 = "Hello," "World!"; // 多个空格 const char* p3 = "Hello,""World!"; // 没有空格 const char* p4 = "Hello,World!"; // 一个整串 const char* p5 = "Nihao,""Shijie!"; // 一个不同的串 printf("p1 = %s, strlen(p1) = %d\n", p1, strlen(p1)); printf("p2 = %s, strlen(p2) = %d\n", p2, strlen(p2)); printf("p3 = %s, strlen(p3) = %d\n", p3, strlen(p3)); printf("p4 = %s, strlen(p4) = %d\n", p4,