why I am gettin a segmentation fault when using pointers


 
Thread Tools Search this Thread
Top Forums Programming why I am gettin a segmentation fault when using pointers
# 1  
Old 02-08-2008
Power why I am gettin a segmentation fault when using pointers

Hello Friends,

Here is my programme.
Code:
#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

Last edited by user_prady; 02-08-2008 at 05:27 AM..
# 2  
Old 02-08-2008
You must assign the address of the variable to the pointer this way:

Code:
input_re_see = &input_re;

instead of:

Code:
*input_re_see = input_re;

Google on "pointers tutorial".

Regards
# 3  
Old 02-09-2008
Bug

Quote:
Originally Posted by Franklin52
You must assign the address of the variable to the pointer this way:

Code:
input_re_see = &input_re;

instead of:

Code:
*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
# 4  
Old 02-13-2008
New problem using pointer in structure

Quote:
Originally Posted by Franklin52
You must assign the address of the variable to the pointer this way:

Code:
input_re_see = &input_re;

instead of:

Code:
*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

Code:
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
Code:
 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Programming

Segmentation fault

I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me. For example this code #include <stdio.h> main() { unsigned long a=0; unsigned long b=0; int z; { printf("Enter two... (2 Replies)
Discussion started by: sizzler786
2 Replies

3. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

4. UNIX for Dummies Questions & Answers

Segmentation fault

#include<stdio.h> #include<malloc.h> #include<unistd.h> #include<stdlib.h> void *start_1(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); pthread_exit((void*)1); } void *start_2(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); return (void*)2; }... (2 Replies)
Discussion started by: vincent__tse
2 Replies

5. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

6. Programming

Segmentation fault in C

i have this code int already_there(char *client_names, char *username) { int i; for(i = 0; i<NUM; i++) { printf("HERE\n"); if (strcmp(client_names, username)==0) return(1); } return(0); } and i get a segmentation fault, whats wrong here? (7 Replies)
Discussion started by: omega666
7 Replies

7. UNIX for Dummies Questions & Answers

Segmentation Fault

Hi, While comparing primary key data of two tables thr bteq script I am getting this Error. This script is a shell script. *** Error: The following error was encountered on the output file. Script.sh: 3043492 Segmentation fault(coredump) Please let me know how to get through it. ... (5 Replies)
Discussion started by: monika
5 Replies

8. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies

9. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

10. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies
Login or Register to Ask a Question