Hi /prog/, I've got a problem with the program I just wrote, when I try to complie it, it says "invalid conversion form 'int' to 'int*'". Could you help me solve this please? Here's the code:
int sommme(int somme, int tab[5], int i)
{
somme=0;
for(i=0; i<=4; i++)
{
somme=somme+tab[i];
}
}
void tri(int tab[5], int b, int i, int j)
{
for(i=0; i<=4; i++)
{
for(j=0; j<=4; j++)
{
if(tab[i]<tab[j])
{
b=tab[i];
tab[i]=tab[j];
tab[j]=b;
}
}
}
}
main()
{
int tab[5];
int i,j,k,somme,rep,b;
do
{
printf("1.Remplir le tableau\n2.Afficher le tableau\n3.Afficher la somme des cases du tableau\n4.Afficher le tableau trié par ordre décroissant\n0.Quitter\nVotre choix:");
scanf("%d",&rep);
switch(rep)
{
case 1:remplir(tab[5],i);break;
case 2:afficher(tab[5],i);break;
case 3:sommme(somme,tab[5],i);break;
case 4:tri(tab[5],b,i,j);break;
default: printf("Votre choix n'est pas valide");break;
}
while(rep!=0);
}
getch();
}
Thank you in advance.
Name:
Anonymous2007-12-12 7:24
case 1:remplir(tab[5],i);break;
case 2:afficher(tab[5],i);break;
case 3:sommme(somme,tab[5],i);break;
case 4:tri(tab[5],b,i,j);break;
Here is your problem. Why are you passing the 6th element of tab to functions that expect a pointer to some ints?
Name:
Anonymous2007-12-12 7:28
>>2
I'm sorry, but I really don't understand what you're trying to say. I know you're trying to be helpful, but I didn't yet study the pointers (Im the frenchfag from two weeks or so, maybe you'll remember me), and we just finished learning about functions last week.
I would be very glad if you could provide more informations, how I should correct it and why. Thanks.
Name:
Anonymous2007-12-12 7:41
I looked on Goggles, but I didn't find anything about how to correct this error, I knew a lot of you know how to do this so please? Could you help me? *puppy eyes*
Name:
Anonymous2007-12-12 7:51
void remplir(int tab[5], int i)
This function expects an int pointer as first argument, but
remplir(tab[5],i);break;
is passing an integer, and not an int pointer, as first argument. Same for others.
Remove the [5] from the actual parameter and you should be set.
Name:
Anonymous2007-12-12 7:58
The thing is that [] is used in two different ways here. When declaring your variable it specifies the size, i.e. in int tab[5]; but everywhere else it means get the nth element of the array, so when you say tab[5] in other parts of your program it means give me the 6th element of the array tab. (It's the 6th because these array indexes start at 0.)
The type of the array tab is int* and the type of its elements in int, which is why you get the error.
Name:
Anonymous2007-12-12 8:12
You see the problem is is that c is an english-derived language. Using french function names just doesn't work.
Name:
Anonymous2007-12-12 8:21
Well thank you everyone, it works perfectly now, eventhough I still don't quite understand the pointer thing, but I'll take care of that later.
Thank you again.
Name:
Anonymous2007-12-12 8:23
>>8 eventhough I still don't quite understand the pointer thing
Stick to Visual Basic.
Name:
Anonymous2007-12-12 8:24
case 1:remplir(tab[5],i);break; case 2:afficher(tab[5],i);break; case 3:sommme(somme,tab[5],i);break; case 4:tri(tab[5],b,i,j);break;
/facepalm
WHY DON'T YOU READ K&R
Name:
Anonymous2007-12-12 8:30
>>10
Because I don't know what K&R is and my goal is not to become and expert programmer.
>>9
I would if I could, but my course is C++, so, yeah...
Name:
Anonymous2007-12-12 8:36
This is what happens when you teach kids Sepples without teaching them C first.
A pointer works because the number it contains is a memory location that references (points to) something else. For example, this might be how a compiler lays out the memory for your main function:
Because I don't know what K&R is and my goal is not to become and expert programmer.
Probably the greatest book ever written http://www.ica.luz.ve/dfinol/tpro/kandr.pdf(about C) I would if I could, but my course is C++, so, yeah...
Everything you've been posting so far is C... Also, VB is for retarded 13 year old boys
>>20 >>21
How many variations of this are there? I have two different eBooks of K&R and they're both different from these two. I haven't found one without any errors though. Someone actually took the effort of typing out the whole book instead of OCR'ing it.