Thread: dynamic array of structs and accessing members of a struct

1. dynamic array of structs and accessing members of a struct

How can i access some members of a struct, which is contained in a dynamic array of structs?
For example : From a N sized dynamic array of structs which contain the real and imaginary parts of a complex number ,find the number with the largest magnitude and print its real and imaginary parts
My problem is : i'm not sure if im using the f1 f2 f3 functions for all the N structs or just the first one.
I dont know how to access the parts of the number with the largest magnitude in main.
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

struct Complex
{
float Re;
float Im;
};

void f1(struct Complex comp)            // Prints 'Imaginary' and Real parts
{
printf("Your complex number is:\n\n    %f + i x %f", comp.Re, comp.Im);
}

float f2(struct Complex comp)          // Finds magnitude
{
double z;

z = sqrt((comp.Re*comp.Re)+(comp.Im*comp.Im));
return z;

}
float f3(struct Complex comp,unsigned int N)   // Finds largest magnitude
{
double z[N];
int i;
for (i=0; i<N; i++)
{
z[i] = f2(comp);
if(z < z[i])
z = z[i];
}
return z;
}
int main ()
{
unsigned int N;
float magnitude;
struct Complex *comp;
printf("Enter N:\n");
scanf("%d",&N);
comp = (struct Complex*)malloc(N*sizeof(struct Complex));
if (comp == NULL)
{
printf("Error! Out of memory");
return 1;
}
srand ( time ( NULL));
(*comp).Re = (float)rand()/RAND_MAX*2.0-1.0;   // Randomly generating the real and imaginary parts (from -1 to 1)
(*comp).Im = (float)rand()/RAND_MAX*2.0-1.0;
magnitude = f3(*comp,N);
printf("\n\nLargest complex number magnitude:\n   %f",magnitude);
return 0;

}
I sear to god this isn't homework . . . . . . 2. If you want to access all allocated structs, have your function receive a pointer to struct. Then you can move through the structs as if it were an array.

A shorthand way of accessing an element via a pointer to struct:

Code:
comp->Re = value;
Don't cast the return value of malloc: FAQ > Casting malloc - Cprogramming.com

f2() and f3() are declared as returning float, but you're returning double. I would suggest just using double instead of float overall. Originally Posted by Default99 I sear to god this isn't homework . . . . . .
Homework questions are fine as long as the user shows effort and does not simply ask for a handout. 3. thanks a lot man ,really helped me out. One more question though: is there a way i can print the number N which shows the position of that struct in the array? (the struct that describes the number with the largest magnitude) 4. Yes, just keep track of the largest value while looping through the structs. If a larger magnitude is found, save this value (for subsequent comparisons) along with the corresponding value of N. 5. Hmm... I'm definitely doing something wrong here, The f3 function is supposed to return the index of the largest magnitude out of the array of magnitudes ,but when i enter a random N at the beginning it always gives back the N i entered. Also , am I giving the f3 function all the elements of the struct array ( comp ) or just the 1st one???
Code:
int f3(struct Complex comp,unsigned int N)   // Finds largest magnitude, returns index{
double m[N];
int i;
for (i=0; i<N; i++)
{
m[i] = f2(comp);        // this is where it needs something else i think
if(m < m[i])
m = m[i];
}

return i;
}
int main ()
{
unsigned int N,i;
struct Complex *comp;
printf("Enter N:\n");
scanf("%d",&N);
comp = (struct Complex*)malloc(N*sizeof(struct Complex));
if (comp == NULL)
{
printf("Error! Out of memory");
return 1;
}
srand ( time ( NULL));
(*comp).Re = (double)rand()/RAND_MAX*2.0-1.0;   // Randomly generating the real and imaginary parts (from -1 to 1)
(*comp).Im = (double)rand()/RAND_MAX*2.0-1.0;
i = f3(*comp,N);      // Every time im using comp in a function does it take only its first element or all its elements?
printf("\n\nThe complex number with the largest magnitude :\n");
f1(*comp);
printf("\nIts assigned number in the array:\n  N = %d",i);
free(comp);

return 0;

} 6. Start with a simple practice program to test passing a struct pointer to a function and traversing the list. Something like this:

Code:
#include <stdio.h>
#include <stdlib.h>

struct stest
{
int x;
};

void stest_load(struct stest *st, int n);
void stest_print(struct stest *st, int n);

int main(void)
{
struct stest *s;
int n = 10;

if((s = malloc( sizeof(*s) * n)) == NULL)
return 1;

stest_print(s, n);

free(s);
return 0;
}

void stest_load(struct stest *st, int n)
{
int i;

for(i = 0; i < n; i++)
st[i].x = i + 1;
}

void stest_print(struct stest *st, int n)
{
int i;

for(i = 0; i < n; i++)
printf("%d\n", st[i].x);
}
You don't need the "double" array, just a single variable that holds the largest current value (and another variable if you want to track the corresponding index). Popular pages Recent additions #include, complex, float, magnitude, number 