Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Passing a struct through a function in C

  1. Nov 5, 2007 #1
    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;
    }
     
  2. jcsd
  3. Nov 5, 2007 #2

    KTC

    User Avatar

    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 (Text):
    char str[1];
    Just have a char if you're going to have a char array of 1 element.

    Code (Text):
    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 (Text):
    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 (Text):
    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 (Text):
    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).
     
  4. Nov 8, 2007 #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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Passing a struct through a function in C
Loading...