Hello Friends,
Here is my programme.
#include <stdio.h>
FILE* fileopen();
void read_line(FILE*);
void read_line(FILE *fh){
char s[200];
double *input_re_see, *input_im_see;
double input_re, input_im;
if( fgets(s,sizeof(s),fh) != NULL) {
sscanf(s,"%lf %lf ", &input_re, &input_im);
// *input_re_see = input_re;
// *input_im_see = input_im;
// printf("%f %f ", *input_re_see, *input_im_see);
printf("input_re= %lf, input_im= %lf ", input_re, input_im );
printf("%s ",s);
}
}
FILE* fileopen(){
void *file = fopen("real.dat", "r");
if(file == NULL){
printf("Error opening file:");
}
return file;
}
int main(void) {
FILE *fh;
int i;
fh = fileopen();
for(i =0 ;i < 50 ; i++){
read_line(fh);
}
return 0;
}
I don't why I am gettin segmentation fault when I remove these comments..
WHat I am doing wrong with the pointer that causes segmentation fault,
Please help..
Regards,
Prady
You must assign the address of the variable to the pointer this way:
input_re_see = &input_re;
instead of:
*input_re_see = input_re;
Google on "pointers tutorial".
Regards
franklin52:
You must assign the address of the variable to the pointer this way:
input_re_see = &input_re;
instead of:
*input_re_see = input_re;
Google on "pointers tutorial".
Regards
Thank you Franklin for encouragement ..
Actually I am learning those stuffs , and
I thought my programme is right bcoz i followed a func in a book
[code]
void swap(float *d1, float *d2){
float temp;
temp = *d1;
*d1 = *d2;
*d2 = *temp;
}
[\code]
Thats why I ve done like that.. Thanks
franklin52:
You must assign the address of the variable to the pointer this way:
input_re_see = &input_re;
instead of:
*input_re_see = input_re;
Google on "pointers tutorial".
Regards
You are right ,,Now I am getting , but the problem again comes when I am trying to use a structure..
I am doing the following
typedef unsigned int U;
typedef struct {U c; U d;} vec32;
FILE* fileopen();
void read_line(FILE *);
void read_line(FILE *fh){
char s[100];
double input_re, input_im, *input_re_tmp, *input_im_tmp;
U lsts, vgagain;
U *lsts_tmp, *vgagain_tmp;
//vec32 *lsts_str, *vgagain_str;
if( fgets(s,sizeof(s),fh) != NULL) {
sscanf(s,"%lf %lf %u %u \n",&input_re, &input_im, &lsts, &vgagain);
input_re_tmp = &input_re;
input_im_tmp = &input_im;
lsts_tmp = &lsts;
vgagain_tmp =&vgagain_tmp;
// lsts_str->d = &lsts;
// vgagain_str->d = &vgagain;
printf(" %f %f %u %u \n ", *input_re_tmp, *input_im_tmp, *lsts_tmp, *vgagain_tmp);
//printf(" %f %f %u %u %u %u %u %u \n ", *input_re_tmp, *input_im_tmp, lsts_str->d, vgagain_str->d);
}
}
FILE* fileopen(){
void *file = fopen("abc.txt", "r");
return file;
}
int main(void) {
FILE *fh;
int i;
fh = fileopen();
for(i =0;i < 5 ; i++){
read_line(fh);
}
return 0;
}
My Input file (abc.txt) is as follows
5.08107 14.1132 0 71
-0.941856 14.9704 0 71
-6.80986 13.3651 0 71
Why I am getting a segmentation fault when I removing the commented lines.. Please I am elementary to pointer to structure , so If I am making some mistakes Please suggest me ..
Thanks in advance...
Regards,
Prady