pointer to structure example pls


 
Thread Tools Search this Thread
Top Forums Programming pointer to structure example pls
# 1  
Old 02-13-2008
pointer to structure example pls

Dear friends,

First all I applogize for posting such a simple question , as I love this forum and forum participants thats why I am posting my problem here..


Why I am getting error when I remove the commented lines

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

I am getting a segmentation fault Please I am elementary to pointer to structure , so If I am making some mistakes Please suggest me ..

Thanks in advance...

Regards,
Prady
# 2  
Old 02-19-2008
Bug Now I got it

Quote:
Originally Posted by user_prady
Dear friends,

First all I applogize for posting such a simple question , as I love this forum and forum participants thats why I am posting my problem here..


Why I am getting error when I remove the commented lines

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

I am getting a segmentation fault Please I am elementary to pointer to structure , so If I am making some mistakes Please suggest me ..

Thanks in advance...

Regards,
Prady

Now I got it what I wanted to do pointer with structure..
Thanks to everybody..


Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
 
typedef unsigned int U;
struct vec32{
	U c;
	U d;
};

struct vec32 my_struct;
struct vec32 lsts;
struct vec32 vgagain;
struct vec32 dcout1i;
struct vec32 dcout1q;
struct vec32 dcout2i;
struct vec32 dcout2q;
double input_re, input_im;
double adc_input_re, adc_input_im;

void rf_model(double *input_re, double *input_im, struct vec32 *lsts, struct vec32 *vgagain_ptr, struct vec32 *dcout1i,  struct vec32 *dcout1q,  struct vec32 *dcout2i,  struct vec32 *dcout2q , double *adc_input_re, double *adc_input_im);

int main(void) 
{

  FILE *Earf;
  int  ch, line=0;
  char c[100];
  double *input_re_ptr, *input_im_ptr, *adc_input_re_ptr, *adc_input_im_ptr;
  struct vec32 *stptr, *lsts_ptr, *vgagain_ptr, *dcout1i_ptr, *dcout1q_ptr, *dcout2i_ptr, *dcout2q_ptr;

  
  input_re_ptr = &input_re;
  input_im_ptr = &input_im;
  lsts_ptr     = &lsts;
  vgagain_ptr  = &vgagain;
  dcout1i_ptr  = &dcout1i;
  dcout1q_ptr  = &dcout1q;
  dcout2i_ptr  = &dcout2i;
  dcout2q_ptr  = &dcout2q;
  
  adc_input_re_ptr = &adc_input_re;
  adc_input_im_ptr = &adc_input_im;
  
   Earf = fopen("rf_model.dat", "r");
   while (fgets(c,sizeof(c),Earf) != NULL){
      sscanf(c,"%lf %lf %u %u %u %u %u %u %lf %lf ", &input_re, &input_im, &(lsts).d , &(vgagain).d,\
              &(dcout1i).d, &(dcout1q).d, &(dcout2i).d, &(dcout2q).d, &adc_input_re, &adc_input_im );
      rf_model(input_re_ptr, input_im_ptr, lsts_ptr, vgagain_ptr, dcout1i_ptr, dcout1q_ptr, dcout2i_ptr, dcout2q_ptr, adc_input_re_ptr, adc_input_im_ptr);
      
   }
  return 0;
}

void rf_model(double *input_re, double *input_im, struct vec32 *lsts, struct vec32 *vgagain, struct vec32 *dcout1i,  struct vec32 *dcout1q,  struct vec32 *dcout2i,  struct vec32 *dcout2q, double *adc_input_re, double *adc_input_im){

      double lna_gain_lin;
      double lna_high_gain_deci = 17.0;
      double lna_low_gain_deci = -3.0;
      double vga1_gain;
      double vga2_gain;
      double fvgagain;
      double vgagain_lin;
      double dcout1_re, dcout1_im;
      double dcout2_re, dcout2_im;

   /****** translate straight binary format to 2's compensation format ****/
      printf("%lf %lf \n", *adc_input_re, *adc_input_im);
      
      dcout1_re = (double)((*dcout1i).d) -128;
      dcout1_im = (double)((*dcout1q).d) -128;
      dcout2_re = (double)((*dcout2i).d) -128;
      dcout2_im = (double)((*dcout2q).d) -128;
   /*** add offset ***/
     *adc_input_re = *input_re + 512.0;
     *adc_input_im = *input_im - 512.0;
     
  /*** LNA control ***/
       if((*lsts).d == 1){
	           lna_gain_lin = pow(10.0,lna_high_gain_deci/20.0);
		     }
         else{
          	     lna_gain_lin = pow(10.0,lna_low_gain_deci/20.0);
		       }
	  
   *adc_input_re = *adc_input_re * lna_gain_lin;
   *adc_input_im = *adc_input_im * lna_gain_lin;

    /*** VGA control ***/
     fvgagain = (double) ((*vgagain).d);
     if(fvgagain <= 41 ){
       vga1_gain = 0.0;
       vga2_gain = fvgagain;
     }
     else if(fvgagain <= 47){
       vga1_gain = 1.0*6.0;
       vga2_gain = fvgagain -vga1_gain ;
     }
     else if(fvgagain <= 53){
       vga1_gain = 2.0*6.0;
       vga2_gain = fvgagain -vga1_gain ;
     }
     else if(fvgagain <= 59){
       vga1_gain = 3.0*6.0;
       vga2_gain = fvgagain -vga1_gain ;
     }
     else if(fvgagain <= 65){
       vga1_gain = 4.0*6.0;
       vga2_gain = fvgagain -vga1_gain ;
     }
     else if(fvgagain <= 71){
       vga1_gain = 5.0*6.0;
       vga2_gain = fvgagain -vga1_gain ;
     }
     else{
       vga1_gain = 5.0*6.0;
       vga2_gain = fvgagain -vga1_gain ;
     }
 
  vgagain_lin = pow(10.0,(vga1_gain)/20.0);

  *adc_input_re = (*adc_input_re - dcout1_re)*vgagain_lin;
  *adc_input_im = (*adc_input_im - dcout1_im)*vgagain_lin;

  vgagain_lin= pow(10.0,(vga2_gain)/20.0);
  *adc_input_re = (*adc_input_re - dcout2_re)*vgagain_lin* 0.001;
  *adc_input_im = (*adc_input_im - dcout2_im)*vgagain_lin* 0.001;

  /****FINAL output         *******/
  printf("%lf %lf\n ", *adc_input_re, *adc_input_im);
}

Regards,
prady
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

structure pointer array as function parameters

if i create an array of pointers to a structure "struct node" as: struct node *r; and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as: r=multiplty(.......) /*some parameters*/ is... (2 Replies)
Discussion started by: mscoder
2 Replies

2. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

3. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

4. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

5. Programming

Accesing structure member:Error:dereferencing pointer to incomplete type

$ gcc -Wall -Werror struct.c struct.c: In function `main': struct.c:18: error: dereferencing pointer to incomplete type $ cat struct.c #include <stdio.h> #include <stdlib.h> #include <string.h> /*Declaration of structure*/ struct human { char *first; char gender; int age; } man,... (3 Replies)
Discussion started by: amit4g
3 Replies

6. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

7. Programming

address of pointer

Hi i'm new to c programming and i'm trying to change the address of a pointer/variable but i can't seem to get it right, I have this char heap; char *firstFree = heap; char *allocMem( int size ) { void *malloc(size_t sizeofint); /*allocate space for an array with size... (19 Replies)
Discussion started by: Poison Ivy
19 Replies

8. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies

9. Programming

pointer to structures

Dear friends I have a bit basic doubts in pointers and the structures inter relationships. the first one. static struct apvt { int dead; int pending; int abouttograb; }*agents=NULL; what agents pointer is... (1 Reply)
Discussion started by: tech_voip
1 Replies

10. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies
Login or Register to Ask a Question