strcmp

using fgets and strcmp in C [duplicate]

拟墨画扇 提交于 2019-12-02 20:13:23
问题 This question already has answers here : strcmp on a line read with fgets (6 answers) Closed 6 years ago . I'm trying to get a string input from the user and then run different functions depending on the input they've entered. For example, say I asked, "What is your favorite fruit?" and I want the program to comment depending on what they enter...I'm not sure how to do this. Here's what I have so far: #include <stdio.h> #include <string.h> char fruit[100]; main() { printf("What is your

SIGSEV on strcmp of memset string

≯℡__Kan透↙ 提交于 2019-12-02 13:34:26
In the following program, i am expecting the for loop to stop after 3 elements. But it keeps on going indefinitely and fails later on with a coredump. is malloc() needed for char*[]` would strcmp fail if i memset to 0? . #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char* str[10]; memset(str,0,10); str[0]=malloc(sizeof(char)*(strlen("Sample")+1)); str[1]=malloc(sizeof(char)*(strlen("Sample")+1)); str[2]=malloc(sizeof(char)*(strlen("Sample")+1)); strcpy(str[0],"Sample"); strcpy(str[1],"Sample"); strcpy(str[2],"Sample"); int i=0; for(i=0;strcmp("",str[i])!=0;i++) { printf(

the if case in this code doesnt work while there is no syntax error

前提是你 提交于 2019-12-02 13:07:36
if(s.name=="kolkata") { printf("the details"); } if(strcmp((s.name,"kolkata")==0) { printf("the details"); } The first 'if' case has no syntax error still it doesn't work,while the second 'if' case does work very efficiently, why? It is not like the first case doesn't work at all, it just works in a way which is not intended . As per the code, if(s.name=="kolkata") is an attempt to compare the pointers themselves. It does not compare the content of the memory location pointer by these pointers. Coming to the point where you were expecting syntax errors , quoting C11 , chapter 6.5.9, the

using fgets and strcmp in C [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-02 11:11:19
This question already has an answer here: strcmp on a line read with fgets 6 answers I'm trying to get a string input from the user and then run different functions depending on the input they've entered. For example, say I asked, "What is your favorite fruit?" and I want the program to comment depending on what they enter...I'm not sure how to do this. Here's what I have so far: #include <stdio.h> #include <string.h> char fruit[100]; main() { printf("What is your favorite fruit?\n"); fgets (fruit, 100, stdin); if (strcmp(fruit, "apple")) { printf("Watch out for worms!\n"); } else { printf(

scanf and strcmp with c string

≡放荡痞女 提交于 2019-12-02 05:05:21
I found a nice example of how to use strcmp, but it's only working with fgets(), and i need to make it work with scanf. So, here's the code: int main(void) { char fruit[] = "apple\n"; char ans[80]; do { printf ("Guess my favorite fruit? "); scanf ("%s",ans); } while (strcmp (fruit, ans) != 0); puts ("Correct answer!"); return 0; } Even when I write the correct answear ("apple") it stays in the loop and keeps asking me what is the favorite fruit... I'm guessing it has something to do with the chars that are not written at ans[80](I need it to be a char array with 80chars at max). I'm not

Fastest way of comparing strings in C

℡╲_俬逩灬. 提交于 2019-12-02 04:53:21
I wanted to know if there are even faster ways of comparing strings in C than using strcmp() , especially when I have to compare a string with multiple pre-defined strings in a switch statement fashion. In my application, the string to be compared can sometimes go as big as 1000 chars, so was just thinking if strcmp() is sufficient enough or if there exists better and efficient way which I am not familiar with. I am actually working on a low power embedded IoT project where more CPU cycles cost power. It doesn't sound as if the problem has as much to do with strcmp itself, as how you use it.

一个关于内联优化和调用约定的Bug

做~自己de王妃 提交于 2019-12-02 03:20:19
很久没有更新博客了(博客园怎么还不更新后台),前几天在写一个Linux 0.11的实验 [1] 时遇到了一个奇葩的Bug,就在这简单记录一下调试过程吧。 现象 这个实验要求在Linux 0.11中实现简单的信号量 [2] ,但在改动内核代码后运行测试程序总是报错,例如: /* pc_test.c */ #define __LIBRARY__ #include <stdio.h> #include <stdlib.h> #include <semaphore.h> #include <unistd.h> _syscall2(long, sem_open, const char *, name, unsigned int, value); _syscall1(int, sem_unlink, const char *, name); int main(void) { sem_t *mutex; if ((mutex = (sem_t *) sem_open("mutex", 1)) == (sem_t *)-1) { perror("opening mutex semaphore creating"); return EXIT_FAILURE; } sem_unlink("mutex"); return EXIT_SUCCESS; } 提示为段错误: 定位 在内核实现信号量的核心代码

Templated Functions.. ERROR: template-id does not match any template declaration

和自甴很熟 提交于 2019-12-01 18:58:45
I have written a function template and an explicitly specialized templated function which simply takes in 3 arguments and calculates the biggest among them and prints it. The specialized function is causing an error,whereas the template works fine. But I want to work with char* type . This is the error I get=> error: template-id ‘Max<>’ for ‘void Max(char, char, char)’ does not match any template declaration Following is my code: template <typename T> void Max(T& a,T& b,T& c) { if(a > b && a >> c) { cout << "Max: " << a << endl; } else if(b > c && b > a) { cout << "Max: " << b << endl; } else

Option Strict On disallows late binding

倖福魔咒の 提交于 2019-12-01 15:47:23
Can someone help me fix this error? Option Strict On disallows late binding Here's the code that's causing the error: Dim SF6StdData As BindingSource = New BindingSource() ' ... If StrComp(SF6StdData.Current("O2AreaCts").ToString, "") = 0 Then AreaCts(3) = 0 Else AreaCts(3) = Convert.ToDouble(SF6StdData.Current("O2AreaCts").ToString) End If I need to rewrite the code so it will not have any errors. I know I could fix this by setting Option Strict to Off in the project properties, but I really don't want to do that. Is there some other way? Late binding is not allowed when Option Strict is on.

compare a character on a char array by using strcmp

守給你的承諾、 提交于 2019-12-01 14:23:46
I would like to use strcmp to find a specific character on a char array. For example, I would like to detect the index number where . is on the text. char host[100] = "hello.world"; size_t i=0; for(i=0;i<strlen(host);i++){ if(strcmp(host[strlen(host)-i], ".")){ printf("%d\n",i); } } however, it outputs "passing argument 1 of 'strcmp' makes pointer from integer without a cast " . I notice that char array is a int, but I could not figure out how I should have passed the char index. Could you please tell me how I should have used the function? This line: if(strcmp(host[strlen(host)-i], ".")){