1
votes

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;
}
1
aux[i] and vetor[i] are single characters, not strings as strcpy and strcmp expect - bruceg
try char vector[N][50]; char aux[50]; and in the if block, use strcpy(aux, vector[i]); strcpy(vector[j], aux); and the first for block may be changed to for(i=0 i < N-1; i++) and the second for block changed to for(j=i+1;j<N;j++) - Shiping
@DavidBowling, in the OP's code, aux and vetor designate char arrays, which, if null terminated, constitute strings. But as bruceg said, the expressions aux[i] and vetor[i] designate single characters. These do not decay to pointers, and cannot be considered strings. - John Bollinger

1 Answers

0
votes

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.