1

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:

  1. expected expression before 'int'

  2. 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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Right, the calling thing is clear. Anyway, that problem is resolved. But instead I am getting a segmentation fault now. :/
The seg fault is probably due to you calling a function with wrong index arguments, for instance pointing to a segment outside of your program.
Added a section to my answer. Please accept it (and upvote) if it solves your problem.
Yes I've done all the basic Merge Sort stuff. I don't understand the mistake. I'm new here, are we allowed to post full codes and ask people to check logical errors as well?
Added some more explaining to my answer. You can ask for logical errors as well - I just explained one :). Of course you can post full code, but try to trim it to make it as relevant as possible. Not lots of irrelevant fluff around it.
|
0
 MergeSort(A, 0, half-1); //error in this line MergeSort(A, half, y); //error in this line 

Comments

0

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.