Thread: strucutre as argument in function?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    strucutre as argument in function?

    hello to everyone,

    I would welcome some help. As you may have understand from the code, I would like to make a function which calculate a value a for each old_chrome[i] by using the old_chrome[i].genes[j] parameters.

    Do u have any idea how to do?

    Many thanks on behalf

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    double fit(int i, double old_chrome[i].genes[j]);
    
        main()
    
    {
    
    
      /****DECLARE VARIABLES****/ 
    
      FILE *fpl_0;
      FILE *fpl_1; 
      FILE *fpl_2;
      FILE *fpl_3;
    
      double n1;
      int i, j,n2;
      int k=0;
      int num_of_chromes;
      int num_of_genes;
      char line[100];
      /*****************************/
    
    
    
    
    
      /********READ INPUT DAT..........****/
    
    
      fpl_0=fopen("data_fit", "r");
    
      if (fpl_0==NULL)
        {
          printf("UNABLE TO OPEN INPUT FILE TO READ PROGRAM PARAMETERS\n");
    	exit(1);
        }
    
      while (fgets(line,100,fpl_0)!=NULL)
        {
    
          sscanf(line,"%d %d", &num_of_chromes, &num_of_genes);
        }
    
      fclose(fpl_0);
    
    
    
    
    
         struct chromosome {
    
          double fitness;
          double genes[num_of_genes];         
                            };
    
      struct chromosome temp_chrome[num_of_chromes], old_chrome[num_of_chromes];
    
    
    
      
      fpl_1=fopen("input_fit","r");
    
    
      if (fpl_1==NULL)
        { 
          printf("UNABLE TO OPEN INPUT FILE TO READ GENES DATA\n");
          exit(2);
        } 
    
    
    
      for (i=0;i<num_of_chromes;i++)
    
        {
          for (j=0;j<num_of_genes;j++)
    
    	{
    	  fscanf(fpl_1,"%lf",&old_chrome[i].genes[j]);
            }
    	
        }
    
    
    
      /*
    
      if (fpl_2==NULL)
        { 
          printf("UNABLE TO OPEN INPUT FILE TO READ RANGE  DATA\n");
          exit(3);
        } 
      
    if (fpl_3==NULL)
        { 
          printf("UNABLE TO OPEN INPUT FILE TO READ INIT_VALUE DATA\n");
          exit(4);
        } 
    
    */
    
    
      fclose(fpl_1);
     
      /********************CLOSE INPUT FILES*************/
     
    
    
    
    
      /****************PRINT INPUT DAT..........***************/
    
      printf("*****************INPUT*************************\n");
     
    
      for(i=0;i<num_of_chromes;i++)
        {
          for (j=0;j<num_of_genes;j++)
    
    	{
    	  printf("%d %d %lf\n",i,j,old_chrome[i].genes[j]);
    
    	}      
    
    
      
        }
    
     printf("%lf\n",old_chrome[0].genes[1]+old_chrome[0].genes[2]);
    
    
    
     /* UNTILL HERE READ DATA TO STRUCTURE FROM FILE AND CHECK IF IS OK***/
    
     for (i=0;i<num_of_chromes;i++)
    
       {
         /*   old_chrome[i].fitness=old_chrome[i].genes[1]+old_chrome[i].genes[2];
         printf("the fitness of chromosome %d %lf \n", i,old_chrome[i].fitness);
         */
       
     
     n1=fit(i);
     printf("its not true %lf\n",n1);
    
       }
    
    }
    
    
    
    /*******fitness function******/
    
    
    double fit(int i, double old_chrome[i].genes)
    
    {
      double a;
    
      a= old_chrome[i].genes[0]+old_chrome[i].genes[1];
      return a;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should define the type (the struct definition) before main, and then you can pass variables of that type to other functions.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    thanks

    I did that but still cannot access the old_chrome[i].genes[] parameters
    when I run I take this

    fitness.c: In function ‘fit’:
    fitness.c:173: error: subscripted value is neither array nor pointer
    fitness.c:173: error: subscripted value is neither array nor pointer


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    
    
    
    
    
         struct chromosome {
    
       
          double fitness;
          double genes[3];         
                            };
    
      struct chromosome temp_chrome[4], old_chrome[4];
    
    
    double fit(int i,struct chromosome old_chrome);
    
    
    
    
        main()
    
    {
    
    
      /****DECLARE VARIABLES****/ 
    
      FILE *fpl_0;
      FILE *fpl_1; 
      FILE *fpl_2;
      FILE *fpl_3;
    
      double n1;
      int i, j,n2;
      int k=0;
      int num_of_chromes;
      int num_of_genes;
      char line[100];
      /*****************************/
    
    
    
    
    
      /********READ INPUT DAT..........****/
    
    
      fpl_0=fopen("data_fit", "r");
    
      if (fpl_0==NULL)
        {
          printf("UNABLE TO OPEN INPUT FILE TO READ PROGRAM PARAMETERS\n");
    	exit(1);
        }
    
      while (fgets(line,100,fpl_0)!=NULL)
        {
    
          sscanf(line,"%d %d", &num_of_chromes, &num_of_genes);
        }
    
      fclose(fpl_0);
    
    
    
    
    
    
      
      fpl_1=fopen("input_fit","r");
    
    
      if (fpl_1==NULL)
        { 
          printf("UNABLE TO OPEN INPUT FILE TO READ GENES DATA\n");
          exit(2);
        } 
    
    
    
      for (i=0;i<num_of_chromes;i++)
    
        {
          for (j=0;j<num_of_genes;j++)
    
    	{
    	  fscanf(fpl_1,"%lf",&old_chrome[i].genes[j]);
            }
    	
        }
    
    
    
      /*
    
      if (fpl_2==NULL)
        { 
          printf("UNABLE TO OPEN INPUT FILE TO READ RANGE  DATA\n");
          exit(3);
        } 
      
    if (fpl_3==NULL)
        { 
          printf("UNABLE TO OPEN INPUT FILE TO READ INIT_VALUE DATA\n");
          exit(4);
        } 
    
    */
    
    
      fclose(fpl_1);
     
      /********************CLOSE INPUT FILES*************/
     
    
    
    
    
      /****************PRINT INPUT DAT..........***************/
    
      printf("*****************INPUT*************************\n");
     
    
      for(i=0;i<num_of_chromes;i++)
        {
          for (j=0;j<num_of_genes;j++)
    
    	{
    	  printf("%d %d %lf\n",i,j,old_chrome[i].genes[j]);
    
    	}      
    
    
      
        }
    
     printf("%lf\n",old_chrome[0].genes[1]+old_chrome[0].genes[2]);
    
    
    
     /* UNTILL HERE READ DATA TO STRUCTURE FROM FILE AND CHECK IF IS OK***/
    
     /*for (i=0;i<num_of_chromes;i++)
    
       {
         old_chrome[i].fitness=old_chrome[i].genes[1]+old_chrome[i].genes[2];
         printf("the fitness of chromosome %d %lf \n", i,old_chrome[i].fitness);
         */
       
         /*
         n1=fit(p.genes);
     printf("its not true %lf\n",n1);
         */
       
    }
    
    
    
    /*******fitness function******/
    
    
    double fit(int i, struct chromosome old_chrome)
    
    {
      
    
      double a;
    
      a = old_chrome[i].genes[0]+old_chrome[i].genes[1];
      return a;
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to indent your code properly. You also seem to have too many blank lines.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    struct chromosome {
        double fitness;
        double genes[3];
    };
    
    struct chromosome temp_chrome[4], old_chrome[4];
    
    double fit(int i,struct chromosome old_chrome);
    
    main()
    {
        /****DECLARE VARIABLES****/
    
        FILE *fpl_0;
        FILE *fpl_1;
        FILE *fpl_2;
        FILE *fpl_3;
    
        double n1;
        int i, j,n2;
        int k=0;
        int num_of_chromes;
        int num_of_genes;
        char line[100];
        /*****************************/
    
        /********READ INPUT DAT..........****/
    
        fpl_0=fopen("data_fit", "r");
    
        if (fpl_0==NULL)
        {
            printf("UNABLE TO OPEN INPUT FILE TO READ PROGRAM PARAMETERS\n");
            exit(1);
        }
    
        while (fgets(line,100,fpl_0)!=NULL)
        {
            sscanf(line,"%d %d", &num_of_chromes, &num_of_genes);
        }
    
        fclose(fpl_0);
    
        fpl_1=fopen("input_fit","r");
    
        if (fpl_1==NULL)
        {
            printf("UNABLE TO OPEN INPUT FILE TO READ GENES DATA\n");
            exit(2);
        }
    
        for (i=0;i<num_of_chromes;i++)
        {
            for (j=0;j<num_of_genes;j++)
            {
                fscanf(fpl_1,"%lf",&old_chrome[i].genes[j]);
            }
        }
    
        /*
        if (fpl_2==NULL)
        {
            printf("UNABLE TO OPEN INPUT FILE TO READ RANGE  DATA\n");
            exit(3);
        }
    
        if (fpl_3==NULL)
        {
            printf("UNABLE TO OPEN INPUT FILE TO READ INIT_VALUE DATA\n");
            exit(4);
        }
        */
    
        fclose(fpl_1);
    
        /********************CLOSE INPUT FILES*************/
    
        /****************PRINT INPUT DAT..........***************/
    
        printf("*****************INPUT*************************\n");
    
        for(i=0;i<num_of_chromes;i++)
        {
            for (j=0;j<num_of_genes;j++)
            {
                printf("%d %d %lf\n",i,j,old_chrome[i].genes[j]);
            }
        }
    
        printf("%lf\n",old_chrome[0].genes[1]+old_chrome[0].genes[2]);
    
        /* UNTILL HERE READ DATA TO STRUCTURE FROM FILE AND CHECK IF IS OK***/
    
        /*for (i=0;i<num_of_chromes;i++)
        {
            old_chrome[i].fitness=old_chrome[i].genes[1]+old_chrome[i].genes[2];
            printf("the fitness of chromosome %d %lf \n", i,old_chrome[i].fitness);
            */
    
            /*
            n1=fit(p.genes);
            printf("its not true %lf\n",n1);
            */
        }
    
    /*******fitness function******/
    double fit(int i, struct chromosome old_chrome)
    {
        double a;
        a = old_chrome[i].genes[0]+old_chrome[i].genes[1];
        return a;
    }
    It appears that you are missing a closing brace for the main function, but due to commented out code this problem does not manifest. Frankly, your main function should be broken up into smaller functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're passing the one struct that you want, so you don't need the [i] part AFAICT. (Although I don't see what p is anywhere.)

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    thanks for the replies


    I change the function to the below because what I want to do is to calculate the fit by accessing the genes[] parameters form the main function for each old_chrome[i]. So each of old_chrome[i], will have its own fit calculated. The p is meant to be a pointer to structure


    Code:
    /*******fitness function******/
    
    
    double fit(struct chromosome *p)
    
    {
      
      int i;
      double a;
    
      a = p->genes[0]+p->genes[1];
      return a;
    
    }

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    and how to do that

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    never mind I found it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM