Thread: Making integer from pointer without a cast

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    126

    Making integer from pointer without a cast

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define CLASS 4
    #define STUDENT 11
    #define GRADE 5
    
    
    
    float average(float ***grade);//functionprototypes
    void classAverage(float *grade);
    void allClassAverage(float **grade);
    
    int main()
    {
        int  i, j, k;//variable declaration
        float averageGrade=0;
        float grade[4][11][5];
        //subject student grade
    
    
        for(i=0;i<CLASS;i++){
            for(j=0;j<STUDENT;j++){
                for(k=0;GRADE<4;k++){
                    printf("Enter in grade %d for subject %d student %d:\n",k+1, i+1,j+1);//user input for grades
                    scanf("%f", &grade[i][j][k]);;        
                        }
                    }
                }
    
        float*** array;
        array=malloc(CLASS*sizeof(float**));
        
       for (i=0; i<STUDENT; i++){
          array[i]=malloc(STUDENT*sizeof(float*));    
            for (j=0;i<GRADE;j++){
                array[i][j]=malloc(GRADE*sizeof(float));
                                } 
                                }
        averageGrade=average(array); //function call for average grade and prints it out
        printf("%.2f, average for all grades\n", averageGrade);
    
        for(i=0;i<CLASS;i++){
            for(j=0;j<STUDENT;j++){//for loops to pass function for subject average
                classAverage(array[GRADE]);
                    }
                }
    
        for(i=0;i<CLASS;i++){//for loop for subject average
            allClassAverage(array[STUDENT][GRADE]);
                }
    }
    
    
    float average(float ***grade)//function definition
    {
        int i, j, k;
        float gradeAve=0;//var declaration
    
            for(i=0;i<CLASS;i++){//for loops to calculate average grade
                for(j=0;j<STUDENT;j++){
                    for(k=0;k<GRADE;k++){
                        gradeAve+=grade[i][j][k];
                        }
                    }
                }    
        return gradeAve/220.0;//return the gradeave value/220 inputs
    }
    
    void classAverage(float *grade)//function def
    {
        float avesub=0;
        int k;// var dec
    
            for(k=0;k<GRADE;k++){//for loop to calculate class average
                avesub+=grade[k];
                    }        
                
    
        printf("%.2f, average class", avesub);//print class ave
    
    }
    
    void allClassAverage(float **grade)//func def
    {
        int j, k;
        float donkeypunch=0;
         static int i=1;//var dec
                for(j=0;j<STUDENT;j++){//nested for to determine both class ave
                    for(k=0;k<GRADE;k++){
                        donkeypunch+=grade[j][k];
                            }
                        }
        printf("%.2f, subject average for student %d\n", (donkeypunch/55), i++);//print sub aves
    }
    Giving me that error on 75:10
    avesub+=grade[k];
    and 90:17
    donkeypunch+=grade[j][k];

    not sure exactly why
    Last edited by Sorinx; 12-15-2013 at 11:21 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: You might read this link Three Star Programmer

    Using three stars is almost always a wrong thing to do.

    I suggest using these prototypes; been a while since I did multi dim array so I might have made a mistake.
    Note: This is just a guess on my part; because your code makes little sense to me.
    I guessed you want to pass the complete 3 dim array; if this is NOT true them you need to decide what is true and tell us.
    Code:
    float average(int grade[CLASS][STUDENT][GRADE]);
    void classAverage(int grade[CLASS][STUDENT][GRADE]);
    void allClassAverage(int grade[CLASS][STUDENT][GRADE]);
    Tim S.
    Last edited by stahta01; 12-15-2013 at 11:03 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Supposed to malloc an array, populate it then pass only parts of it to the functions to get the averages needed.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Sorinx View Post
    Supposed to malloc an array, populate it then pass only parts of it to the functions to get the averages needed.
    You really need to pass only part of it!

    You will NOT be needing to use three stars then! (except maybe where you declare array)

    Edit: Decide what you need to pass to each function then document it!

    Then pass it.

    I suggest at least changing the three stars all to two till you figure out what you need to do.

    Edit: And stop using magic numbers! You define constant macro use them.

    Edit: I am giving up helping you for two reasons.
    I am weak on doing malloc for arrays and I have no idea what any of your functions are supposed to do.
    The use of magic numbers and poor variable names make it nearly impossible for me to guess.

    Tim S.
    Last edited by stahta01; 12-15-2013 at 11:36 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Ok I made some dumb errors I fixed them but the error is still in those 2 lines

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define CLASS 4
    #define STUDENT 11
    #define GRADE 5
    
    
    
    float average(float ***grade);//functionprototypes
    void classAverage(float **grade);
    void allClassAverage(float *grade);
    
    int main()
    {
        int  i, j, k;//variable declaration
        float averageGrade=0;
        float grade[4][11][5];
        //subject student grade
    
    
        for(i=0;i<CLASS;i++){
            for(j=0;j<STUDENT;j++){
                for(k=0;GRADE<4;k++){
                    printf("Enter in grade %d for subject %d student %d:\n",k+1, i+1,j+1);//user input for grades
                    scanf("%f", &grade[i][j][k]);;        
                        }
                    }
                }
    
        float*** array;
        array=malloc(CLASS*sizeof(float**));
        
       for (i=0; i<STUDENT; i++){
          array[i]=malloc(STUDENT*sizeof(float*));    
            for (j=0;i<GRADE;j++){
                array[i][j]=malloc(GRADE*sizeof(float));
                                } 
                                }
        averageGrade=average(array); //function call for average grade and prints it out
        printf("%.2f, average for all grades\n", averageGrade);
    
        for(i=0;i<CLASS;i++){
            for(j=0;j<STUDENT;j++){//for loops to pass function for subject average
                classAverage(array[GRADE]);
                    }
                }
    
        for(i=0;i<CLASS;i++){//for loop for subject average
            allClassAverage(array[STUDENT][GRADE]);
                }
    }
    
    
    float average(float ***grade)//function definition
    {
        int i, j, k;
        float gradeAve=0;//var declaration
    
            for(i=0;i<CLASS;i++){//for loops to calculate average grade
                for(j=0;j<STUDENT;j++){
                    for(k=0;k<GRADE;k++){
                        gradeAve+=grade[i][j][k];
                        }
                    }
                }    
        return gradeAve/220.0;//return the gradeave value/220 inputs
    }
    
    void classAverage(float **grade)//function def
    {
        float avesub=0;
        int k;// var dec
    
            for(k=0;k<GRADE;k++){//for loop to calculate class average
                avesub+=grade[k];
                    }        
                
    
        printf("%.2f, average class", avesub);//print class ave
    
    }
    
    void allClassAverage(float *grade)//func def
    {
        int j, k;
        float donkeypunch=0;
         static int i=1;//var dec
                for(j=0;j<STUDENT;j++){//nested for to determine both class ave
                    for(k=0;k<GRADE;k++){
                        donkeypunch+=grade[j][k];
                            }
                        }
        printf("%.2f, subject average for student %d\n", (donkeypunch/55), i++);//print sub aves
    }


    invalid operands to binary 75:10

    90:27

    subscripted value is neither array nor pointer nor value
    Last edited by Sorinx; 12-15-2013 at 11:28 PM.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I am far from an expert on this; I never did malloc for multidim array.

    But, the errors was reduce by changing classAverage and allClassAverage number of stars.
    float average(float ***grade);//functionprototypes
    void classAverage(float *grade);
    void allClassAverage(float **grade);

    You really NEED to comment to code; its possible the better people on this board are ignoring you because you code is without comments and the code looks real bad.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Same lines different error. not sure what I'm mistaking

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Ok I think I got it. Talking to myself is doing wonders for some reason

    Code:
    #include <stdio.h>
    #include <malloc.h>
    
    #define CLASS 2
    #define STUDENT 2
    #define GRADE 2
    
    
    
    float average(float ***grade);//functionprototypes
    void allClassAverage(float **grade);
    void classAverage(float *grade);
    
    
    
    int main()
    {
        int  i, j, k;//variable declaration
        float averageGrade=0;
        float grade[CLASS][STUDENT][GRADE];
        //subject student grade
    
        float*** array;
        array=malloc(sizeof(float**)*CLASS);
        
        
       for (i = 0 ;  i < CLASS; i++) {
          array[i] = malloc(sizeof(float *) * STUDENT);
     
          for (j = 0; j < STUDENT; j++) {
             array[i][j] = (float *)malloc(sizeof(int) * GRADE);
     
              for (k = 0; k < GRADE; k++)
                 array[i][j][k] = i * j * k;
          }
       }
     
        for(i=0;i<CLASS;i++){
            for(j=0;j<STUDENT;j++){
                for(k=0;k<GRADE;k++){
                    printf("Enter in grade %d for class %d student %d:\n",k+1, i+1,j+1);//user input for grades
                    scanf("%f", &array[i][j][k]);;        
                        }
                    }
                }
    
        averageGrade=average(array); //function call for average grade and prints it out
        printf("%.2f, average for all grades\n", averageGrade);
    
        for(i=0;i<CLASS;i++){
            for(j=0;j<STUDENT;j++){//for loops to pass function for subject average
                classAverage(array[i][j]);
                    }
                }
    
        for(i=0;i<CLASS;i++){//for loop for subject average
            allClassAverage(array[i]);
                }
    }
    
    
    float average(float ***grade)//function definition
    {
        int i, j, k;
        float gradeAve=0;//var declaration
    
            for(i=0;i<CLASS;i++){//for loops to calculate average grade
                for(j=0;j<STUDENT;j++){
                    for(k=0;k<GRADE;k++){
                        gradeAve+=grade[i][j][k];
                        }
                    }
                }    
    
        return gradeAve/8;//return the gradeave value/220 inputs
    }
    
    void classAverage(float *grade)//function def
    {
        float avesub=0;
        int k;// var dec
    
            for(k=0;k<GRADE;k++){//for loop to calculate class average
                avesub+=grade[k];
                    }        
                
    
    //    printf("%.2f, average class\n", avesub);//print class ave
    
    }
    
    void allClassAverage(float **grade)//func def
    {
        int j, k;
        float donkeypunch=0;
         static int i=1;//var dec
                for(j=0;j<STUDENT;j++){//nested for to determine both class ave
                    for(k=0;k<GRADE;k++){
                    printf("%f grade jk \n", grade[j][k]);
                        donkeypunch+=grade[j][k];
                            }
                        }
                        
    //    printf("%.2f, subject average for student %d\n", (donkeypunch/4), i++);//print sub aves
    }
    Last edited by Sorinx; 12-16-2013 at 01:12 AM.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Alright I'm done, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to integer without a cast
    By javaeyes in forum C Programming
    Replies: 9
    Last Post: 03-11-2012, 10:01 AM
  2. pointer from integer without a cast - warning
    By Andreea Coman in forum C Programming
    Replies: 17
    Last Post: 01-11-2012, 02:48 PM
  3. 'Makes pointer from integer without a cast'
    By idlackage in forum C Programming
    Replies: 5
    Last Post: 06-10-2010, 05:48 AM
  4. integer to pointer without cast
    By eidgeare in forum C Programming
    Replies: 3
    Last Post: 12-08-2009, 03:14 PM
  5. Pointer from integer without cast
    By STDSkillz in forum C Programming
    Replies: 2
    Last Post: 10-22-2007, 09:39 PM