Reputation: 55
I'm a beginner in C. I checked the various discussion for the query I was looking for, but none of them helped. I wrote a code for Merge Sort and I'm getting the following errors:
expected expression before 'int'
too few arguments to function 'MergeSort'
int *list; //pointer to array of integers
int * MergeSort(int *A, int x, int y); //function definition, the function returns a pointer to an array of integers.
int * MergeSort(int *A, int x, int y) //function declaration
{
//some code
int size=1+y-x;
int half=size/2;
MergeSort(int *A, 0, half-1); //error in this line
MergeSort(int *A, half, y); //error in this line
//some code
}
Help would be greatly appreciated! Thank you.
Update: Previous error resolved.
Segmentation fault (core dumped). I don't understand the problem. Here's a link to the code I've written. http://ideone.com/mHXQ66
Upvotes: 1
Views: 8588
Reputation: 23013
You don't need to specify the type of the argument when you call a function.
So instead of
MergeSort(*A, 0, half - 1);
You do
// You already specified that A was a pointer in your function definition: int *A
MergeSort(A, 0, half - 1);
When you want to call this function, with say the list
argument you defined at the top, you just write
MergeSort(list, myX, myY); // where myX and myY are defined somewhere, relating to your list
With regards to your function, you need some more meat in the function body. You especially need to check for the case when your array split into pieces of size 1. So add this
if(x - y < 2) // if run size == 1
return;
Otherwise you never return! Think of when x == y
, you will just call MergeSort(A, 0, 0) over and over again until your stack overflows.
Concerning the segmentation fault
I cannot see anything wrong with the full code you linked to. Seems pretty ok to me, but I don't have the possibility to debug your program for you. Rather, I will point you to DDD, a graphical front-end to the GDB debugger, making it A LOT more user friendly.
It can be downloaded here, and a great debugging tutorial can be found here. Using that you will find the error in minutes.
Upvotes: 2
Reputation: 755587
There is no need to specify the type when passing A
as an argument, just using the values name
MergeSort(A, 0, half - 1);
MergeSort(A, half, y);
Upvotes: 0
Reputation: 1734
MergeSort(A, 0, half-1); //error in this line
MergeSort(A, half, y); //error in this line
Upvotes: 0