|
 |
|
 |
04-12-2003, 11:26 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2003
Location: Fresno, CA
Posts: 4
|
structure problem
First off, I am not a programmer. I'm in a Geomatics Engineering program that requires us to have some basic programming knowledge. I'll be gratefull for any help I get here, but please dumb it down as much as possible.
Ok, here is my problem:
I am trying to pass an array inside a structure, inside my function I assign some data that I need to use in my main. This is the part where I assign data to the array (this is from inside a loop):
if(cfflag = 1)
{
current.cut[m] = area;
++m;
}
I am currently trying to debug this and in my main function, immediately following the function containing the above code, I find the value is 0, not whatever it was just assigned to be.
I am not that good at this stuff and it's probably something stupid. I don't even know if it's ok to make sturctures this way:
typedef struct AREA
{
double sta;
double cut[10]; // <- can I do this?
double fill[10];
}AREA;
God I hope this makes sense to someone. I can post more of my code if it will help.
Thanks
Ian
edit:
here is the function call and declaration, etc:
call in main:
area_coords(stablk, blksize, ¤t);
header:
void area_coords(DATA stablk[100], int blksize, AREA current);
|
|
|
04-12-2003, 03:51 PM
|
#2 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Netherlands
Posts: 11
|
Yes, it is possible to use an array inside a struct like that. But if you want to test if cfflag equals one, you should use '==' instead of '='.
Maybe if you post the relevant parts of main() and the body of area_coords(), we can take a closer look.
|
|
|
04-13-2003, 05:00 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Apr 2003
Location: Fresno, CA
Posts: 4
|
Sure...
MAIN:
posb=0;posa=0,pts=0, totalpts=0;
while(pts<nptsa+nptsb)
{
blksize = 0;
// Extract stationing into temporary block
exrtact_sta(b, a, stablk, &posb, &posa, &blksize);
pts+= blksize;
// Calc area of cut and fill for the chunk
for(i=0;i<10;++i) //initialize
{
current.cut[i]=0;
current.fill[i]=0;
}
cut=0;fill=0;
area_coords(stablk, blksize, current); //calcs the areas of cut/fill
for(i=0;i<10;++i)
{
cut += current.cut[i];
fill += current.fill[i]=0;
}
// Move current station area data to previous (for computation of next volume)
previous.sta = current.sta;
for(i=0;i<10;++i)
{
previous.cut[i]=0;
previous.fill[i]=0;
}
for(i=0;i<10;++i)
{
previous.cut[i] = current.cut[i];
previous.fill[i] = current.fill[i];
}
}//end for loop
FUNCTION:
void area_coords(DATA stablk[100], int blksize, AREA current)
{
double area, one, two;
int i, j, k, m, n, cfflag;
one=0; two=0, j=0, k=0, m=0,n=0;
// Find first inflection point and calc area to second inflection point
for(i=0;i<blksize;++i)
{
if(stablk[i].flag1==0 && stablk[i].flag2==0)
{
j=i;
if(stablk[i+1].z1>stablk[i+1].z2)
cfflag=1;
else if(stablk[i+1].z1<stablk[i+1].z2)
cfflag=0;
one += stablk[j].off * stablk[j+1].z1;
two += stablk[j+1].off * stablk[j].z1;
++j;
while(stablk[j].flag1!=0 && stablk[j].flag2!=0)
{
one += stablk[j].off * stablk[j+1].z1;
two += stablk[j+1].off * stablk[j].z1;
++j;
}
k=j;
while(j>i)
{
one += stablk[j].off * stablk[j-1].z2;
two += stablk[j-1].off * stablk[j].z2;
--j;
}
area=(one-two)/2;
if(area<0)
area=area*-1;
// Determine if area goes to cut or fill
current.sta = stablk[i].sta;
if(cfflag == 1)
{
current.cut[m] = area;
++m;
}
if(cfflag == 0)
{
current.fill[n] = area;
++n;
}
i=k;
}
// Update i to start calcing area from next infelction point
}
}
|
|
|
04-13-2003, 08:26 PM
|
#4 (permalink)
|
|
Code Monkey
Join Date: Jan 2003
Posts: 57
|
The problem that I see is that you are passing the struct by value. You need to pass by reference.
ie struct_name* _name_
then when you access the struct in the function you need to use pointer reference
_name_->structmember
you might want to read up on that. If I'm wrong someone can correct me.
|
|
|
04-14-2003, 03:55 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
yup you're right Palin, the way the structs are used now, will only make them accessible during the scope of the function, meaning once teh function has been called, the memory used by it can't be reached from the struct parsed to the functioncall.
So use a reference to the struct.
|
|
|
04-21-2003, 12:19 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Apr 2003
Location: Fresno, CA
Posts: 4
|
Thanks guys
It was a structure problem like my friend suggested it might be and was pointed out here.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:14 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|