Why do I get a segmentation fault with strcmp [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I believe it is because of the strcmp(). I tried it multiple ways so far and this is just the latest. My objective is to get the index of the array so I can go on to a switch statement to execute code. Any help would be appreciated, although I'm only expecting a "You can't do that."
Big picture is to incorporate this snip of code into a "utility" file that has multiple "functions" and would call it like util("Ping") to execute a Ping and so on...
int main(){
char *cmd = "Ping";
char *names[3]={"Ping","Stop","Go"};
int index = 3;
int i;
char *test;
for (i = 0; i < 44; i++)
{
test = names[i];
if (!strcmp(cmd,test))
{
index = i;
}
}
printf("%s is index of %d\n",cmd,index);}

Why do loop it for 44 times I don't get it. But when I changed 44 to 3 in the loop it works.
for (i = 0; i < 3; i++)
Depends on your compiler though but in case of 44 iteration windows gives me message application not responding.

Related

This code should print i=1 but it is printing i=0 Why is it so? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
#include<studio.h>
int main()
{
int a=5,i;
i!=a>10;
printf("i=%d",i);
return 0;
}
This code should print i=1 but it is printing i=0 Why is it so?
That's because you don't do anything to i.
Your "i!=a>10" evaluates to false, but the result is not stored into a variable.
As it is mentioned in the comments, you need something like this:
int a = 5;
int i = !(a > 10);
The != is mostly used in if-clauses, like
if (a != 0) {...}
I hope this helps. ;)
It's not printing because:
the variable i is not initialized
the second statement should be changed and be a variable int i = !(a > 10)
Returning a value and printing it are different things, but that's not the point of the question.

How to correctly format a "for" loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm quite new to programming and I'm trying to make a simple program with a loop:
#include <stdio.h>
int a = 5;
int b = 0;
int main(void)
{
for (int i = 0; i < a, i++)
{
b++;
}
printf("%i", b);
}
However, when I try to compile I get the errors: relational comparison result unused [-Werror,-Wunused-comparison] and expected ';' in 'for' statement specifier for line 8. I've tried to look at several different sources on how to construct for loops and I just can't see what I'm doing wrong. Any help would be much appreciated.
Here's an example where you can practice interpreting the error statement. As you'll see, it says expected ';' in 'for' statement specificer. That's saying there's a place where you should have a semi-colon but you don't.
In your casse, there should be a semi-colon after the i < a. Right now, you have a comma.
I think you are using , instead of ; in the for loop after the statement.
for (int i = 0; i < a; i++)

I can't understand what's going wrong in this program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
#include<math.h>
#include<stdio.h>
int main(void)
{
int i = 0;
int f = 10000;
int div1 = (powl(10,i));
int temp1 = f/div1;
for(i = 2; temp1 > 1; i++)
{
printf("%i\n",temp1);
}
}
As far as I know, the value of div1 should be 100,1000,10000... With corresponding increments in I. Then temp1 should be 100,10, then loop stops (?). But I get an endless loop of 10000 10000 10000 10000......
Can someone explain what am I doing wrong?
The for loop checks for temp1, but temp1 is not modified in the loop's body. Try putting the desired modification inside the loop's body or as the last expression in the for loop; the variable i is perhaps not necessary at all.
Your for statement should have like this. You missed to call those to statement inside your for loop
for(i = 2; temp1 > 1; i++)
{
div1 = (powl(10,i));
temp1 = f/div1;
printf("%i\n",temp1);
}

How can I insert a new line after 10 characters in C? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm reading data from standard input one character at a time and outputting the results to standard output. Every 10 characters, I want to have a new line. For example:
Input-
123456789101112
Output-
1234567891
01112
This is what I have so far, but it doesn't work.
if(numChars = 10) {
printf("\n");
numChars = 0;
}
#include <windows.h>
int main()
{
char test[] = "123456789101112123133431234567891011121231334312345678910111212313343";
int i = 0;
while (i < strlen(test))
{
printf("%c", test[i++]);
if (i % 10 == 0) {
printf("\n");
}
}
return 0;
}

Stumped: for loop to build an array not working, initial condition ignored? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This one has me stumped. Here is my code to build an array, b[i] of doubles, from 0 to N where N = 126.
int N = 126;
double b[N];
int i;
for(i = 0; i < N; i++);
{
b[i] = (double)i;
printf("b[%lf] = %d\n",b[i], i);
}
For some reason, this is what I get:
b[126.000000] = 126
and nothing else. The initial condition of i being at 0 is ignored, and for some reason it sets i to be the value of N. Strange!
I'm a bit of a c novice so I must be missing something obvious. Any help greatly appreciated!
Andy.
The mistake is at you using the ; at the end of the for loop statement. That is why the program is simply executing the remaining statements as if they are in no loop, and at that time i has become 126.
Remove the ; on the end of the for loop, it is running through the loop without doing anything then executing the body for the last value of i(which is N = 126)

Resources