string.h

How to inline string.h function on linux?

不打扰是莪最后的温柔 提交于 2020-01-03 15:54:37
问题 I want to optimize some code such that all the functions in string.h will be inlined. I'm on x86_64. I've tried -O3, -minline-all-stringops and when I do "nm a.out" it shows it is calling the glibc version. Checking with gcc -S, I see the calls. What am I missing? There are dozens of #ifdef _SOME_SETTING_ in string.h, and bits/string3.h shows the inline version, but I don't know how to get there. for example: $ cat test.c #include <string.h> main() { char *a, *b; strcpy(b,a); } /* When

Building strcat without the library and without pointers

老子叫甜甜 提交于 2019-12-13 04:22:50
问题 I have been asked to build the strcat from string.h without using the library and pointers. I have this so far but somehow it doesn't work: void strcatO(char a[], char b[]) { int i = 0; for(i = 0; i < strlen(b); ++i) { a[strlen(a) + i + 1] = b[i]; } printf("%s", a); } Output: 回答1: somehow it doesn't work a[strlen(a) + i + 1] = b[i]; appends characters after a 's null character . void strcatO(char a[], char b[]) { int i = 0; for(i = 0; i < strlen(b); ++i) { a[strlen(a) + i + 1] = b[i]; // Oops

Making strcpy function with linked list in c

為{幸葍}努か 提交于 2019-12-12 04:36:19
问题 I was making my own strcpy function using linked list but couldn't get how to do. Without using linked list it could be like this char* cp2014strcpy(char * dest_ptr, const char * src_ptr) { char* strresult = dest_ptr; if((NULL != dest_ptr) && (NULL != src_ptr)) { while (NULL != src_ptr) { *dest_ptr++ = *src_ptr++; } *dest_ptr = NULL; } return strresult; } but I couldn't get how to make strcpy using linked list. 回答1: #include <stdio.h> #include <stdlib.h> typedef struct node { char ch; struct

Find the length of string array with strlen()

走远了吗. 提交于 2019-12-10 17:12:31
问题 I have been trying to find the length of string which has an array of char s with strlen() function but it is not working. The code I am using is something like this: string s[]={"a","b","c"}; int len = strlen(s.c_str()); It produces the following error: "request for member âc_strâ in âwArrayâ, which is of non-class type" But when I have used this strlen() function on string s before like this, it worked fine: fin.open("input.txt"); string tempStr; getline(fin, tempStr,'\n'); int len = strlen

stoi and stoll in c++

别等时光非礼了梦想. 提交于 2019-12-10 09:37:41
问题 I have #include(string) in my declaratives at the top of the program but when I try to run stoi(string) or stoll(string) i get the following error. I am running Cygwin g++ v4.5.3. Z:\G\CSCE 437>g++ convert.cpp -o conv convert.cpp: In function void transfer(std::string*)': convert.cpp:103:36: error: stoll' was not declared in this scope convert.cpp:116:35: error: `stoi' was not declared in this scope fileTime[numRec] = stoll(result[0]); //converts string to Long Long if(numRec = 0){

stoi and stoll in c++

最后都变了- 提交于 2019-12-06 01:16:02
I have #include(string) in my declaratives at the top of the program but when I try to run stoi(string) or stoll(string) i get the following error. I am running Cygwin g++ v4.5.3. Z:\G\CSCE 437>g++ convert.cpp -o conv convert.cpp: In function void transfer(std::string*)': convert.cpp:103:36: error: stoll' was not declared in this scope convert.cpp:116:35: error: `stoi' was not declared in this scope fileTime[numRec] = stoll(result[0]); //converts string to Long Long if(numRec = 0){ beginningTime = fileTime[0]; } fileTime[numRec] = timeDiff; hostName[numRec] = result[1]; diskNum[numRec] = stoi

What is the difference between memcpy() and strncpy() given the latter can easily be a substitute for the former?

ε祈祈猫儿з 提交于 2019-12-04 17:16:40
问题 What is the significant difference between memcpy() and strncpy() ? I ask this because we can easily alter strncpy() to copy any type of data we want, not just characters, simply by casting the first two non- char* arguments to char* and altering the third argument as a multiple of the size of that non-char type. In the following program, I have successfully used that to copy part of an integer array into other, and it works as good as memcpy() . #include <stdio.h> #include <string.h> int

Swap occurrences of two most frequent letters in a string

自古美人都是妖i 提交于 2019-12-04 07:03:53
问题 I don't know what is the problem in my code, but when I compile I get: warning: passing arg 2 of `strcspn' makes pointer from integer without a cast Here is the code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define STR_LEN 50 int main(void) { int i = 0, j = 0, length = 0, count1 = 0, count2 = 0, count3 = 0; char letter3 = 'a', letter2 = 'a', string[STR_LEN] = { 0 }; length = strlen(string); printf("Enter a sentence: "); fgets(string, STR_LEN, stdin); for (i = 0; i < length;

strcmp() return different values for same string comparisons

一曲冷凌霜 提交于 2019-11-28 07:30:23
问题 char s1[] = "0"; char s2[] = "9"; printf("%d\n", strcmp(s1, s2)); // Prints -9 printf("%d\n", strcmp("0", "9")); // Prints -1 Why do strcmp returns different values when it receives the same parameters ? Those values are still legal since strcmp's man page says that the return value of strcmp can be less, greater or equal than 0, but I don't understand why they are different in this example. 回答1: I assume you are using GCC when compiling this, I tried it on 4.8.4. The trick here is that GCC

strupr() and strlwr() in string.h part are of the ANSI standard?

白昼怎懂夜的黑 提交于 2019-11-26 23:17:29
I was looking for this on internet and in every place with the functions of string.h these two are not mentioned. Is because what? They aren't in every compiler? They are non-standard functions from Microsoft's C library. MS has since deprecated them in favor of renamed funcitons _strlwr() and _strupr() : strlwr() doc strupr() doc Note that the MS docs claim they are POSIX functions, but as far as I can tell they never have been. If you need to use them on a non-MS toolchain, they're easy enough to implement. char* strlwr(char* s) { char* tmp = s; for (;*tmp;++tmp) { *tmp = tolower((unsigned