LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-17-2006, 12:53 AM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Question Pointers/Passing arguments to functions question (C)


If I am passing an argument through a function, such as:
Code:
char * filelist[n];

sortByTime(filelist, files);
And receiving it as:
Code:
int sortByTime(char * arr[], int elem)
{
  struct stat s1, s2;
  int i, j, flag;
  char * temp;

  for(i = (elem); i > 0; i--)
    {
      for(j = 0; j < i; j++)
	{
	  stat(arr[j], &s1);
	  stat(arr[j + 1], &s2);

	  if(s1.st_mtime < s2.st_mtime)
	    {
	      temp = malloc(strlen(arr[j]) + 1);
	      strcpy(temp, arr[j]);

	      arr[j] = malloc(strlen(arr[j + 1]) + 1);
	      strcpy(arr[j], arr[j+1]);

	      arr[j + 1] = malloc(strlen(temp) + 1);
	      strcpy(arr[j + 1], temp);
	    }
	}
    }
Shouldn't the array then be sorted when I return to the main function, since I'm doing it through a pointer?

P.S. I know that it is getting rearranged within the sort function because I have printed the contents within the function and it prints out correctly.

Last edited by smoothdogg00; 03-17-2006 at 12:54 AM.
 
Old 03-17-2006, 09:42 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
The only problem I can think of with this is the fact that you copy the data for the strings; that's not necessary since the leftmost dimension in your array holds a pointer to where the data really is in memory. Your sorting method also looks a little strange to me, but if it works, it works. This code sorts it right for me:
Code:
#include <stdio.h>
#include <string.h>

void sort(char* array[], int size) {
    int i = 0, j = 0;
    for (i = 0; i < size - 1; i += 1) {
        for (j = i + 1; j < size; j += 1) {
            if (strcmp(array[i], array[j]) > 0) {
                char* tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
            }
        }
    }
}

int main(int argc, char** argv) {
    char* array[7] = {"ruby", "python", "perl", "c", "java", "php", "cpp"};
    int i = 0;
    sort(array, 7);
    for (i = 0; i < 7; i += 1) {
        printf("%s\n", array[i]);
    }
    return 0;
}
It's not exactly the same as what you're doing, but it should be close enough that it would work.
 
Old 03-17-2006, 10:17 AM   #3
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,023

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
i think this is a paas by reference issue:

Code:
schneidz@lq:/temp/sd00> cat sd00.c
#include "stdio.h"

void sortval(int valx, int valy)
{
 valx = valy;
}

void sortref(int *refx, int *refy)
{
 *refx = *refy;
}

main(int argc, char *argv[])
{
 int x = 5, y = 7;
 sortval(x, y);
 printf("passed by value: \t x = %d \t y = %d \n", x, y);
 sortref(&x, &y);
 printf("passed by reference: \t x = %d \t y = %d \n", x, y);
}
schneidz@lq:/temp/sd00> cc sd00.c -o sd00.x
schneidz@lq:/temp/sd00> sd00.x
passed by value:         x = 5   y = 7
passed by reference:     x = 7   y = 7
schneidz@lq:/temp/sd00>
hope this helps,
 
Old 03-17-2006, 11:12 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
The problem is that you are passing a pointer to an array of pointer-to-chars. These pointers are uninitialized when you call this function. In the function you call malloc() on the pointers (thus changing what they point to), but since the pointer itself to this array is passed by value, the changes are lost as you return from the function. You need a pointer to a pointer to an array of pointers.
 
Old 03-17-2006, 01:20 PM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
But isn't my code exactly the same? And I get the right result even if I allocate new memory, like
Code:
char* tmp = strdup(array[i]);
array[i] = strdup(array[j]);
array[j] = strdup(tmp);
It's admittedly not that bright, but it still works.
 
Old 03-17-2006, 01:27 PM   #6
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Angry

For some reason, when I compare two st_mtimes as follows:
Code:
if(s1.st_mtime < s2.st_mtime)
   switch arr[j] and arr[j+1]
If I print the results, it is saying something like 1564620778 < 2286208, and then it still performs the switch.

Then, I tried casting to:
Code:
if( (int)s1.st_mtime < (int)s2.st_mtime )
   switch arr[j] and arr[j + 1]
I know the swapping is working, thats no longer a problem thanks to everyone's help. But this comparing of numbers is ridiculous.

Are they somehow signed ints?

They shouldn't be signed if they are supposed to be the number of seconds since Unix was created (or whatever it is).
 
Old 03-17-2006, 01:44 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Without seeing the exact code it is hard to judge what is wrong.

Have you a rogue semicolon
Code:
if ( a < b);
Have you not included braces

Code:
if ( a < b )
  printf("sometimes %i", a);
  printf("always %i", b);
 
Old 03-17-2006, 01:46 PM   #8
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
I figured it out, it had to do with the paths I was sending to s1 to access s1.st_mtime.

Thank you everyone for all of your help and suggestions!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 01:37 AM
Linked Lists: Pointers sent to functions as arguments do not change their valur Jose Muņiz Programming 3 01-12-2004 07:45 PM
question on pointers to functions h/w Programming 3 10-06-2003 04:51 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM


All times are GMT -5. The time now is 03:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration