ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
gcc -c -Wall sometest.c
sometest.c: In function `sortByTime':
sometest.c:23: warning: passing arg 1 of `stat' from incompatible pointer type
sometest.c:23: warning: passing arg 2 of `stat' from incompatible pointer type
sometest.c:24: error: request for member `st_mtim' in something not a structure
or union
sometest.c:24: error: request for member `st_mtim' in something not a structure
or union
line23:
The first argument of stat must be a pointer to a string, the second one must be a pointer to a struct stat.
line24:
arr is not of type struct stat, and for that reason does not contain a member st_mtime
It seems that you don't have any idea what your doing. Benefit of the doubt with regards to 'homework' question.
Code:
int sortByTime(char * arr[])
{
struct stat sb1,sb2;
int i, j;
char * temp;
for(i = (sizeof(arr)/sizeof(char *)); i >= 1; i--)
{
for(j = 1; j < (i - 1); j++)
{
stat(arr[j], &sb1);
stat(arr[j+1], &sb2);
if(sb1.st_mtime > sb2.st_mtime)
{
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], temp);
}
}
}
return 0;
}
With the correct include files, this compiles without errors. I did not check if it works.
Last edited by Wim Sturkenboom; 03-15-2006 at 11:08 PM.
Now, for the next question (probably even easier):
Code:
(sizeof(arr)/sizeof(char *))
This is giving me trouble in the above code.
I can access items within arr with the following:
Code:
printf("%s", arr[10])
So I know there is more than one element in the array. However, first code sample results in 1, meaning that the size of the array is 4 and the size of char* is 4 (normal). Why would it say the array size is 4, when I can access things up to [50]?
And "yes", it is true that I have almost no idea what I am doing in C. I am being forced to learn it and program with it, but it is not being taught to me.
Last edited by smoothdogg00; 03-15-2006 at 11:39 PM.
arr[] is an array (of pointers to char) so arr is a pointer
For that reason it has a size of 4 (on 32bit systems), In your situation, you have to pass the number of elements as a second argument.
The code that you showed did not cause segfaults on my system. It however does not sort correctly due to pointer problems. This might cause the segfault or it might not (in which case you have to look in code that you did not post).
The sort-problem is in your compare routine. Use of a debugger or printf statements will help.
debugging using printf
----------------------
First add a simple printf as the first line after the variable declaration in the compare function.
Code:
printf("entering compare\n");
return 0;
Using the return, we force to skip the rest of the compare function. If you don't get a segfault, you're problem is (very likely) in the compare function.
Next add the following code before the new return
Code:
printf("%s | %s\n", (char*)s1,(char*)s2);
This might cause a segfault or print garbage which is an indication that there's a pointer problem.
Next you can start printing pointers. First change the %s in the above printf by %p
Code:
printf("%p | %p\n", (char*)s1,(char*)s2);
Also add a loop before you call qsort and print out the content of filelist. Mine code looked like
You will now see that the pointers don't have any relation while you expect to see the values printed in the loop to show in the compare as well (s1 and s2). There also should not be a segfault (to my knowkedge)
explanation
-----------
Your compare routine takes two pointers to a void; however, you're passing an array of pointers as the first argument of qsort and for that reason your arguments in the compare-function are pointers to a pointer (and not pointers to void).
Some small changes solve it
The compare function is a function that you will need to declare, that will determine how you compare two values. If the first value is to be considerd greater that then second then it should return a positive number. So a simple implementation for integers might just be
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.