2014-03-29 62 views
-1
typedef struct 
{ 
    char name[50]; 
    int age; 
    int sex; 
} Person ; 

void sortAge(Person x[],int n) 
{ 
    printf("Age sort: \n"); 
    int i,j; 
    for(i=0;i<n;i++) 
    { 
     for(j=i+1;j<n;j++) 
     { 
      if (x[i].age > x[j].age) 
      { 
       int temp = x[i].age; // I change the age 
       x[i].age = x[j].age; 
       x[j].age = temp; 

       temp = x[i].sex; // I change the sex 
       x[i].sex = x[j].sex; 
       x[j].sex = temp;    

       // how I can use the same to change the names? 
       // tried strcpy but no work :/ 
      } 
     } 

    } 

阵列内的排序字符串使用strcpy函数如何气泡结构

... 
char temp2[50]; 
strcpy(temp2,x[i].name); 
etc... 

我得到这个错误..

56 27 C:\Users\**\Desktop\Untitled1.cpp [Error] 'strcpy' was not declared in this scope 
+0

使用'Person'临时变量并使用'memcpy'复制结构,而不是结构的内容会更容易。 – AntonH

+0

'strcpy()'的原型在''中。只是'#include '而你所说的错误应该会消失。因为你正在写头文件,所以你可能还需要''#include ''scanf()'和'printf()';) – pmg

+2

'Person temp = x [i]; x [i] = x [j]; x [j] = temp;' – BLUEPIXY

回答

2

误差.. 56 27 C:\用户** \ Desktop \ Untitled1.cpp [错误]'strcpy'未在此范围内声明

您应该在源文件的开头包含<string.h>