Reputation: 11
There's a few students in a class. I need to sort the students in alphabetical order. Teacher sorts a number, which is a student number. I need to print the student's name. But I'm receiving the following errors: [Note] expected 'const char * restrict' but argument is of type 'char' [Warning] passing argument 1 of 'strcmp' makes pointer from integer without a cast The warning repeats for argument 2 of 'strcmp' and all arguments from 'strcpy'.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int N, K, k, i, j;
//students names
scanf("%d", &N);
//vector with students names
char vetor[N];
char aux[N];
//get string e load it on vetor
for(i=0; i<N; i++)
scanf("%s", vetor[i]);
//alphabetic order
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
//string comparison
if(strcmp(vetor[i], vetor[j]) < 0) {
//string copy
strcpy(aux[i], vetor[i]);
strcpy(vetor[i], vetor[j]);
strcpy(vetor[j], aux[i]);
}
}
}
//get sorted number
scanf("%d", &K);
K=vetor[K];
//print sorted student name
printf("%s", vetor[K]);
return 0;
}
Upvotes: 1
Views: 3978
Reputation:
You're using char
arrays of N
size, but you're trying to use a syscall that copies strings from one array to another, not single char
.
As far as your code respect, you can do:
if(strcmp(vetor, vetor) < 0) {
//string copy
strcpy(aux, vetor);
strcpy(vetor, vetor);
strcpy(vetor, aux);
}
Note that an array is like a pointer: it points at the first element of the array.
Upvotes: 0