0

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);

          }
      }
}
hlx
  • 182
  • 1
  • 4
  • 15

3 Answers3

2

temp_n is a single char. strcpy takes char*

taufique
  • 2,701
  • 1
  • 26
  • 40
  • Ok Should i declare tmp_n as a tmp_n[20] ? – hlx Oct 17 '12 at 13:07
  • 1
    yes. because you are copying a string to another STRING not char. Try using it. It should work if you don't have other mistake. – taufique Oct 17 '12 at 13:10
  • It doesn't work, the code returns nothing but an error code "C0000005" – hlx Oct 17 '12 at 14:00
  • 1
    what other change did you make to your code? if you replaced char temp_n with char temp[SIZE] then this code is not supposed to give any error. probably you made another mistake to do this task – taufique Oct 17 '12 at 14:12
  • It's the only change. There isnt any compilation error, It return a huge number on the first grade – hlx Oct 17 '12 at 14:21
  • 1
    Can you give me your code please so that I can run and check it. Don't paste it here. Paste in paste.ubuntu.com and after pasting give me the link. – taufique Oct 17 '12 at 14:34
  • It working now, just a basic change. Thank you, I appreciate you time :) – hlx Oct 17 '12 at 14:46
  • 1
    You are most welcome. And you should know the best way to thank here is upvote :-P – taufique Oct 17 '12 at 14:49
0
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.

md5
  • 23,373
  • 3
  • 44
  • 93
0

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.

mihirj
  • 1,199
  • 9
  • 15