Hello ladies and gents, could someone explain what I'm doing wrong here, the ideas of this excercise is:

- Declare an array with 10 firstnames using pointers!

- Show the array on screen using a function!

- Sort out the names alphabetically and show the array again sorted!

I want to try and write the program first just getting the ten names into the array using pointers, but there seems to be something wrong, message I keep getting is:
error C2664: 'inhoud' : cannot convert parameter 1 from 'char [10]' to 'char *[]'

I tried to use the library MSDN, but I don't fully understand the example that is being given in it?

This is my code:

void inhoud(char *naam[]);

int main()
{
	char naam[10];	

	cout<< "Voer tien voornamen in: " <<endl;
	
	inhoud(naam);	

	return 0;
}

void inhoud(char *naam[])
{
	for (short i=0; i<10;i++)
	{
		naam [i]= new char [20];
		cin.getline(naam[i], sizeof naam);
	}
}

Could someone please explain this please, I don't want someone to give the solution, but if you could give me an example how it should be written and why you wrote it that way, I would appreciate it alot. ;)

char naam[10];

should be
char *naam[10];
explanation:
u want to declare an array of pointers to char. U were declaring just an array of char.

and as to sorting, use the STL for that.
Put your strings into a list and call sort on that (though your teacher may want you to write your own sorting function, he should have told you specifically if he doesn't want you to explore and find out that the language has one built in IMO (*)).

(*) whether your teacher appreciates such an approach depends on how well you relate to that teacher.
With my teachers in the past they might not have liked it and said so but they would not hold it against me ever. In the next course they probably changed the assignment description, I never checked :)

Thanks Asif,

It's working!

Got another question tough, when using new, it is best to give the used memory back and doing so is by using delete(naam)!

Since the creation of the dynamic memory ( new ) is in the function, I assumed that I need to put ( delete ) into the function aswell, correct?

@jwenting, euh, I'll let you guys see how I did it as soon as I'm tackling that part ok :lol:

wenting, hum, by anychance Dutch?

U deallocate only those memory that u have allocated dynamically. That is u delete those memory that u have allocated using new. But ur array of pointers to char (naam) is not dynamically allocated. So u must not use delete naam.

But the naam array contains 10 pointers. Each of these 10 pointers point to DYNAMICALLY ALLOCATED memory (more specifically to a char).

naam = new char [20];

Which u might want to deallocate afterwards like this
delete [] naam;

Thanks again for the explanation, two other questions about it:

1) Why can't I write, delete naam then, since it is linked towards the array of pointers?

2) When placing the delete [] naam into my program, am I correct in assuming it should be in the function, alltough, I'm not sure because by doing so I would automatically delete the array of pointers without it even getting to main correct?

So, does it have to be stated as last part in main?

1) Why can't I write, delete naam then, since it is linked towards the array of pointers?

>> I told u that u cant use
delete naam;
but u CAN use
delete naam;
however as far as i know(i m not sure though) delete naam will delete the memory allocated for the first character pointed by naam. Suppose naam points to the string "rock"; then the statement delete naam will deallocate the memory only for the first character of "rock" namely for "r". But the rest of the memory spaces will not be deallocated. so "ock" and rest of memory bytes that u allocated will not be deallocated.
but using
delete [] naam
will make sure all the memory bytes assigned for naam(in ur case 20 bytes) is deallocated.

2) When placing the delete [] naam into my program, am I correct in assuming it should be in the function, alltough, I'm not sure because by doing so I would automatically delete the array of pointers without it even getting to main correct?

>>u should delete memory when u think it is no longer necessary for the program. Ur memory was allocated in the function inhoud. so the deletion should come after this function. Whether u delete it just before the program ends(at the end of main) or somewhere else depends on u. My idea is that for ur particular problem u might write this just before the return statement of main:

for(int i = 0; i < 10; ++i)
delete [] naam;

I know you told me I couldn't use delete (naam), I wasn't referring to that, I refered towards delete naam

I understand what the difference is now between delete naam and delete[] naam[].

Thanks for the help, much appreciated!

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

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