Help with c errors
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 'c开发者_开发知识库onst 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;
}
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
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;
}
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
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.
精彩评论