Passing a struct through a function in C

  • Thread starter Thread starter mukhiwa
  • Start date Start date
  • Tags Tags
    Function
AI Thread Summary
The discussion revolves around issues with reading data from a text file into a struct and using that data in a function to calculate energy values. Key points include concerns about the struct setup, function prototype correctness, and data passing methods. The struct definition has a flaw with the character array size, and there are questions about the proper way to read data into the struct, particularly regarding the use of array indexing. The initial implementation attempts to read the entire file before processing, which leads to an end-of-file condition that disrupts the data reading loop. Suggestions include removing unnecessary loops and improving clarity in the code structure. After implementing these changes, the user reports that the code functions correctly, leading to the desired output.
mukhiwa
Messages
2
Reaction score
0
Small problem here, need ideas. I'm using a struct to read a text file of the format

4
Atomic Cluster
O -0.028882 -0.317470 0.526009
O 0.887822 0.327691 0.464289
O 0.956723 -0.765947 0.228034
O 0.419663 -0.075427 -0.474913

and then wanting to use the numerical data in a function that I have set up. Trouble is it's not actually passing the numerical values into my function and I'm not getting the desired output (Energy = -6.0 thereabout).

Question: Have i set up my struct correctly?
Question: Is this the correct way: energy(r.x,r.y,r.z);
to pass "substitute" the values in my loop into my function?
Question: Is my function prototype for energy set up correctly?

Many thanks for your help.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int N, i,j,k;
char A[50];
double Energy,Phi, phi;
float T,R;

struct
{
char str[1];
float x;
float y;
float z;
;
}r[4];

void readcluster(void);
double energy(float a, float b, float c);

int main()
{
readcluster();
printf("Energy = %f\n", Phi);
printf("Energy per atom = %f\n", Energy);
return 0;
}

void readcluster(void)
{

double energy(float a, float b, float c);
FILE *fp = fopen("cluster.xyz", "r");
fscanf (fp,"%d\n", &N);
printf("\n%d\n", N);
while(!feof(fp)) {
if(fgets(A, 50, fp)) printf("%s", A);
}
i = 0;

while ((i <= N)&&(!feof(fp))){
fscanf (fp,"%s %f %f %f", &r.str[1], &r.x, &r.y, &r.z);
energy(r.x,r.y,r.z);
printf("O %f %f %f\n", r.x, r.y, r.z);

i++;
}

}


double energy(float a, float b, float c)
{
float XX, YY, ZZ;
for (j=i+1;j<=N;j++){
XX = pow(a - r[j].x,2.0);
YY = pow(b - r[j].y,2.0);
ZZ = pow(c - r[j].z,2.0);
R = sqrt(XX + YY + ZZ);
//printf("R[%d] = %f\n",j,R);
if(R < 0.890899){
phi = 8.0;
}else{
phi = 4*(pow(R,-12.0)-(pow(R,-6.0)));
}
Phi = Phi + phi;
}

Energy = Phi/N;
return 0;
}
 
Technology news on Phys.org
Use code block when posting codes makes it much nicer to read, and much likely for people to read and hence for you to get a reply.

(Relatively quick glance of your code.)

Code:
char str[1];
Just have a char if you're going to have a char array of 1 element.

Code:
void readcluster(void)
{

double energy(float a, float b, float c);
Why do you have a function declaration inside of a function body? :confused:

Code:
while(!feof(fp)) {
    if(fgets(A, 50, fp)) printf("%s", A);
}
i = 0;

while ((i <= N)&&(!feof(fp))){
The first while loop loops until it finds the end of file. The second while loop will hence always evaluates as false as you're at end of file by that point.

Code:
fscanf (fp,"%s %f %f %f", &r[i].str[1], &r[i].x, &r[i].y, &r[i].z);
Use %c after declaring str to be a char. Array is zero-base. You do r[0], r[1], ..., r[N-1] for N elements. Why bother reading in N (the first line of your input file where you indicate how many lines of numbers to read) if you have already assume the answer to be 4 with your code by writing }r[4]; with your struct?

Code:
Energy = Phi/N;
return 0;
If you're not going to return any useful value, i.e. all manipulation done via globals, just declare the function not to return a value (void).
 
Thank you very much for your tips. I've removed while loop
PHP:
 while(!feof(fp)) {
    if(fgets(A, 50, fp)) printf("%s", A);
}
i = 0;

to the single fgets line and it all fine now. Made it a bit more tidy as well. Cheers
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
119
Views
16K
Replies
5
Views
2K
Replies
16
Views
9K
Replies
4
Views
2K
Replies
4
Views
2K
Replies
6
Views
2K
Replies
4
Views
3K
Back
Top