Click to See Complete Forum and Search --> : Declaring memory for and giving values to grids in a struct using separate functions


arnaudtemme
02-13-2006, 07:19 AM
Guys,

I progressed well after your last comments. Now I am puzzled though. Watch this: (in main)
tillage_parameters till_pars;
declare_tillage_grids(&till_pars, ascii);
sprintf(ch," till_pars: field 10 10 = %d\n",till_pars.tillfields[10][10]);printf(ch);
read_integer(till_pars.fieldfile,till_pars.tillfie lds,ascii);
sprintf(ch," till_pars: field 10 10 = %d\n",till_pars.tillfields[10][10]);printf(ch); --> here the error occursIt gives the old "access violation" error message at runtime that normally would mean that int_grid 'till_pars.tillfields' has had no memory assigned in declare_tillage_grids(&till_pars,ascii). FYI, the typedefs for int_grid and tillage_parameters:
typedef int intmatelems;
typedef intmatelems *int_grid;
struct tillage_parameters
{
int_grid *tillfields;
char fieldfile[30];
};However, I have succesfully assigned memory to int_grid till_pars.tillfields and proved it with the first of the printf statements, which gives as output:till_pars: field 10 10 = 0The zero is the correct (uninitialized) value at this point. So something wrong must be happening in read_integer(till_pars.fieldfile,till_pars.tillfie lds,ascii);, where tillfields is correctly spelled in the code. This is that function:
void read_integer(char *filename, int_grid *intmatrix, geo_parameters ascii){
char f_name[30],str_1[80];
int row, col,
scan_cnt,
numtel,
scan_lon,
num_str;
double scan_do;
bool er_ifile=0;

FILE *p;
strcpy(f_name,filename);
strcat(f_name,".asc" );
p=fopen(f_name,"r" );
er_ifile=1; scan_cnt=0;
if (p==NULL){
(void)printf(" Error: unable to open file %s \n" , f_name);
er_ifile = NULL;
} else {
// code to read the header of the file
}
if( er_ifile != NULL ) {
for(row=0;row<ascii.nr;row++) {
for(col=0;col<ascii.nc;col++) {
scan_do = 0.0; scan_cnt++;
fscanf(p," %lf" ,&num_str);
intmatrix[row][col]= ( num_str );
}
}
}
fclose(p);
} Sorry, I can't completely get the tabs to work for me. My question: what is wrong with read_integer? A sister-function read_double with similar coding performs fine in the same test. Perhaps it is once more something to do with pointers, references and addresses?

evac-q8r
02-13-2006, 07:44 AM
It looks like you have created a pointer to a file given by the variable p, but there isn't anything in this file. You have done everything for this file such as give it a name and an extenstion but never has the file pointed to actual information. So you create a file which is fine but I'm assuming you need to pass the file (as a pointer to a file) with the actual information as an argument to the function (read_integer) and then set p = fileargpointer. So once you arrive to the fscanf within the read_integer function, there was never anything in the file for you to actually scan. That's what it looks like to me. Everything else looks OK though. Hope that helps.

EVAC

arnaudtemme
02-13-2006, 10:09 AM
fscanf(p," %d" ,&num_str); beats fscanf(p," %lf" ,&num_str);Thanks EVAC, you did get me to look at it differently. I knew that passing strings to a function and open files with that worked, so I had to find something else...
Regards,
Arnaud