| | |
Sorting arrays of pointers with function?
![]() |
•
•
•
•
Originally Posted by Dave Sinkula
How do you declare the array that you are passing to the function?
Link isn't working.
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.
>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.
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.
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
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
•
•
•
•
Originally Posted by JoBe
1) Post code. I really hate playing 20 questions.
Declaration: char *naam[10];
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.
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.
Hope this helps!
C Syntax (Toggle Plain Text)
#include <iostream> #include <list> #include <string> using namespace std; int main() { list<string> names; list<string>::iterator p; string a; for( int i = 0; i <= 10; i++ ) { cout << "Enter a name: "; cin >> a; names.push_back(a); names.sort(); } for( p = names.begin(); p != names.end(); ++p ) cout << *p << endl; return 0; }
big146
@ 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.
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.
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?
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?
•
•
•
•
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.
C Syntax (Toggle Plain Text)
#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] = \"%s\"\n", (int)i, (void*)&a[i], (int)i, 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; }
C Syntax (Toggle Plain Text)
&a[0] = 0012FF80, a[0] = "Fifi" &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] = "Bruno" &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] = "Lizzi" &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
@ 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
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
•
•
•
•
Originally Posted by JoBe
Because char*a[] is the same as char a [][]
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 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.
![]() |
Other Threads in the C Forum
- Previous Thread: recursive findaverage function for Binary search tree
- Next Thread: concatenating string and others
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






