C - Invalid instruction 4 - c

I am working on an app that generates then lists school records. But every time I run it keeps giving me a
illegal instruction: 4
This is very annoying and won't go away. How can I fix this issue? Its been keeping me awake all night I've been looking all over the internet and can't find a solution.
This is the code that I am using I'm thinking its something with the arrays
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <time.h>
struct student{
char Name[15];
char Grade[5];
char Age[3];
};
void GenerateStudent(int numOfStudents){
//Setting struct
struct student sdv[numOfStudents];
//Setting Defult Values
char names[50][15] = {
"Daniel",
"Olivia",
"Blair",
"Charley",
"Tom",
"Jim",
"Peter",
"Liam",
"Tasha",
"Marissa",
"Alexa",
"Ben",
"Kylie",
"Jasmin",
"Jaz",
"Merik",
"Nathan",
"William",
"Lucas",
"Mason",
"Logan",
"Alexander",
"Jack",
"Owen",
"James",
"Oliver",
"Jackson",
"Carter",
"Ryan",
"Matthew",
"Emma",
"Ava",
"Sophia",
"Charlotte",
"Emily",
"Abigail",
"Chloe",
"Isabella",
"Avery",
"Ella",
"Lily",
"Amelia",
"Hannah",
"Sofia",
"Grace",
"Victoria",
"Maya",
"Audrey",
"Evelyn",
"Nolan"
};
char grades[5][5] = {"A","B","C","D","F"};
int ages[] = {5,6,7,8,9,10,11,12,13,14,15,16,17};
int nameNum,gradeNum,ageNum;
//Getting Rand seed using time
time_t t;
srand((unsigned) time(&t));
for(int i = 0;i < numOfStudents;i++){
//Generating Rand #
nameNum = rand() % 50 + 1;
gradeNum = rand() % 5 + 1;
ageNum = rand() % 13;
//Writing Values to Array
strcpy(sdv[i].Name, names[nameNum]);
strcpy(sdv[i].Grade, grades[gradeNum]);
sprintf(sdv[i].Age, "%d", ages[ageNum]);
}
//Make sure you make a file called student_records.txt so you get the output of this app
FILE * fpointer = fopen("school_records.txt", "a");
printf("Writing to file...\n");
for(int n = 0;n < numOfStudents;n++){
//Printing Records into the file
fprintf(fpointer, "Name: %s Age: %s Avg Grade: %s\n", sdv[n].Name,sdv[n].Age,sdv[n].Grade);
}
//Removing the file from memory and saving changes
printf("Done!\n");
fclose(fpointer);
};
void mainMenu(){
printf("---------------Main Menu---------------\n");
printf("█████▒▒ 1.Generate Students\n");
printf("█████▒▒ 2.Clear Records\n");
printf("█████▒▒ 3.Exit\n");
printf("---------------------------------------\n");
}
void genMenu(){
printf("-------------Generate Menu-------------\n");
printf("Enter # of records you want to make\n");
printf("---------------------------------------\n");
}
void cleanMenu(){
printf("--------------Clear Menu---------------\n");
printf("Are you sure that you want to delete\nall information from the file?\n[Y]es or [N]o\n");
printf("-------------------------------------\n");
}
int main(){
//Declaring Variables
//We cant leave this one unasigned just becuase it can cause an error if the use types 0
int recordRequests = 1;
int i,entOption;
int exitNum = 0;
while(exitNum == 0){
mainMenu();
printf("Enter Option: ");
scanf("%d", &entOption);
if(entOption == 1){
printf("\n\n\n");
genMenu();
printf("Enter Value: ");
scanf("%d", &recordRequests);
GenerateStudent(recordRequests);
}else if(entOption == 2){
char ans;
int loopExit = 0;
cleanMenu();
printf("Enter Value: ");
scanf("&c", ans);
if(ans == 'Y'){
printf("Cleaning file...\n");
FILE * fpointer = fopen("school_records.txt", "w");
//Printing Records into the file
fprintf(fpointer,"");
//Removing the file from memory and saving changes
fclose(fpointer);
printf("Clean sucsessful!\nReturning to menu...\n");
loopExit = 0;
}else if(ans == 'N'){
printf("Sending back to main menu...\n\n\n");
loopExit = 1;
}else{
printf("Invalid Option Please type Y for yes or N for no\n");
}
}
}
return 0;
}

I believe I was able to reproduce your problem. When I run your code on my Microsoft compiler, I get a "Run-Time Check Failure #4" complaining about stack corruption near the variable-length array. I was able to fix this by adding space for the terminating null character by changing char Age[2]; to char Age[3];, as has already been hinted by several other people in the comments section.
The problem was that the following line was accessing all three arrays out of bounds:
sprintf(sdv[i].Age, "%d", ages[ageNum]);
It is accessing the array sdv out of bounds, because i is in the range [0..numOfStudents], but it should be in the range [0..numOfStudents-1]. This can be fixed by replacing the line for(int i = 0;i <= numOfStudents;i++){ with for(int i = 0;i < numOfStudents;i++){.
It is accessing the array sdv[i].Age out of bounds. That array is declared as a char array of length 2. However, the maximum string length is 3. For example, the string "14" requires a char array of length 3 for storage, because it also requires space for the terminating null character. This can be fixed by replacing the line char Age[2]; with char Age[3]; in the declaration of the student struct.
It is accessing the array ages out of bounds, because ageNum is in the range [1..13], but it should be in the range [0..12]. This is because, in the programming language C, array indexes are 0-based, in contrast to some other programming languages, which have 1-based array indexes. This can be fixed by changing the line ageNum = rand() % 13 + 1 to ageNum = rand() % 13;. Also, the +1 must be removed for the same reason from the two lines above that line.
Most likely, what I described as #2 is causing the stack memory corruption. However, all three out of bounds array accesses cause undefined behavior, so theoretically, any one of these could be the problem.
Additonally, your program contains the following bug:
In the function main, you assign the value 0 to exitNum and then set the for loop condition so that it continues as long as exitNum stays 0. However, you never assign a different value to exitNum. Therefore, you effectively have an infinite loop, making it impossible to exit your program from the main menu.

Related

seg fault in c with pointers

I have two fucntions that prompt the user to print ingredients names for pizza and print them out. But whenever I'm trying to print out the available ingredients, I get a seg fault.
And when I prompt to enter ingredients, if I say we have 3 available ingredients today, I can only type 2 in (as shown in the output picture)
int get_ingredients(char** ingredients,int* num_ingredients) {
char **ingredients_array = NULL; /*this is an array of pointers points to single ingredient in get_item*/
int temp,i,j;
ingredients = &ingredients_array; /*ingredients is a pointer points to the array of pointers*/
temp = *num_ingredients;
printf("How many available pizza ingredients do we have today? ");
scanf("%d",&temp);
ingredients_array = (char**)calloc(temp, sizeof(char*));
i = 0;
printf("Enter the ingredients one to a line: \n");
while (i < temp){
*(ingredients_array+i) = get_item();
i++;
}
i = 0;
printf("Available ingredients today are: \n");
while(i < temp) {
j = i+1;
printf("%d",j);
print(". ");
printf("%s", **(ingredients_array+i));
i++;
}
*num_ingredients = temp;
return EXIT_SUCCESS;
}
char* get_item(){
int i,a;
char *each_ingredient= (char*)malloc(61*sizeof(char)); /*points to each input*/
a = getchar();
i = 0;
while (a != EOF && (char)a != '\n'){
*(each_ingredient+i)= (char)a;
a = getchar();
i++;
}
*(each_ingredient+i) = '\n';
return each_ingredient;
}
This is the output
There are many issues with your get_ingredients question.
The first issue that I noticed is that you changed the pointer of the parameter ingredients, you changed it to the pointer get_ingredients which was set to NULL. So accessing the data from ingredients pointer from now on will get you a segmentation fault, for trying to access an illegal address.
int get_ingredients(char** ingredients,int* num_ingredients) {
char **ingredients_array = NULL; /*this is an array of pointers points to single ingredient in get_item*/
int temp,i,j;
//ingredients pointer is lost forever and set to the address of the pointer of ingredients_array
ingredients = &ingredients_array; /*ingredients is a pointer points to the array of pointers*/
Now the second issue is more of an optimization thing. You set a variable, and then you changed it on the scanf, making the useless to set it to some value initially.
//Temp is set to num_ingredients, but this is of no use,becuase this value is never used and is overwritten by scanf("%d",&temp);
temp = *num_ingredients;
printf("How many available pizza ingredients do we have today? ");
scanf("%d",&temp);
Should allocate pointer of a pointer to char.
ingredients_array = (char**)calloc(temp, sizeof(char*));
Changes
ingredients_array = (char**)calloc(temp, sizeof(char**));
The while loops can be replaced with a for a loop. Which a more appropriate type of loop for this case.
//This can write it better with a for loop
i = 0;
printf("Enter the ingredients one to a line: \n");
while (i < temp){
*(ingredients_array+i) = get_item();
i++;
}
i = 0;
printf("Available ingredients today are: \n");
//Use for loop intead, because it more appropiate for this case
while(i < temp) {
//better to use i+1 instead of j, simply things
j = i+1;
//These two printf can be on the same line
printf("%d",j);
printf(". ");
//Allocation error?
printf("%s", **(ingredients_array+i));
i++;
}
Using a for loop instead, getting user input from standard function, and using less confusing indexing.
printf("Enter the ingredients one to a line: \n");
//Using for loop
for(int i = 0;i < temp;i++)
{
char ptr[80]; //Store user input
scanf("%s", ptr); //Get user input
ingredients_array[i] = strdup(ptr); //strdup is to make a another string with the same contents
}
printf("Available ingredients today are: \n");
for(int i = 0; i < temp;i++)
{
//These two printf can be on the same line
printf("%d",i+1);
print(". ");
//Allocation error?
printf("%s\n", ingredients_array[i]);
}
*num_ingredients lost its original pointer earlier, so this serves no use.
//This cannot be used because now points to ingredients_array and not to num_ingredients
*num_ingredients = temp;
Now the code with all those changes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int get_ingredients(char** ingredients,int* num_ingredients) {
char **ingredients_array = NULL; /*this is an array of pointers points to single ingredient in get_item*/
int temp;
printf("How many available pizza ingredients do we have today? ");
scanf("%d",&temp);
ingredients_array = (char**)calloc(temp, sizeof(char**));
printf("Enter the ingredients one to a line: \n");
//Using for loop
for(int i = 0;i < temp;i++)
{
char ptr[80]; //Store user input
scanf("%s", ptr); //Get user input
ingredients_array[i] = strdup(ptr); //strdup is to make a another string with the same contents
}
printf("Available ingredients today are: \n");
for(int i = 0; i < temp;i++)
{
printf("%d. ",i+1);
printf("%s\n", ingredients_array[i]);
}
*num_ingredients = temp;
return EXIT_SUCCESS;
}
int main()
{
char** ingredients;
int a;
int foo = get_ingredients(ingredients, &a);
return 0;
}
Output
How many available pizza ingredients do we have today? 4
Enter the ingredients one to a line:
meat
MEAT
mEaT
no_salas
Available ingredients today are:
1. meat
2. MEAT
3. mEaT
4. no_salad

Having problems with 2D char arrays

So I've got an assignment where my program asks the brand (10 letters), model (10 letters), age (1986 - 2019) and cost (positive real number) of 10 cars and then wants the program to check which car is the oldest and to print out it's brand and model. I don't have a problem with the first part but with the second part.
The code is:
//First part
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define C 10
#define M 11
int main(void)
{
char brand[C][M];
char model[C][M];
int year[C];
float cost[C];
int i, len1, len2, min;
for(i=0; i<C; i++){
printf("Car %d\n", i+1);
do{
printf("Brand: ");
scanf("%s", brand[i]);
len1 = strlen(brand[i]);
} while(len1<0 || len1>10);
do{
printf("Model: ");
scanf("%s", model[i]);
len2 = strlen(model[i]);
} while(len2<0 || len2>10);
do{
printf("Year: ");
scanf("%d", &year[i]);
} while(year[i]<1986 || year[i]>2019);
do{
printf("Cost: ");
scanf("%d", &cost[i]);
} while(cost[i]<=0);
}
//Second part
year[0] = min;
for(i=0; i<10; i++)
if(year[i] < min){
min = year[i];
printf("\nThe oldest car is %s %s\n", brand[i], model[i]);
}
For some reason it either prints out gibberish in the place of brand[i] or if I lose the columns of the if statement prints out all the car brands and their models, where I only want the oldest one.
Aside from scanf not being recommended there are some problems with this code, first when you read the brand and model you do:
do{
printf("Brand: ");
scanf("%s", brand[i]);
len1 = strlen(brand[i]);
} while(len1<0 || len1>10);
The problem here is that you first write the string to brand[i] and then check if it's too long, but you have already written it into the array so if the string is longer than your space you already have a buffer overflow. Limit the size you can read with scanf using scanf("%10s, brand[i]) or better yet use fgets(brand[i], sizeof(brand[i]), stdin).
Next in the second part you use min without initializing it, and you overwrite the content of year[0] with it. You probably wanted something like:
min = 2020; // or a number that will be bigger than all your cars anyway
int older = 0;
i = 0;
for(i=0; i<C; i++){ // Use C here, you have it might as well use it instead of magic numbers
if(year[i] < min){
older = i;
min = year[i];
}
}
printf("\nThe oldest car is %s %s\n", brand[older], model[older]);
but bare in mind that this solution will print multiple cars if they are the oldest ones and have the same year

How to find matching values in two arrays?

I need to get the user to input 6 numbers and I store those in an array called winningNum[]. Then I have to read in a file that has a bunch of users firstName lastName and the numbers they have guessed. I need to compare these two arrays and only print out the first and last name of the users from the file that got a minimum of three numbers matched.
This is the struct of for the input file users
typedef struct
{
char firstName [20];
char lastName [20];
int numbers[6];
}KBLottoPlayer;
Getting the winning numbers from the user
int getNum()
{
int winningNum[6];
int i;
printf("Please enter the six nunbers between 1-53:\n");
scanf("%d %d %d %d %d %d", &winningNum[0], &winningNum[1],
&winningNum[2] ,&winningNum[3], &winningNum[4], &winningNum[5] );
}
This is where I am reading in the file and putting it into the struct array
KBLottoPlayer* readArray()
{
int i,size;
FILE *in = fopen("KnightsBall.in","r");
fscanf(in,"%d",&size);
KBLottoPlayer* temp;
temp =(KBLottoPlayer*)malloc(sizeof(KBLottoPlayer)*size);
if((in = fopen("KnightsBall.in", "r")) != NULL )
{
char buffer[100];
fgets(buffer, 5, in);
for(i=0;i<size;i++)
{
fscanf(in," %s %s ", temp[i].firstName, temp[i].lastName);
fscanf(in,"%d %d %d %d %d %d ", &temp[i].numbers[0],
&temp[i].numbers[1], &temp[i].numbers[2], &temp[i].numbers[3],
&temp[i].numbers[4], &temp[i].numbers[5]);
}
}
else
{
printf("File is Not Exist.\n");
}
return temp;
}
I essentially need to only store the first and last name of the users that got 3 4 5 6 of the winning numbers correct.
I will admit that you only need hints to go past a problem.
Unrelated, but you never test your input functions. Beware a single incorrect line will give undefined results and you will not even know where the problem is. Remember: never trust what comes from the outside.
Back to your problem. A simple way is to use 2 nested loops, one on the winning numbers and one on the guessed ones just counting the matches: if the total number of matches is at least 3, you keep the record, else you reject it. You can even do that when reading the file (here in pseudo-code):
int recnum = 0; // next record to store
for (int i=0; i<size; i++) { // loop over the input file
read the line into temp[recnum]
int count = 0; // number of correct guesses
for (int j=0; j<6; j++) { // loop over the winning numbers
for (int k=0; k<6; k++) { // loop over the guessed numbers
if winning[j] == guessed[k] {
count++;
}
}
}
if (count >= 3) recnum++; // only keep if at least 3 correct guesses
}

Extreme sum values calculated inside a structure

While I was learning to code in C about structure and pointers, I tried to make a program that calculate grades of students.
I thought it would work from my previous experiences for such calculation without pointers and structure. But with those, it gave me wild results in the program.
#include <stdio.h>
#include <string.h>
/*
The program will scan year, name, score of three different subjects,
and calculate the sum and the average.
Three different people (using array) will be taken into account.
*/
struct grade {
int year;
char name[20];
int score[3];
int total;
float avg;
};
void main() {
struct grade p[3];
char str = 'c';
char *pstr = NULL;
int i, j;
pstr = &str;
for (j = 0; j < 3; j++) {
printf("Year of Admission: ");
scanf("%d", &p[j].year);
printf("Name of the Student: ");
scanf("%s", pstr);
strcpy(p[j].name, pstr);
for (i = 0; i < 3; i++) {
printf("The score for Subject %d: ", i + 1);
scanf("%d", &p[j].score[i]);
p[j].total += p[j].score[i];
}
p[j].avg = p[j].total / 3.0;
}
for (j = 0; j < 3; j++) {
printf("%s's\n", p[j].name);
printf("Total score: %d\n", p[j].total);
printf("Average: %.2f\n", p[j].avg);
}
}
I could have written each of three different subjects as one variable but for an extra "challenge", I made an array inside the structure.
int score[3];
However, the program only prints out extremely small number -89541694... for both totals and averages.
I assume that this particular line inside a for-loop is a problem.
scanf("%d", &p[j].score[i]);
But I could not figure out why. I am really new to pointers and still learning them.
I hope for your generous teaching and explanations.
Thank you in advance.
Local variables are not initialized with 0, so you just need to zero it before calculating total:
p[j].total = 0;
before
for (i = 0; i < 3; i++) {
printf("The score for Subject %d: ", i + 1);
scanf("%d", &p[j].score[i]);
p[j].total += p[j].score[i];
}
The variable pstr points to a single char. A string in C needs to be at least two characters for a single-character string: The actual character, and the null terminator.
When you use e.g. scanf to read a string, the function will write at least two bytes to the memory pointed to by pstr. But since it only points to a single byte you will write out of bounds and that leads to undefined behavior.
If you want to be able to read more than a single character you need to have more space allocated for the string. And you need to limit scanf so it will not write out of bounds.
For example
char pstr[40]; // Allows for strings up to 39 character, plus terminator
// ...
scanf("%39s", pstr); // Read at most 39 characters from standard input, and write to pstr
Another problem is that local variables are not automatically initialized, their values will be indeterminate.
That means the contents of the array p is unknown and seemingly random.
When you do
p[j].total += p[j].score[i];
you use the seemingly random value of p[j].total to calculate another seemingly random number.
To initialize all structures and all their members to "zero" in the array, do e.g.
struct grad p[3] = { 0 };
Instead of making pstr a pointer you might wanted to do somehting like this
char pstr[30];
And accordingly you will scanf the string using scanf("%29s",pstr); and check it's return value.
To describe the problem a bit - you had a pointer pointing to a char which is not capable of holding an input characters and the corresponding \0 (nul terminating character). As a result this gives rise to undefined behavior. And then using it in strcpy is also an illegal code. (Undefined behavior).
Here the solution I gave simply declared an array of 30 characters and we limited the string input using scanf upto 29 characters because we need to store the terminating null.
Showing you atleast a bit of code to make you understand how to write these codes:-
if( scanf("%29s",pstr)!= 1){
fprintf(stderrm"Error in input");
exit(EXIT_FAILURE);
}
Another problem is initialize the variables - here you used p[j].total += p[j].score[i]; What is the value of p[j].total initially. It contains garbage value. In the loop make p[j].total = 0; first. That will give you the correct result.
Note: The wild results are the garbage value resulted from addition of some garbage value with p[j].score[i].
Also note that without making the changes that I said if you only change the initialization thing then also code is not guranteed to work. undefined behavior is undefined behavior - cases may arise which will simply crash the program making you wonder where you went wrong.
Illustration code may help you:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
The program will scan year, name, score of three different subjects,
and calculate the sum and the average.
Three different people (using array) will be taken into account.
*/
struct grade {
int year;
char name[20];
int score[3];
int total;
float avg;
};
int main(void) {
struct grade p[3];
char pstr[20];
int i, j;
for (j = 0; j < 3; j++) {
printf("Year of Admission: ");
if(scanf("%d", &p[j].year)!=1){
fprintf(stderr, "%s\n", "Error in input");
exit(EXIT_FAILURE);
}
printf("Name of the Student: ");
if(scanf("%19s", pstr)!=1){
fprintf(stderr, "%s\n", "Error in input");
exit(EXIT_FAILURE);
}
strcpy(p[j].name, pstr);
p[j].total = 0;
for (i = 0; i < 3; i++) {
printf("The score for Subject %d: ", i + 1);
if(scanf("%d", &p[j].score[i])!=1){
fprintf(stderr, "%s\n", "Error in input");
exit(EXIT_FAILURE);
}
if(p[j].score < 0){
fprintf(stderr, "%s\n", "Error in input");
exit(EXIT_FAILURE);
}
p[j].total += p[j].score[i];
}
p[j].avg = p[j].total / 3.0;
}
for (j = 0; j < 3; j++) {
printf("%s's\n", p[j].name);
printf("Total score: %d\n", p[j].total);
printf("Average: %.2f\n", p[j].avg);
}
return 0;
}
In fact instead of using the pstr just input the names directly in the structure variable instance itself. No need to use a temporary variable.

Expression must have class type error

I'm working on a homework assignment and I've hit a brick wall. I think I have all of the code that I need, I just need to get the program to compile. The object of the assignment is
Create a structure to hold student names and averages. The structure should contain a first name, last name and an integer grade average.
Then:
Write a program that will do the following:
1.) Create an array of pointers to these student structures.
2.) Prompt the user for names and averages.
3.) After you get the student’s information use malloc to provide the memory to store the information.
4.) Place the address of the student, returned by malloc, into the pointer array.
5.) AFTER the user indicates there are no more students:
Search the data entered and find the highest and lowest grade
average.
a)Print the name and grade for the highest grade
b)Print the name and grade for the lowest grade
c)Print the average of all grades entered
Here is my code:
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#define SIZE 25
int enterStudents (int ePointArray[SIZE]);
void searchData (int *sPointArray, int *sHigh, int *sLow);
int calculateAvg (int, int *avgPointArray);
void printData (int, int *pHigh, int *pLow);
struct student
{
char firstName[20];
char lastName[20];
int average;
};
int main()
{
int pointArray[SIZE], high[3], low[3];
int i = 0, studentCounter, avgGrade;
for (i = 0; i < SIZE; i++)
pointArray[i] = 0;
studentCounter = enterStudents(pointArray);
searchData(pointArray, high, low);
avgGrade = calculateAvg(studentCounter, pointArray);
printData(avgGrade, high, low);
return 0;
}
int enterStudents (int ePointArray[SIZE])
{
char tempFname[20], tempLname[20], yesNo[2] = "y";
int tempAvg, counter = 0;
int *studPtr;
struct student aStud={"\0", "\0", 0};
while( counter < SIZE && strcmp(yesNo, "y")==0)
{
printf(" Enter first name: ");
scanf("%s", tempFname);
printf(" Enter last name: ");
scanf("%s", tempLname);
printf(" Enter grade average:");
scanf("%d", tempAvg);
strcpy(aStud.firstName, tempFname);
strcpy(aStud.lastName, tempLname);
aStud.average = tempAvg;
studPtr = malloc(sizeof(struct student));
ePointArray[counter] = *studPtr;
counter++;
printf("/n");
printf(" Do you have more students? yes or no:");
scanf("%s", yesNo);
}
return counter;
}
void searchData (int sPointArray[SIZE], int sHigh[3], int sLow[3])
{
int searchCounter = 0;
while( searchCounter = 0)
{
if( *sPointArray[searchCounter].average > *sPointArray[searchCounter+1].average)
{
sHigh[0] = &sPointArray[searchCounter].firstName;
sHigh[1] = &sPointArray[searchCounter].lastName;
sHigh[2] = &sPointArray[searchCounter].average;
}
if( *sPointArray[searchCounter].average < *sPointArray[searchCounter+1].average)
{
sLow[0] = &sPointArray[searchCounter].firstName;
sLow[1] = &sPointArray[searchCounter].lastName;
sLow[3] = &sPointArray[searchCounter].average;
}
searchCounter++;
}
}
int calculateAvg( int totalStudents, int avgPointArray[SIZE])
{
int sum = 0;
int avgCounter;
double overallAvg;
for( avgCounter = 0; avgCounter < totalStudents; avgCounter++)
sum = sum + *avgPointArray[avgCounter].average;
overallAvg = sum/totalStudents;
return overallAvg;
}
void printData (int pAverage, int pHigh[3], int pLow[3])
{
printf(" Highest Grade: %s %s %d", pHigh[0], pHigh[1], pHigh[3]);
printf("/n");
printf(" Lowest Grade: %s %s %d", pLow[0], pLow[2], pLow[3]);
printf("/n");
printf(" Average Grade: %d",pAverage);
}
The main chunk of problems come from the searchData function. In the if statements, every occurrence of *sPointArray and &sPointArray is underlined in red and the error reads
"Error: expression must have class type"
The same thing also happens in the calculateAvg function with *avgPointArray in the for loop. I know that the error is a fairly common problem for noobie C programmers (i.e myself) and that it generally has to do with writing the code as a function instead of a statement or something like that, but I can't for the life of me find where I have went wrong. Any help would be highly appreciated. I've been working at this for so long my vision is blurring.
Also, for anyone who solves this in like two seconds and wants proof that I'm a true idiot, there is an error in the enterStudents function where it says StudPtr = malloc(sizeof...). The error shows under the assignment symbol and says
"Error: a value of type "void*" cannot be assigned to an entity of type "int*".
I understand this concept in theory, but some advice for how to fix it would be highly appreciated.
Thank you in advance for any help.
You declare the sPointArray as an array of integers, but use it as an array of structures.

Resources