| | |
Sorting arrays of pointers with function?
![]() |
Hello ladies and gents, I have tried to write my sorting function but keep on getting error messages.
Ive been reading the tutorials on this forum wich explaines alot but I'm still having lots of trouble in comprehending how pointers work and more important, how that I can correctly write the code that I need for them
Hope someone can help me out and explain with this piece of code what I'm doing wrong and how I have to change it and most important why it has to be changed!
Ive been reading the tutorials on this forum wich explaines alot but I'm still having lots of trouble in comprehending how pointers work and more important, how that I can correctly write the code that I need for them
Hope someone can help me out and explain with this piece of code what I'm doing wrong and how I have to change it and most important why it has to be changed!
C Syntax (Toggle Plain Text)
void sorteer(char *naam[]) { for (short i=0; i<9;i++) { for (short i=0; naam[i]!= '\0';i++) { char temp; stricmp(*naam[i], *naam[i+1]); strcpy(temp, *naam[i]); strcpy(*naam[i], *naam[i+1]); strcpy(*naam[i+1], temp); } } }
Right, you are confusing 'chars' with 'array of chars' and 'array of array of chars'. It would probably help yourself to describe what the variables are ("this is a char", "this is an array of chars") (to you) and then describe what you want to do (like "copy the array of chars xxxx to a temporary array of chars"), and match that with what the variables are and how you are modifying them (with * or []).
We could just tell you what we see wrong, but then you may still not get the issues; this will be a little work but it is vital that pointer usage becomes second nature to you if you are going to write anything significant in C or C++.
Good luck!
We could just tell you what we see wrong, but then you may still not get the issues; this will be a little work but it is vital that pointer usage becomes second nature to you if you are going to write anything significant in C or C++.
Good luck!
Ok guys,
I'll try to explain what my idea is/was with this function, I want to be able to sort ten names wich are randomly picked and put them in alphabetical order.
char temp= a temporary place to put the first of the names into so the actual first place of the name is free!
stricmp(*naam[i], *naam[i+1]); = this in my opinion will make a comparison of the first and second name to see wether the first name is should be in front of the second, if not it will switch the names by:
strcpy(temp, *naam[i]); = puts the name into a temporary place.
strcpy(*naam[i], *naam[i+1]); = puts the second name into the first place.
strcpy(*naam[i+1], temp); = puts the first name wich was transported to temp into the second place.
naam equals to me a firstname from a person.
temp equals to me as a temporary place to put a certain name wich isn't in it correct place.
Hope you guys understand what I mean by this explanation?
I'll try to explain what my idea is/was with this function, I want to be able to sort ten names wich are randomly picked and put them in alphabetical order.
char temp= a temporary place to put the first of the names into so the actual first place of the name is free!
stricmp(*naam[i], *naam[i+1]); = this in my opinion will make a comparison of the first and second name to see wether the first name is should be in front of the second, if not it will switch the names by:
strcpy(temp, *naam[i]); = puts the name into a temporary place.
strcpy(*naam[i], *naam[i+1]); = puts the second name into the first place.
strcpy(*naam[i+1], temp); = puts the first name wich was transported to temp into the second place.
naam equals to me a firstname from a person.
temp equals to me as a temporary place to put a certain name wich isn't in it correct place.
Hope you guys understand what I mean by this explanation?
Well temp is a single char, so you must be dealing with really trivial names. Similarly, your use of the string functions is wrong because you are feeding them single chars, not strings.
Or: you are not trying to swap names, you are only trying to swap letters. But this won't work with the functions you are using.
And since both of your loops use i, do you know which i you are using in the innermost loop?
Or: you are not trying to swap names, you are only trying to swap letters. But this won't work with the functions you are using.
And since both of your loops use i, do you know which i you are using in the innermost loop?
- Ive allready changed the char temp into char temp[20] so it can contain not only one letter but indeed the full name!
- If I'm feeding it with char's, then how do I feed him strings, that is the question I'm asking
- I was using the inner [i] to get from the first to the second name, tough, Ive come to realize that:
- don't need it, because I have one loop from 0-9.
- and stricmp is selecting number [i] and [i+1].
- If I'm feeding it with char's, then how do I feed him strings, that is the question I'm asking
- I was using the inner [i] to get from the first to the second name, tough, Ive come to realize that:
- don't need it, because I have one loop from 0-9.
- and stricmp is selecting number [i] and [i+1].
This is what Ive come up with:
My theorie on it:
I didn't need the second loop because the first as Ive said before is selecting the names one by one.
stricmp will give a value to resultaat of -1, 0 or 1, if the value is one, the selection will step in and threw use of strcpy the two names will get switched!
That's my theorie :!:
Problem is, it doesn't work, so I would really really really appreciate it, if someone could explain to me why it's not working.
C Syntax (Toggle Plain Text)
for(int i=0;i<9;i++) { int resultaat= stricmp(naam[i], naam[i+1]); if (resultaat==1) { strcpy(temp, *naam); strcpy(*naam, *(naam+i)); strcpy(*(naam+i), temp); } }
My theorie on it:
I didn't need the second loop because the first as Ive said before is selecting the names one by one.
stricmp will give a value to resultaat of -1, 0 or 1, if the value is one, the selection will step in and threw use of strcpy the two names will get switched!
That's my theorie :!:
Problem is, it doesn't work, so I would really really really appreciate it, if someone could explain to me why it's not working.
Are you sorting an array of strings? Or are you sorting an array of pointers to strings? Given the topic, I've assumed the latter.
C Syntax (Toggle Plain Text)
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; } } } }
Hi, the idea is just to put ten names, dave, johan, eric, kathy, ... ten names in alphabetical order, to do this I wanted to write this function in wich instead of arrays being used, it would be done by pointers, so selecting and putting them in alphabetical order would be done by pointers :!:
That's what I wanted to do, thanks for the example and yes, it is the second subject that I wanted to do
But, Ive got one more thing to ask, could you please explain the following, what and why you are doing this.
for ( i = 0; i < size - 1; ++i )
for ( j = i; j < size; ++j ) why do you need two different variables i and j ?
if ( strcmp(name[i], name[j]) > 0 ) You've got me here, why two different letters, I assume they are connected towards int i and int j, but why, how...
And why do you make it so that they both have to be bigger then 0 ???????
Thanks again for the help!
That's what I wanted to do, thanks for the example and yes, it is the second subject that I wanted to do
But, Ive got one more thing to ask, could you please explain the following, what and why you are doing this.
for ( i = 0; i < size - 1; ++i )
for ( j = i; j < size; ++j ) why do you need two different variables i and j ?
if ( strcmp(name[i], name[j]) > 0 ) You've got me here, why two different letters, I assume they are connected towards int i and int j, but why, how...
And why do you make it so that they both have to be bigger then 0 ???????
Thanks again for the help!
•
•
•
•
Originally Posted by JoBe
Hi, the idea is just to put ten names, dave, johan, eric, kathy, ... ten names in alphabetical order, to do this I wanted to write this function in wich instead of arrays being used, it would be done by pointers, so selecting and putting them in alphabetical order would be done by pointers :!:
•
•
•
•
Originally Posted by JoBe
But, Ive got one more thing to ask, could you please explain the following, what and why you are doing this.
for ( i = 0; i < size - 1; ++i )
for ( j = i; j < size; ++j ) why do you need two different variables i and j ?
•
•
•
•
Originally Posted by JoBe
if ( strcmp(name[i], name[j]) > 0 ) You've got me here, why two different letters, I assume they are connected towards int i and int j, but why, how...
And why do you make it so that they both have to be bigger then 0 ???????
![]() |
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 |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork forloop frequency givemetehcodez global gtkgcurlcompiling hacking highest homework i/o inches infiniteloop interest intmain() kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes radix read recv recvblocked repetition research scanf scheduling segmentationfault send sequential single socketprograming socketprogramming stack standard string suggestions systemcall turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






