Passing a struct through a function in C

  • Thread starter mukhiwa
  • Start date
  • Tags
    Function
In summary, the conversation discusses a problem with using a struct to read a text file and pass numerical data into a function. The code provided includes a struct, a function prototype, and a main function. Suggestions are made to improve the code, such as removing unnecessary while loops and making the function return a value.
  • #1
mukhiwa
2
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
  • #2
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);
:yuck: 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).
 
  • #3
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
 

1. What is a struct in C?

A struct in C is a user-defined data type that allows you to combine different data types into a single variable. It can contain variables of different data types, such as integers, floats, and characters, making it a useful tool for organizing and manipulating data.

2. How do you declare a struct in C?

To declare a struct in C, you use the struct keyword followed by the name of the struct, curly braces, and a semicolon. Inside the curly braces, you define the variables and their data types. For example: struct Student { char name[50]; int age; float gpa; };

3. How do you pass a struct as a parameter in a function in C?

To pass a struct as a parameter in a function in C, you can either pass it by value or by reference. When passing by value, a copy of the struct is passed to the function, and any changes made to the struct inside the function will not affect the original struct. When passing by reference, a pointer to the original struct is passed, allowing the function to directly manipulate the struct's values.

4. Can a function return a struct in C?

Yes, a function can return a struct in C. To do so, you would declare the return type of the function as the struct name, and inside the function, you would create a struct variable and assign values to its members before returning it.

5. How do you access struct members in C?

To access struct members in C, you use the dot operator . followed by the member's name. For example, if you have a struct named Person with a member named name, you can access it as Person.name. If you are working with a struct pointer, you can use the arrow operator -> instead. For example, PersonPtr->name.

Similar threads

  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
912
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
4
Replies
119
Views
15K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top