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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Infinity08Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sprockstonAuthor Commented:
Thank you very much for your clear, concise answer. Best of luck in the future. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.