1

getting this error:

1>c:\users\b1021568\documents\visual

studio 2010\projects\tarefa42\tarefa
42\main.cpp(112): error C2664: 'cria_aluno' : cannot convert parameter 2 from 'const char [7]' to 'char' 1> There is no context in which this conversion is possible

when trying to compile this:

int main(void)
{
    Aluno *a[5];

    a[0] = cria_aluno(1, "turma1", "Joao", 7.0, 8.4, 4.3);
    a[1] = cria_aluno(2, "turma2", "Maria", 3.2, 5.1, 10.0);
    a[2] = cria_aluno(3, "turma3", "Rafael", 8.1, 3.2, 4.5);
    a[3] = cria_aluno(4, "turma4", "Jose", 1.3, 7.7, 9.3);
    a[4] = cria_aluno(5, "turma5", "Lais", 4.5, 1.3, 9.9);

    ordena(5, a);
return 0;
}

thats my cria_aluno function:

Aluno *cria_aluno(int mat, char turma, char nome, float p1, float p2, float p3)
{
    Aluno *a;

    a = (Aluno*) malloc(sizeof(Aluno));
    if(a == NULL)
    {
        printf("Memoria insuficiente");
        return NULL;
    }
    a->mat = mat;
    a->turma = turma;
    strcpy(a->nome, nome);
    a->p1 = p1;
    a->p2 = p2;
    a->p3 = p3;
    return a;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 3
    Show us the definition of Aluno. –  May 09 '11 at 11:51
  • Maybe also translating your code to english might help you get help. – RedX May 09 '11 at 11:53
  • 2
    @RedX - The code's not in English or any other language, it's in C. I don't know what the words mean (though I suppose `nome` means `name`) but if you can't understand the code you won't be able to answer the question no matter what language the variable names are in. – Chris Lutz May 09 '11 at 11:56
  • @Chris I do understand the code and i even know the language it's written in. But if it was calling a bunch of functions it would be better if you didn't have to read every function to know what it wants to accomplish. And sometimes people want to do one thing and do another thing in the code. These bugs you can't catch unless you know what his intention was. – RedX May 09 '11 at 11:58
  • `c++` tag added: `C` and `C++` are two different languages with different ways to do things. You appear to be using `C++` ("turma1" is of type `char[7]` in `C` and of type `const char[7]` in `C++`): you're better off using `std::string`. If you really want `C`, I suggest you set up your compiler to compile `C`. – pmg May 09 '11 at 14:07

4 Answers4

3

Change it to

Aluno *cria_aluno(int mat, const char* turma, const char* nome, float p1, float p2, float p3)

"turma1", etc. are of type const char*, not char

MByD
  • 135,866
  • 28
  • 264
  • 277
1

Change

Aluno *cria_aluno(int mat, char turma, char nome, float p1, float p2, float p3)

to

Aluno *cria_aluno(int mat, const char* turma, const char* nome, float p1, 
                  float p2, float p3)
{
   Aluno *a = (Aluno*) malloc(sizeof(Aluno));
   if(a == NULL)
   {
      printf("Memoria insuficiente");
      return NULL;
   }
   a->mat = mat;
   a->turma = malloc(strlen(turma)+1);
   strcpy(a->turma, turma);
   a->nome = malloc(strlen(nome)+1);
   strcpy(a->nome, nome);
   a->p1 = p1;
   a->p2 = p2;
   a->p3 = p3;
   return a;
}
rtn
  • 127,556
  • 20
  • 111
  • 121
  • 2
    Not so fast - you don't know the type of `a->turma`. You may have to `malloc` some room for it first (or maybe a pointer copy is what the OP wants here.) – Chris Lutz May 09 '11 at 11:54
  • I agree, but the immediate problem here is that it doesn't compile, not to rewrite all his code. – rtn May 09 '11 at 12:04
  • If `a->turma` and `a->nome` are pointers, they'll have garbage. The code will compile but will crash almost immediately, and he'll be right back here asking essentially the same question. – Chris Lutz May 09 '11 at 12:07
  • Yes yes, I'm aware of this. Added some malloc :) – rtn May 09 '11 at 12:09
1

Your function expect as parameter 2 and 3 char type and not char pointer (char*, usually used as "string").

In you main function, you called cria_aluno with char* type (string) as parameter 2 and 3, that is the cause of your error.

First you need to decide what you wish to store in Aluno structure. Lets take turma as example:

If you wish to store a single character, you should use char as the type of turma in the structure and in the function. Also, in the function call, you should use a single character as parameter 2, for example: 'a'. To copy this character, you should use a simple copy: a->turma = turma;

If you wish to store a string, you should use char[x] (where x is the max string length + \0 at the end) as the type of turma in the structure. In the function, you should use char* (const char* will be better). In the function call, you can use a string (example: "example"). To copy this attribute, you should use strcpy.

Another way to store turma in your structure as string mode, is change the type to char* in the structure. Then, when needed, allocate the memory.

Good luck

Amir

0

In the function call

a[0] = cria_aluno(1, "turma1", "Joao", 7.0, 8.4, 4.3); 

"turma1" and "Joao" are string literals, which are arrays of char (const char in C++). The types of the two expressions are char [7] and char [5], respectively. These types are not compatible with char, which is what you've declared turma and nome to be in cria_aluno, hence the error.

In most circumstances, array expressions have their types implicitly converted from "N-element array of T" to "pointer to T". So what actually gets passed to cria_aluno are two expressions of type char *, not char. Thus, you need to change the declaration of cria_aluno to

Aluno *cria_aluno(int mat, const char *turma, const char *nome, float p1, float p2, float p3)

Why const char * instead of char *? This helps protect you from accidentally modifying the contents of what the pointer points to; attempting to modify the contents of a string literal leads to undefined behavior.

John Bode
  • 119,563
  • 19
  • 122
  • 198