Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

invalid conversion form 'int' to 'int*' ?

Name: Anonymous 2007-12-12 7:18

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:

#include <stdio.h>
#include <math.h>
#include <conio.h>

void remplir(int tab[5], int i)
{
     for(i=0; i<=4; i++)
     {
              scanf("%d",&tab[i]);
     }
}

void afficher(int tab[5], int i)
{
     for(i=0; i<=4; i++)
     {
              printf("%d",tab[i]);
     }
}

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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-12-12 8:23

>>8
eventhough I still don't quite understand the pointer thing
Stick to Visual Basic.

Name: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-12-12 8:36

This is what happens when you teach kids Sepples without teaching them C first.

Name: Anonymous 2007-12-12 8:49

>>11
I don't know what K&R is

Bad troll.

Name: Anonymous 2007-12-12 8:54

>>8

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:

---------------------------
 location | value | symbol
---------------------------
 1000       1005    tab
 1001       2       rep
 1002       0       i
 1003       0       j
 1004       0       k
 1005       0       tab[0]
 1006       0       tab[1]
 1007       0       tab[2]
 1008       0       tab[3]
 1009       0       tab[4]
 1010       0       tab[5]
---------------------------


You can see that tab (a variable of type int *) points to an array of actual ints, because it contains the memory location of the first element.

In practice, it's a little more complicated than this (as you can see if you run your program through a disassembler), but this is the general idea.

Name: Anonymous 2007-12-12 9:18

>>11
If your course is in C++, then why is your code written in C?

Name: Anonymous 2007-12-12 10:47

I still don't quite understand the pointer thing
learn an assembly language, it worked for me.

Name: Anonymous 2007-12-12 12:09

>I still don't quite understand the pointer thing
Alors reste sous Visual Basic, Java ou un autre language inférieur.

Protip : Lis le STRUCTURES ET INTERPRETATION DES PROGRAMMES INFORMATIQUES

Name: Anonymous 2007-12-12 14:55

>>16
And you turned out okay, right?

Name: Anonymous 2007-12-12 17:20

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

Name: Anonymous 2007-12-12 17:21

Name: Anonymous 2007-12-12 17:29

Name: Anonymous 2007-12-12 19:00

K AND R stands for KERNIGHAN AND RITCHIE, the inventors of the C language

Name: Anonymous 2007-12-12 19:12

>>21
K&R HERE

Name: Anonymous 2007-12-13 3:11

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

Name: Anonymous 2007-12-13 4:25

>>22
I invented that.

Name: Anonymous 2007-12-13 4:57

Ritchie invented it and kernighan was only involved in the documentation

Name: Anonymous 2007-12-13 8:14

case 1:remplir(tab[5],i);break;

tab[5] is an in. You want to pass an array. Use

void blah(int[] tab);

Also, don't pass in local variables! Just create an i in the scope of the function you're going to be using it in.

Name: Anonymous 2007-12-13 8:26

void blah(int[] tab);
GTFO.

Name: Anonymous 2007-12-13 8:34

>>28
Yeah. Too much c# I suppose.

Name: Anonymous 2007-12-13 15:23

wow, you are doing several things wrong!

1. use local variables in functions instead of passing them as parameters:

void remplir(int tab[5], int i)
{
     for(i=0; i<=4; i++)

better:

void remplir(int tab[5])
{
     for(int i=0; i<=4; i++)

(repeat for the other functions=

2. the pointer thingy was mentioned before:
array[index] returns element
array itself = pointer to first element

3. return something:
int sommme(int somme, int tab[5], int i)

you never return something there
apply 1. (local variables) to int somme and return it then (return somme;)

final result in main:
case 3: printf("%d", sommme(tab[5]); break;

4. main():
--> void main()

you will figure these things out over tiem I guess!

Name: Anonymous 2007-12-13 16:14

for(int i=0; i<=4; i++)
Not valid ANSI C code.
--> void main()
Oh, I see. You're just trolling. Nevermind, then.

Name: Anonymous 2007-12-13 16:34

>>31
where does he state he needs ANSI C?

Name: Anonymous 2009-03-18 2:46

I wants lots and lots of some delectable pot!

Marijuana MUST be legalized.

Name: Anonymous 2009-08-16 23:33

Lain.

Name: Anonymous 2010-11-28 2:22

Don't change these.
Name: Email:
Entire Thread Thread List