Sorting arrays of pointers with function?

Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Sorting arrays of pointers with function?

 
0
  #11
Nov 23rd, 2004
Originally Posted by Dave Sinkula
How do you declare the array that you are passing to the function?
Well, that's an array, but one of pointers correct?

I'm not that hip on sorting, but it looked like the bubble sort to me.
Link isn't working.

Your assumption of -1, 0, 1 as return values is not entierly accurate for strcmp.
Ok, I'll read this one, if it's not to much trouble, could you explain the things Ive asked in my previous post, thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting arrays of pointers with function?

 
0
  #12
Nov 23rd, 2004
>Well, that's an array, but one of pointers correct?
Post code. I really hate playing 20 questions.

>Link isn't working.
It is for me. Google for "bubble sort". [To me, I hear an echo of "kuckoo for Cocoa Puffs". Wierd.]

>Ok, I'll read this one, if it's not to much trouble, could you explain the things Ive asked in my previous post, thanks in advance.
>> I'm not that hip on sorting, but it looked like the bubble sort to me.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Sorting arrays of pointers with function?

 
0
  #13
Nov 23rd, 2004
1) Post code. I really hate playing 20 questions.
Declaration: char *naam[10];

2) Google for "bubble sort".
Ok, will do
[To me, I hear an echo of "kuckoo for Cocoa Puffs". Wierd.]
Nope, you've got me, don't understand this, tough, I do get the feeling it's written with a twitch of sarcasm right :mrgreen:

3) I'm not that hip on sorting, but it looked like the bubble sort to me.
Guess you're telling me to check out bubble sort, though, not sure about it since I'm not native English speaking, thanks anyway.

You could say, subject closed
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting arrays of pointers with function?

 
0
  #14
Nov 23rd, 2004
Originally Posted by JoBe
1) Post code. I really hate playing 20 questions.
Declaration: char *naam[10];
::throws up hands::

I was trying to see if you to understand the difference between these:
#include <stdio.h>
#include <string.h>

void mysort(const char *name[], size_t size)
{
   size_t i, j;
   for ( i = 0; i < size - 1; ++i )
   {
      for ( j = i; j < size; ++j )
      {
         if ( strcmp(name[i], name[j]) > 0 )
         {
            const char *swap = name[i];
            name[i] = name[j];
            name[j] = swap;
         }
      }
   }
}

void show(const char *name[], size_t size)
{
   size_t i;
   for ( i = 0; i < size; ++i )
   {
      printf("%p:%s\n", (void*)name[i], name[i]);
   }
   putchar('\n');
}

int main ( void )
{
   const char *array[] = { "Jeff", "Dave", "Vlad", "Jose", "Mike" };
   size_t size = sizeof array / sizeof *array;
   show(array, size);
   mysort(array, size);
   show(array, size);
   return 0;
}

/* my output
0040A143:Jeff
0040A148:Dave
0040A14D:Vlad
0040A152:Jose
0040A157:Mike

0040A148:Dave
0040A143:Jeff
0040A152:Jose
0040A157:Mike
0040A14D:Vlad
*/
#include <stdio.h>
#include <string.h>

#define SIZE 20

void mysort(char (*name)[SIZE], size_t size)
{
   size_t i, j;
   for ( i = 0; i < size - 1; ++i )
   {
      for ( j = i; j < size; ++j )
      {
         if ( strcmp(name[i], name[j]) > 0 )
         {
            char swap[SIZE];
            strcpy(swap, name[i]);
            strcpy(name[i], name[j]);
            strcpy(name[j], swap);
         }
      }
   }
}

void show(const char (*name)[SIZE], size_t size)
{
   size_t i;
   for ( i = 0; i < size; ++i )
   {
      printf("%p:%s\n", (void*)name[i], name[i]);
   }
   putchar('\n');
}

int main ( void )
{
   char array[][SIZE] = { "Jeff", "Dave", "Vlad", "Jose", "Mike"};
   size_t size = sizeof array / sizeof *array;
   show(array, size);
   mysort(array, size);
   show(array, size);
   return 0;
}

/* my output
0012FF28:Jeff
0012FF3C:Dave
0012FF50:Vlad
0012FF64:Jose
0012FF78:Mike

0012FF28:Dave
0012FF3C:Jeff
0012FF50:Jose
0012FF64:Mike
0012FF78:Vlad
*/

>why do you need two different variables i and j ?
Get your code 'working' with one index and you will find that it doesn't work.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

Re: Sorting arrays of pointers with function?

 
0
  #15
Nov 23rd, 2004
Focus on learning the syntax and what you can and cant do. Then study the STL. Spend more time coding and less time writing algorithms.
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. list<string> names;
  9. list<string>::iterator p;
  10. string a;
  11.  
  12. for( int i = 0; i <= 10; i++ ) {
  13. cout << "Enter a name: ";
  14. cin >> a;
  15. names.push_back(a);
  16. names.sort();
  17. }
  18.  
  19. for( p = names.begin(); p != names.end(); ++p )
  20. cout << *p << endl;
  21.  
  22. return 0;
  23. }
Hope this helps!
big146
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Sorting arrays of pointers with function?

 
0
  #16
Nov 24th, 2004
@ Dave Sinkula, do I understand the difference, not entirely no :-|

But thanks for the two different examples, I'm going to try and see how they work and see wether by this I can grasp the difference between the two and more importantly, try and understand why it has to be written like that. Just a pitty I can't ask those twenty questions, seems your the person who could bring clarity intoo this subject

@ big146, thanks for trying to help, but your example makes me confused even more, to me it seems your using items wich are related towards structures, tough we have seen them aswell, I'm trying to understand pointers and mixing them with structures isn't really helpfull tough I appreciate your effort. Thing is, I wish we got more time to learn the different subjects of C++, but fact is, we're learning structured programming and Object Oriented programming within a time period of FOUR (4) months :!:

Not only that, we also have to make assignements in a certain period wich count for are final exams.

My personnal feeling about it is that alltough I understand the overall syntax, there are manny different subjects wich are not 100% clear to me from wich pointers is the one that gives me the biggest problems.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Sorting arrays of pointers with function?

 
0
  #17
Nov 24th, 2004
Ok, here's another way to think about pointers.....

Imagine you are at a dog show and there's a row of dogs in front of you. The judge says he wants to see the dogs in order of size. Your job is to deliver the dogs to her one at a time in order.

Do you:
A) move the dogs around so they stand in order of size and then fetch the dogs one by one from the smallest end to the largest end,
or
B) do you order their names on paper and then take the dogs in the order they are on the paper?

Both work. Moving dogs around can be hard work, though, so all things being equal it would be easier to do (B).

Well, in your sorting problem, (A) is like re-ordering the strings themselves; it is harder and more work.
(B) is like re-ordering the pointers to the strings. It is faster.

Both methods end up with a sorted array of strings/dogs, so both are valid for this problem. There may be some advantages to one or the other method in OTHER problems, but here they both get to the same result.

Now, back to the roster of the dogs:

1) Bruno
2) Fifi
3) Lizzi

Here, their number is like the pointer; the number isn't the dog, but it lets you know WHICH dog is meant. 2? That's Fifi. But, of course, it ISN'T fifi, it is only Fifi's number.

In your code, someone has allocated an array of strings. Let's say they are in memory this way:

char* naam[] = { "Fifi", "Bruno", "Lizzi" };

The compiler does something like this:

at memory location 1000: 'F'
at memory location 1001: 'i'
at memory location 1002: 'f'
at memory location 1003: 'i'
at memory location 1004: 0 (null terminator)
at memory location 1005: 'B'
at memory location 1006: 'r'
and so on.

So "Fifi" is at address 1000 in this example. "Bruno" is at 1005.

If you sort the STRINGS, since Bruno comes before Fifi, you may move the bytes from 1000 through 1004 ("Fifi<null>") to be at the bytes from 1005 through 1009. And you may move the bytes from 1005 through 1010 ("Bruno<null>") to be at the bytes from 1000 through 1005. (OOPS! We just overwrote the first byte of the second string!)

On the other hand, if you sort the POINTERS, swapping Bruno and Fifi means you swap Bruno's POINTER, 1005, with Fifi's POINTER, 1000. No problem, the strings have not moved, only their POINTERS have moved.

In this example, not only is this faster (swap two pointers rather than two arrays of charactors), but it is more likely to work in the case where the strings are of different lengths. Otherwise, you have to have strings that have some max length that you can trust to swap into (i.e. all strings have to at least be as long as the longest string).

Does this help?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting arrays of pointers with function?

 
0
  #18
Nov 24th, 2004
Originally Posted by Chainsaw
In your code, someone has allocated an array of strings. Let's say they are in memory this way:

char* naam[] = { "Fifi", "Bruno", "Lizzi" };

The compiler does something like this:

at memory location 1000: 'F'
at memory location 1001: 'i'
at memory location 1002: 'f'
at memory location 1003: 'i'
at memory location 1004: 0 (null terminator)
at memory location 1005: 'B'
at memory location 1006: 'r'
and so on.

So "Fifi" is at address 1000 in this example. "Bruno" is at 1005.
This is misleading. Here naam is an array of pointers, not an array of strings.
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int main ( void )
  5. {
  6. char* a[] = { "Fifi", "Bruno", "Lizzi"};
  7. char b[][6] = { "Fifi", "Bruno", "Lizzi"};
  8. size_t i, j;
  9. for ( i = 0; i < sizeof a / sizeof *a; ++i )
  10. {
  11. printf ( "&a[%d] = %p, a[%d] = \"%s\"\n",
  12. (int)i, (void*)&a[i], (int)i, a[i] );
  13. for ( j = 0; a[i][j]; ++j )
  14. {
  15. printf ( "&a[%d][%d] = %p, a[%d][%d] = '%c'\n", (int)i, (int)j,
  16. (void*)&a[i][j], (int)i, (int)j, a[i][j] );
  17. }
  18. }
  19. for ( i = 0; i < sizeof b / sizeof *b; ++i )
  20. {
  21. printf ( "&b[%d] = %p, b[%d] = \"%s\"\n",
  22. (int)i, (void*)&b[i], (int)i, b[i] );
  23. for ( j = 0; j < sizeof *b / sizeof **b; ++j )
  24. {
  25. printf ( "&b[%d][%d] = %p, b[%d][%d] = ", (int)i, (int)j,
  26. (void*)&b[i][j], (int)i, (int)j);
  27. printf ( isprint (b[i][j] ) ? "'%c'\n" : "%d\n", b[i][j] );
  28. }
  29. }
  30. return 0;
  31. }
The output of this program shows this difference.
  1. &a[0] = 0012FF80, a[0] = "Fifi"
  2. &a[0][0] = 0040A146, a[0][0] = 'F'
  3. &a[0][1] = 0040A147, a[0][1] = 'i'
  4. &a[0][2] = 0040A148, a[0][2] = 'f'
  5. &a[0][3] = 0040A149, a[0][3] = 'i'
  6. &a[1] = 0012FF84, a[1] = "Bruno"
  7. &a[1][0] = 0040A14B, a[1][0] = 'B'
  8. &a[1][1] = 0040A14C, a[1][1] = 'r'
  9. &a[1][2] = 0040A14D, a[1][2] = 'u'
  10. &a[1][3] = 0040A14E, a[1][3] = 'n'
  11. &a[1][4] = 0040A14F, a[1][4] = 'o'
  12. &a[2] = 0012FF88, a[2] = "Lizzi"
  13. &a[2][0] = 0040A151, a[2][0] = 'L'
  14. &a[2][1] = 0040A152, a[2][1] = 'i'
  15. &a[2][2] = 0040A153, a[2][2] = 'z'
  16. &a[2][3] = 0040A154, a[2][3] = 'z'
  17. &a[2][4] = 0040A155, a[2][4] = 'i'
  18. &b[0] = 0012FF6C, b[0] = "Fifi"
  19. &b[0][0] = 0012FF6C, b[0][0] = 'F'
  20. &b[0][1] = 0012FF6D, b[0][1] = 'i'
  21. &b[0][2] = 0012FF6E, b[0][2] = 'f'
  22. &b[0][3] = 0012FF6F, b[0][3] = 'i'
  23. &b[0][4] = 0012FF70, b[0][4] = 0
  24. &b[0][5] = 0012FF71, b[0][5] = 0
  25. &b[1] = 0012FF72, b[1] = "Bruno"
  26. &b[1][0] = 0012FF72, b[1][0] = 'B'
  27. &b[1][1] = 0012FF73, b[1][1] = 'r'
  28. &b[1][2] = 0012FF74, b[1][2] = 'u'
  29. &b[1][3] = 0012FF75, b[1][3] = 'n'
  30. &b[1][4] = 0012FF76, b[1][4] = 'o'
  31. &b[1][5] = 0012FF77, b[1][5] = 0
  32. &b[2] = 0012FF78, b[2] = "Lizzi"
  33. &b[2][0] = 0012FF78, b[2][0] = 'L'
  34. &b[2][1] = 0012FF79, b[2][1] = 'i'
  35. &b[2][2] = 0012FF7A, b[2][2] = 'z'
  36. &b[2][3] = 0012FF7B, b[2][3] = 'z'
  37. &b[2][4] = 0012FF7C, b[2][4] = 'i'
  38. &b[2][5] = 0012FF7D, b[2][5] = 0
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Sorting arrays of pointers with function?

 
0
  #19
Nov 24th, 2004
@ Chainsaw & Dave Sinkula,

Thanks for the help guys, very much appreciated!

Chainsaw, am I correct when saying that when following your example, the pointer directs to the first location(adress) of each dogs name and that therefore each dog has it's own pointer with the adress of the array where it's being started???

Therefore calling it an array of pointers wich have strings inside them?

Are *name and &name both referring too a certain adress in the memory?
Are *name[] or name[][] both referring to the actual string of names in the arrays?




And Dave, Ive tried out those two examples you gave me earlier and those are a big help, I'll try and work it out with the last example you gave me!

In this you are working with a declaration of a pointer char*a and one time referring to it with a two dimensional array correct?

Because char*a[] is the same as char a [][], in the first you are referring towards adresses wich contains arrays, second one is the same but with a two dimensional declaration of an array correct?

Both are referring to the letters inside the array, when wanting the adresses, you need to type in name or &name?

Anyway, I'll certainly give it a go
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting arrays of pointers with function?

 
0
  #20
Nov 24th, 2004
Originally Posted by JoBe
Because char*a[] is the same as char a [][]
No. This helps me with the difference.

Maybe I should have written it like this instead.
#include <stdio.h>
#include <ctype.h>

int main ( void )
{
   char* a[]    = { "Fifi", "Bruno", "Lizzi"};
   char  b[][6] = { "Fifi", "Bruno", "Lizzi"};
   size_t i, j;
   for ( i = 0; i < sizeof a / sizeof *a; ++i )
   {
      printf ( "&a[%d] = %p, a[%d] = %p\n",
               (int)i, (void*)&a[i], (int)i, (void*)a[i] );
      for ( j = 0; a[i][j]; ++j )
      {
         printf ( "&a[%d][%d] = %p, a[%d][%d] = '%c'\n", (int)i, (int)j,
                  (void*)&a[i][j], (int)i, (int)j, a[i][j] );
      }
   }
   for ( i = 0; i < sizeof b / sizeof *b; ++i )
   {
      printf ( "&b[%d] = %p, b[%d] = \"%s\"\n",
               (int)i, (void*)&b[i], (int)i, b[i] );
      for ( j = 0; j < sizeof *b / sizeof **b; ++j )
      {
         printf ( "&b[%d][%d] = %p, b[%d][%d] = ", (int)i, (int)j,
                  (void*)&b[i][j], (int)i, (int)j);
         printf ( isprint (b[i][j] ) ? "'%c'\n" : "%d\n", b[i][j] );
      }
   }
   return 0;
}
&a[0] = 0012FF80, a[0] = 0040A146
&a[0][0] = 0040A146, a[0][0] = 'F'
&a[0][1] = 0040A147, a[0][1] = 'i'
&a[0][2] = 0040A148, a[0][2] = 'f'
&a[0][3] = 0040A149, a[0][3] = 'i'
&a[1] = 0012FF84, a[1] = 0040A14B
&a[1][0] = 0040A14B, a[1][0] = 'B'
&a[1][1] = 0040A14C, a[1][1] = 'r'
&a[1][2] = 0040A14D, a[1][2] = 'u'
&a[1][3] = 0040A14E, a[1][3] = 'n'
&a[1][4] = 0040A14F, a[1][4] = 'o'
&a[2] = 0012FF88, a[2] = 0040A151
&a[2][0] = 0040A151, a[2][0] = 'L'
&a[2][1] = 0040A152, a[2][1] = 'i'
&a[2][2] = 0040A153, a[2][2] = 'z'
&a[2][3] = 0040A154, a[2][3] = 'z'
&a[2][4] = 0040A155, a[2][4] = 'i'
&b[0] = 0012FF6C, b[0] = "Fifi"
&b[0][0] = 0012FF6C, b[0][0] = 'F'
&b[0][1] = 0012FF6D, b[0][1] = 'i'
&b[0][2] = 0012FF6E, b[0][2] = 'f'
&b[0][3] = 0012FF6F, b[0][3] = 'i'
&b[0][4] = 0012FF70, b[0][4] = 0
&b[0][5] = 0012FF71, b[0][5] = 0
&b[1] = 0012FF72, b[1] = "Bruno"
&b[1][0] = 0012FF72, b[1][0] = 'B'
&b[1][1] = 0012FF73, b[1][1] = 'r'
&b[1][2] = 0012FF74, b[1][2] = 'u'
&b[1][3] = 0012FF75, b[1][3] = 'n'
&b[1][4] = 0012FF76, b[1][4] = 'o'
&b[1][5] = 0012FF77, b[1][5] = 0
&b[2] = 0012FF78, b[2] = "Lizzi"
&b[2][0] = 0012FF78, b[2][0] = 'L'
&b[2][1] = 0012FF79, b[2][1] = 'i'
&b[2][2] = 0012FF7A, b[2][2] = 'z'
&b[2][3] = 0012FF7B, b[2][3] = 'z'
&b[2][4] = 0012FF7C, b[2][4] = 'i'
&b[2][5] = 0012FF7D, b[2][5] = 0
The variable a is an array of pointers. Each of these pointers is initialized to the memory location of a string literal.

The variable b is an array of arrays of char. Each of these arrays is initialized with the contents of the string literal initializers. A string in C is a null-terminated array of char, so b is an array of strings.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC