Why does this program segfault
Posted
by Leda
on Stack Overflow
See other posts from Stack Overflow
or by Leda
Published on 2010-05-13T20:31:57Z
Indexed on
2010/05/13
20:34 UTC
Read the original article
Hit count: 168
Upon compiling and running this small program to reverse a string, I get a Segmentation Fault before any output occurs. Forgive me if this is an obvious question, I'm still very new to C.
#include <stdio.h>
int reverse(char string[], int length);
int main() {
char string[] = "reversed";
printf("String at start of main = %s", string);
reverse(string, sizeof(string));
printf("%s\n", string);
return 0;
}
// Reverse string
int reverse(char string[], int length) {
int i;
char reversed[] = {};
int temp;
for(i = 0; i < length; ++i) {
temp = string[i];
reversed[length - i] = temp;
}
return 0;
}
© Stack Overflow or respective owner