record is a pointer to a pointer (**record), used like a dynamic array and numwords is the number of words/records.

This is homework. I'm supposed to read in a text file, read out a text file with all the lines a certain size or less, and read out a sorted text file that has one occurrence of each word (without worrying about punctuation) and the number of times that word occurred.

Everything but the sorted part is done, I just can't seem to get the qsort function to work right.

Any help will be greatly appreciated. Thank you.

November 12th, 2010, 05:05 PM

Paul McKenzie

Re: qsort help

Quote:

Originally Posted by Neon612

I keep trying different ways of writing a compare function but when I run the program I keep getting a seg fault at the qsort function call.

If this is a C++ program, you're better off using std::sort as there is no need for all of those casts, i.e. it's typesafe, and the comparison function is much simpler.

Also on a side note: what exactly does "warning: implicit declaration of function ‘strdup’ " mean?

From what I've been able to find that warning says something along the lines of not having the right header file imported.

strdup is defined in <string.h>. However, it may be deprecated on some systems. If it's not available, it's equivalent to malloc a big enough block and then strcpy.

November 12th, 2010, 06:55 PM

Neon612

Re: qsort help

Thanks, so I need to use strcpy instead? I tried it replacing:

Code:

record[numwords] -> data = (char *)strdup(newword);

with

Code:

strcpy(record[numwords] -> data, newword);

but now that strcpy line is seg faulting on me.

Do I need to do another malloc for the record[num] -> data string? Shouldn't the first malloc set aside enough space for the whole structure (count and data combined)?

November 12th, 2010, 08:50 PM

Paul McKenzie

Re: qsort help

Quote:

Originally Posted by Neon612

Thank you. Simple fix: size of(Wordrec *)

Also on a side note: what exactly does "warning: implicit declaration of function ‘strdup’ " mean?

From what I've been able to find that warning says something along the lines of not having the right header file imported.

When you write a 'C' program, you need not declare your functions. The problem with this is that if you do not declare your functions, the compiler will use as the default return type "int".

That is why when you post 'C' code, you need to post all of the code, including all the headers. For example, there is a huge difference in this C program if the header is not included:

Code:

int main()
{
double x = sqrt(10);
}

Code:

#include <math.h>

int main()
{
double x = sqrt(10);
}

You say "what makes this different"? The answer is the header file <math.h>. When math.h is not included, the compiler assumes that sqrt() is a function returning an int. But in reality, the function returns a double. What can happen? A crash due to a corrupt stack.

The <math.h> declares the sqrt() function as returning a double and taking a double as the argument. This forces the 'C' compiler to do the compile check to make sure 1) the correct object code is generated with respect to the return type of double, and 2) the arguments you're passing are the right type and the right number.

For example, this program compiles just fine:

Code:

int main()
{
double x = sqrt(10, 3, 5, 5);
}

What happens when you run it? Who knows.

That's why when 'C' was first developed, these errors existed all over the place in practically every non-trivial app. That's why function prototypes made it into 'C' in a later revision of the language (but still not mandatory) -- the bugs were literally making 'C' programmers have sleepless nights.

However, for C++, it is a requirement that all functions are declared and/or defined before you call them. You can't make this mistake in a C++ program.