We help IT Professionals succeed at work.

Segmentation Fault in C when Passing Arrays

sprockston
sprockston asked
on
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2009

Commented:

#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

CERTIFIED EXPERT
Top Expert 2009
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

Author

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.