Segmentation Fault in C when Passing Arrays

Hello. I am trying to make a function for my program that will find the min and the max values. It seems right when I type it out, but I keep getting a segmentation fault error after entering 8 values into the array. Any suggestions? Thanks!
#include <stdio.h>
 
void findMinAndMax (int *arrayOfMarks, int size, int *max, int *min){
	int i = 1;
	(*max) = arrayOfMarks[0];
	(*min) = arrayOfMarks[0];
	while (i<size){
		if (arrayOfMarks[i]>(*max)){
			(*max)=arrayOfMarks[i];
			}
		if (arrayOfMarks[i]<(*min)){
			(*min)=arrayOfMarks[i];
			}
		}
}
 
int main (){
	int count;
	int marks[8];
	int *max;
	int *min;
	for (count=0;count<8;count++){
		scanf("%d",&marks[count]);
		}
	findMinAndMax(marks,8,max,min);
	printf("%d %d",max,min);
	return 0;
}

Open in new window

sprockstonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Infinity08Connect With a Mentor Commented:
Ah, and of course you have to increment i inside the while loop ;)
#include <stdio.h>
 
void findMinAndMax (int *arrayOfMarks, int size, int *max, int *min){
        int i = 1;
        (*max) = arrayOfMarks[0];
        (*min) = arrayOfMarks[0];
        while (i<size) {
                if (arrayOfMarks[i]>(*max)){
                        (*max)=arrayOfMarks[i];
                }
                if (arrayOfMarks[i]<(*min)){
                        (*min)=arrayOfMarks[i];
                }
                ++i;
        }
}
 
int main (){
        int count;
        int marks[8];
        int max;         /* <--- you need to provide space in memory for max and min */
        int min;
        for (count=0;count<8;count++){
                scanf("%d",&marks[count]);
        }
        findMinAndMax(marks,8,&max,&min);   /* <--- pass the addresses of max and min */
        printf("%d %d",max,min);
        return 0;
}

Open in new window

0
 
Infinity08Commented:

#include <stdio.h>
 
void findMinAndMax (int *arrayOfMarks, int size, int *max, int *min){
        int i = 1;
        (*max) = arrayOfMarks[0];
        (*min) = arrayOfMarks[0];
        while (i<size){
                if (arrayOfMarks[i]>(*max)){
                        (*max)=arrayOfMarks[i];
                        }
                if (arrayOfMarks[i]<(*min)){
                        (*min)=arrayOfMarks[i];
                        }
                }
}
 
int main (){
        int count;
        int marks[8];
        int max;         /* <--- you need to provide space in memory for max and min */
        int min;
        for (count=0;count<8;count++){
                scanf("%d",&marks[count]);
                }
        findMinAndMax(marks,8,&max,&min);   /* <--- pass the addresses of max and min */
        printf("%d %d",max,min);
        return 0;
}

Open in new window

0
 
sprockstonAuthor Commented:
Thank you very much for your clear, concise answer. Best of luck in the future. :)
0
All Courses

From novice to tech pro — start learning today.