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!

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);
		}
	}

}

Recommended Answers

All 19 Replies

Describe, in English sentences, what you think the data types are for the following:

  • naam
  • 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!

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, *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); = puts the name into a temporary place.

strcpy(*naam, *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?

- 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 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 and [i+1].

This is what Ive come up with:

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.

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, 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!

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 :!:

How do you declare the array that you are passing to the function?

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 ?

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

if ( strcmp(name, 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 ???????

Your assumption of -1, 0, 1 as return values is not entierly accurate for strcmp.

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.

>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.

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 :)

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.

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.

#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;
}

Hope this helps!

@ 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.

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?

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.

#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;
}

The output of this program shows this difference.

&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 ;)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.