matmatmat
August 18th, 2009, 04:36 PM
I am doing (the third?) "[Beginner] Programming Challenge"
#include <stdio.h>
char re[];
void rstart(char a[], int length)
{
//strips an amount of characters [length] from the start of a string
//if the length is 3 then it works (if a[] was "1. Hi" that re becomes "Hi"
int retval;
for(retval = 0; a[retval] != '\0'; retval++) {}//get length of array
int endn = retval - length;
int i2;
int b = 0;
for(i2=0;i2<=retval;i2++){
if (i2 <= endn){
}
else{
re[b]=a[i2];
b++;
}
}
}
int main(int argc, char** argv)
{
printf("h");
FILE *fp;
fp=fopen("bhaarat.txt", "r");
char names[30][30];
int c;
int i = 0;
int i2 = 0;
while ((c = fgetc (fp)) != EOF){
//read file
if (c == '\n'){
printf("c");
i++;
i2 = 0;
}else{
printf("else");
names[i][i2] = c;
++i2;
}
i++;
}
int i3;
for(i3=0; names[i3] != '\0'; i3++){
rstart(names[i3], 3);
}
return 0;
}
when run it gives Segmentation fault
#include <stdio.h>
char re[];
void rstart(char a[], int length)
{
//strips an amount of characters [length] from the start of a string
//if the length is 3 then it works (if a[] was "1. Hi" that re becomes "Hi"
int retval;
for(retval = 0; a[retval] != '\0'; retval++) {}//get length of array
int endn = retval - length;
int i2;
int b = 0;
for(i2=0;i2<=retval;i2++){
if (i2 <= endn){
}
else{
re[b]=a[i2];
b++;
}
}
}
int main(int argc, char** argv)
{
printf("h");
FILE *fp;
fp=fopen("bhaarat.txt", "r");
char names[30][30];
int c;
int i = 0;
int i2 = 0;
while ((c = fgetc (fp)) != EOF){
//read file
if (c == '\n'){
printf("c");
i++;
i2 = 0;
}else{
printf("else");
names[i][i2] = c;
++i2;
}
i++;
}
int i3;
for(i3=0; names[i3] != '\0'; i3++){
rstart(names[i3], 3);
}
return 0;
}
when run it gives Segmentation fault