GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jun-2004, 10:33
00rish 00rish is offline
New Member
 
Join Date: Jun 2004
Location: Waterloo/Oakville, ON, Canada
Posts: 4
00rish is on a distinguished road
Question

passing pointers to structure to fns


hi,

this is my first time posting... trying to get this to work for a project im working on at work... it all compiles... im compiling using the gcc cmd on a UNIX system (i dont know what version).. basically within MAIN it all works very nicely.. when i pass the pointer by reference to the fn i cant access what i need to.. any help is greatly appreciated.. :::
CPP / C++ / C Code:
//ive just included what is needed:

struct coords_3d {
     double x, y,r;
};

struct Surface_Array{
     int num_points;
     struct coords_3d *points;
};

struct angle_grid {
     struct Surface_Array *radii;
};


main()
{
     struct angle_grid *depth_array;
     int iang,k;
     FILE *output,*surface;
     char linereading[200];
     float temp_x, temp_y, temp_z;
     double pptxoff, pptyoff, pptzoff;
     
     output=fopen("the filename", "w");
     surface=fopen("suface pts file", "r");   //this file contains a whole bunch of datapts
     
     //usually calculated using steps but given as constants to make understanding and readability easier
     num_circle_steps=72;
     num_rads=28;
     
     //allocate 72 structures that depth_array points to
     depth_array = (struct angle_grid *) malloc(num_circle_steps*sizeof(struct angle_grid));
     
     //go thru each angle and initialise so there are 28 radii pointed to in each of the angle structures
     //then initialise the number of points to be 1 and get a lil space for hte first points, then intialise those points
     for (iang=0; iang<num_circle_steps; iang++)
     {
          (depth_array+iang)->radii = (struct Surface_Array *) malloc((num_rads)*sizeof(struct Surface_Array));

          for(k=0; k<=num_rads-1; k++)
          {
               (depth_array+iang)->radii[k].num_points=1;
               (depth_array+iang)->radii[k].points = (struct coords_3d *) malloc(((depth_array+iang)->radii[k].num_points+1)*sizeof(struct coords_3d));
               (depth_array+iang)->radii[k].points.x= (double) iang*k;
               (depth_array+iang)->radii[k].points.y= (double) iang*k;
               (depth_array+iang)->radii[k].points.r= (double) iang*k;
               
               //this prints out all the CORRECT values to the file OUTPUT, just as a test it proves that everything is initialised correctly
               fprintf(output, "depth_array[%d][%d]   %d   %f %f %f\n", iang,k, (depth_array+iang)->radii[k].num_points, (depth_array+iang)->radii[k].points.x,(depth_array+iang)->radii[k].points.y,(depth_array+iang)->radii[k].points.r);
          }
     }


     if(surface) //if it opened correctly
     {
          while(fgets(linereading,198,surface))
          {
               sscanf(linereading, "%f %f %f", &temp_x, &temp_y, &temp_z);
               put_in_depth_array((double) temp_x-pptxoff, (double) temp_y-pptyoff,(double) temp_z-pptzoff, &depth_array);
          }
     }
}

//*********************************

void
put_in_depth_array(double x, double y, double z, struct angle_grid *array)
{
     int angle_low, rad_low;
     int cur_point;

     //usually found using a few steps here, using constants for easier readability
     angle_low=1;
     rad_low=13;     

     cur_point=(array+angle_low)->radii[rad_low].num_points;
     printf("%d\n", cur_point);   

     //THIS IS WHERE THE ERROR IS... WHEN I OUTPUT THIS.. I GET: 1701670958 as the output.. it should be 1, like it is given above!.. 
     //i need this value so as i need to add more and more points passed to this function (stored in x,y,z) for a specific angle and radii
     //i just get hte number of points, increment it, then REALLOC enough space for the points structure... ie:

     cur_point++;
     (array+angle_low)->radii[rad_low].points= (struct coords_3d *) realloc((array+angle_low)->radii[rad_low].points, (cur_point+1)*sizeof(struct coords_3d));
     (array+angle_low)->radii[rad_low].points[cur_point].x=x;
     (array+angle_low)->radii[rad_low].points[cur_point].y=y;
     (array+angle_low)->radii[rad_low].points[cur_point].r=z;
}
Last edited by JdS : 04-Jun-2004 at 18:41. Reason: Please enclose c code in [c] & [/c] for syntax highlighting
  #2  
Old 04-Jun-2004, 16:54
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
http://www.gidforums.com/t-689.html
__________________
-Aaron
  #3  
Old 05-Jun-2004, 08:13
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi 00rish. Welcome to GIDForums. I haven't compiled the program or anything, so I don't know if this will solve your problem.

Why are you dereferencing your struct when passing here:
CPP / C++ / C Code:
put_in_depth_array((double) temp_x-pptxoff, (double) temp_y-pptyoff,(double) temp_z-pptzoff, &depth_array);

Instead of passing &depth_array, just pass depth_array. I don't know if that will solve your problem for sure.
  #4  
Old 05-Jun-2004, 10:35
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Dsmith is right. One thing you should know about arrays is that they're always passed by reference (aka by pointer). When you pass the name of an array to a function, you're actually passing its base address, because it would be impractical and inefficient to make a copy of the array, especially since the compiler may not know how big it is. So, you do not need to pass it by reference or by pointer, because it's already done for you by the compiler.
__________________
-Aaron
  #5  
Old 07-Jun-2004, 09:32
00rish 00rish is offline
New Member
 
Join Date: Jun 2004
Location: Waterloo/Oakville, ON, Canada
Posts: 4
00rish is on a distinguished road

thanks to all


hey guys,

you are correct.. that was my problem!.. So frustrating as I was stuck on this for a day and a half... I thought I had tried getting rid of the &, but it must have been before when I had some other errors in my code!

Thanks again!
~Rishi
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
strlen error (need help with pointers) mitosis C Programming Language 1 24-Apr-2004 07:50
Help with C++ pointers Mjkramer21 C++ Forum 23 18-Apr-2004 08:53
Passing Pointers To Pointers in Functions elumira C Programming Language 8 05-Mar-2004 22:23
pointers and arrays jack C Programming Language 4 15-Jan-2004 13:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:27.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.