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 5 months ago.
Improve this question
I dont know why this code gives segementation fault(code dumped) is there something wrong with the syntax
#include <stdio.h>
int checksub(char strng){
int a=strlen(strng);
printf("%d",a);
}
int main(){
checksub("twoi");
}
You're passing a char* argument, so you must accept one. Further, you're promising to return int but fail to do so. Third, you've got a single-use variable that's basically irrelevant, so you can simplify to this:
void checksub(char* strng) {
printf("%d", strlen(strng));
}
Where strlen() returns size_t, you'll actually need:
void checksub(char* strng) {
printf("%zu", strlen(strng));
}
--
There's a lot of things your compiler should have warned you about here, so turn on -Wall or equivalent and pay close attention.
Related
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 4 years ago.
Improve this question
I dunno why this doesn't work. the code has a problem with the *c in
charToInt function but should be a legal statement in c. at least so I thought. I am excited to learn something new here.
int charToint(char *c) {
return *c - '0';
}
int main(void) {
char c = '3';
printf("%d\n", charToint(c));
{
You're passing a char to a function that expects a char *. Your compiler should have warned you about this. The value of that char is then interpreted as a pointer value and dereferenced. Dereferencing an invalid pointer invokes undefined behavior, which in this case results in the program crashing.
The function is ultimately trying to work with a char, so change it to accept a char:
int charToint(char c) {
return c - '0';
}
Alternately, you can leave the function as it and pass it a pointer:
printf("%d\n", charToint(&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 4 years ago.
Improve this question
Can someone explain to me why this code give me a seg Fault.?
char cmd[MAXSIZE];
char* args[2];
fgets(cmd, MAXSIZE, stdin);
// read_line(cmd);
char* cmdPt = cmd;
printf(cmdPt[0]);
And then when I:
printf("%c", cmdPt[0])
It doesn't give my seg Fault.
The problem is in printf() statement.
printf(cmdPt[0]); should be printf(cmdPt);
Because printf() first argument is const char *format i.e you should provide char array base address not single char.
Read the compiler warnings properly, you can find yourself solution.
expected ‘const char * restrict’ but argument is of type ‘char’
Better you should use printf() with a format string. As suggested in comments, you should always compile your code with flags -Wall -pedantic-errors It helps you lot.
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 years ago.
Improve this question
code for copying string
#include<stdio.h>
#include<string.h>
int main()
{
char from[100]="we are the people",to[100];
int i,count=0;
puts(from);
//copying string
for(i=0;from[i];i++)
{
to[i]=from[i];
}
to[i]='\0';
//printing the new string
puts[to];
}
why compiler show array subscript is not an integar in this statement ?
puts[to];
but why this does not show error ?
puts[from];
it should be 'puts(to);' I think you mixed up with array and function. '[]' is for array and '()' is for function calling.
Chnage
puts[to]; to puts(to);
puts[to] means you are declaring an array.
[ ] is used for array size declaration.
( ) is used for function calling.
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 9 years ago.
Improve this question
Got xCode 5.0.2 bought mac yesterday and do not understand why this simple code doesn't work.
#include "stdio.h"
int main(){
int N;
printf("vvedite koli4estvo dannih\n");//mistake and warning is here
scanf("%d", &N);
int *arr = new (int [N]);
return 0;
}
mistake is
expected expression
implicit declaration of function 'new' is invalid in c99
Your code is written in C but you are using new; a C++ operator. Use malloc instead.
int *arr = malloc(sizeof(int)*N); // allocates memory for N itegers
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 years ago.
Improve this question
I have written this code:
void f1(void)
{
sprintf("\nf1.");
}
void f2(void)
{
sprintf("\nf2.");
}
void f3(void)
{
sprintf("\nf3.");
}
void f4(void)
{
sprintf("\nf4.");
}
int main()
{
// int i;
void (*f[4])(void);
f[1]=f1;
f[2]=f2;
f[3]=f3;
f[4]=f4;
(*f[1])();
(*f[2])();
(*f[3])();
(*f[4])();
/*for(i=0;i<4;i++)
{
(*f[i])();
}*/
getch();
}
The program compiles, runs, and shows the output
f1.
f2.
f3.
f4.
but when I press enter, it throws the exception stating "Run-Time Check Failure #2 - Stack around the variable 'f' was corrupted."
Please explain to me the reason for this, and also when to use snprintf and sprintf.
Also, let me know why I cannot use
for(i=0;i<4;i++)
{
(*f[i])();
}
for function pointer. It's throwing an error for this as well.
void (*f[4])(void);
f[4]=f4;
Arrays start from 0 in C. Accessing f[4] is illegal.
In addition to your out of bounds array problem, you must have this line:
#include <stdio.h>
because you're using the sprintf function. If you do this, your compiler should give you an error to say you've called it incorrectly. Your compiler has probably warned you that it's used an implicit declaration. As it stands, it's going ahead and calling the function incorrectly and hoping for the best. This may also be causing a stack problem.