Sponsored Content
Top Forums Programming why I am gettin a segmentation fault when using pointers Post 302166838 by user_prady on Wednesday 13th of February 2008 01:01:09 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
UNLOCKED_STDIO(3)					     Linux Programmer's Manual						 UNLOCKED_STDIO(3)

NAME
*_unlocked - non-locking stdio functions SYNOPSIS
#include <stdio.h> int getc_unlocked(FILE *stream); int getchar_unlocked(void); int putc_unlocked(int c, FILE *stream); int putchar_unlocked(int c); #define _BSD_SOURCE /* or _SVID_SOURCE or _GNU_SOURCE */ #include <stdio.h> void clearerr_unlocked(FILE *stream); int feof_unlocked(FILE *stream); int ferror_unlocked(FILE *stream); int fileno_unlocked(FILE *stream); int fflush_unlocked(FILE *stream); int fgetc_unlocked(FILE *stream); int fputc_unlocked(int c, FILE *stream); size_t fread_unlocked(void *ptr, size_t size, size_t n, FILE *stream); size_t fwrite_unlocked(const void *ptr, size_t size, size_t n, FILE *stream); #define _GNU_SOURCE #include <stdio.h> char *fgets_unlocked(char *s, int n, FILE *stream); int fputs_unlocked(const char *s, FILE *stream); #define _GNU_SOURCE #include <wchar.h> wint_t getwc_unlocked(FILE *stream); wint_t getwchar_unlocked(void); wint_t fgetwc_unlocked(FILE *stream); wint_t fputwc_unlocked(wchar_t wc, FILE *stream); wint_t putwc_unlocked(wchar_t wc, FILE *stream); wint_t putwchar_unlocked(wchar_t wc); wchar_t *fgetws_unlocked(wchar_t *ws, int n, FILE *stream); int fputws_unlocked(const wchar_t *ws, FILE *stream); DESCRIPTION
Each of these functions has the same behaviour as its counterpart without the `_unlocked' suffix, except that they do not use locking (they do not set locks themselves, and do not test for the presence of locks set by others) and hence are thread-unsafe. See flockfile(3). CONFORMING TO
The four functions getc_unlocked(), getchar_unlocked(), putc_unlocked(), putchar_unlocked() are in POSIX.1. The nonstandard *_unlocked() variants occur on a few Unix systems, and are available in recent glibc. They should probably not be used. SEE ALSO
flockfile(3) 2001-10-18 UNLOCKED_STDIO(3)
All times are GMT -4. The time now is 07:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy