hlx
hlx

Reputation: 192

C strcpy and char

I need to write a code that will to this: You enter names and first names and a grade. Only for the grade >= 10, you print the names and firstname of the student with a backward sort. Exemple:

Bob
Dylan
12

Robert
Patt
9

Chris
Strozy
15

Josh
Sta
11

will give :

Chris Strozy 15 Bob Dylan 12 Josh Sta 11.

My errors are on the strcpy lines :

too few arguments to function 'strncpy'| assing argument 1 of 'strncpy' makes pointer from integer without a cast

 char tab_nom[N][M] ;
 char tab_prenom[N][M] ;
 float tab_notes[N];
 char tmp_n, tmp_p;
 int i,j,tmp;

for (i=0;i<N;i++)
 {
  printf("Saisissez le nom %d :", i+1);
  scanf("%s",tab_nom[i]);
  printf("Saisissez le prenom %d :", i+1);
  scanf("%s",tab_prenom[i]);
  printf("Saisissez la note %d :", i+1);
  scanf("%f",&tab_notes[i]);
  }

 for (i=0;i<N;i++)
 {
      for(j=0; j< N-1 ; j++)
      {
          if (tab_notes[j] < tab_notes[j+1])
          {
              tmp=tab_notes[j];
              tab_notes[j]=tab_notes[j+1];
              tab_notes[j+1]=tmp;

              strcpy(tmp_n,tab_nom[j]);
              strcpy(tab_nom[j],tab_nom[j+1]);
              strcpy(tab_nom[j+1],tmp_n);

              strcpy(tmp_p,tab_prenom[j]);
              strcpy(tab_prenom[j],tab_prenom[j+1]);
              strcpy(tab_prenom[j+1],tmp_p);

          }
      }
}

Upvotes: 0

Views: 6950

Answers (3)

taufique
taufique

Reputation: 2751

temp_n is a single char. strcpy takes char*

Upvotes: 2

mihirj
mihirj

Reputation: 1219

First of all, you are using strncpy and not strcpy.

Definition of strncpy is as follows

char *strncpy(char *restrict s1, const char *restrict s2, size_t n);

so I think you would like to change your code as

strncpy(tmp_n,tab_nom[j],sizeof(tmp_n));

Else, you can use strcpy(tmp_n, tab_nom[j]); without any compilation error.

Upvotes: 0

md5
md5

Reputation: 23737

strncpy(tmp_n, tab_nom[j]);
/* ... */
strncpy(tmp_p,tab_prenom[j]);

You are trying to copy a string in a single character... Moreover strncpy has a third argument (size). Try strcpy instead.

Upvotes: 0

Related Questions