错误 - 使用指针对数组中的名称进行排序的C程序

时间:2014-06-01 17:50:13

标签: c pointers

我正在尝试编写一个小型c程序来读取名称数组,并使用指针基于字母顺序对它们进行排序

有人可以看一下这段代码并验证它,

代码

#include<stdio.h>
#include<string.h>
#include<string.h>
void sort char a[][20], int n, int (*p)(const char*, const char*),void(*q)(char* char*);
int main(){
    char a[10],[20];
    int n,i;
    printf("Enter How Many Numbers you want:");
    scanf("%d",&n);
    void swap(char*,char*);
    printf("Enter strings:");
    fflush(n);
    for(i=0;i<n;i++)
    puts(a[i]);
}   
void sort char a[][20], int n, int (*p)(const char*, const char*),void(*q)(char* char*){
    int i,j;
    for(i=0;i<n-1;i++)
        for(j=i+1;j<n;j++)
            if(*p)(a[i],a[j]>0)
                (*q)[a(i),a(j)];
            return;

}
void swap(char *s1, char *s2){
    char t[20];
    strcpy(t,s1);
    strcpy(s1,s2);
    strcpy(s2,t);
    return;
}   

错误:

--------------------配置:mingw5 - CUI Debug,Builder类型:MinGW ---------------- ----

[Error] line4: error: expected initializer before "char"
[Error] line6: error: expected unqualified-id before '[' token
[Error] line12: error: invalid conversion from `int' to `FILE*'
[Error] line12: error:   initializing argument 1 of `int fflush(FILE*)'
[Error] line14: error: invalid conversion from `char' to `const char*'
[Error] line14: error:   initializing argument 1 of `int puts(const char*)'
[Error] line16: error: expected initializer before "char"
[Warning] line31:5: warning: no newline at end of file

Complete Make Untitled19: 7 error(s), 1 warning(s)

1 个答案:

答案 0 :(得分:1)

1    #include<stdio.h>
2    #include<string.h>
3    #include<string.h>
4    void sort char a[][20], int n, int (*p)(const char*, const char*),void(*q)(char* char*);
//        ^^^^ 
//[Error] line4: error: expected initializer before "char"
//The compiler questions why 'sort' is here. 
//Perhaps the line is missing parenthesis? Change to: 
//   void sort(char a[][20], int n, int (*p)(const char*, const char*),void(*q)(char* char*));

5   int main(){
6       char a[10],[20];
//                ^   
//[Error] line6: error: expected unqualified-id before '[' token
//The compiler is confused about the comma.  
//Perhaps the comma should be removed?

7       int n,i;
8       printf("Enter How Many Numbers you want:");
9       scanf("%d",&n);
10      void swap(char*,char*);
11      printf("Enter strings:");
12      fflush(n);
//             ^ 
//[Error] line12: error: invalid conversion from `int' to `FILE*'
//[Error] line12: error: initializing argument 1 of `int fflush(FILE*)'
//fflush(n) probably is not doing what was intended.  
//Perhaps the line should be removed?

13      for(i=0;i<n;i++)
14          puts(a[i]);
//               ^^^^  
//[Error] line14: error: invalid conversion from `char' to `const char*'
//[Error] line14: error:   initializing argument 1 of `int puts(const char*)'
//These errors are a result of the comma on line #6.

15      }   
16      void sort char a[][20], int n, int (*p)(const char*, const char*),void(*q)(char* char*){
//           ^^^^ 
//[Error] line16: error: expected initializer before "char"
//The compiler questions why 'sort' is here. 
//Perhaps the line is missing parenthesis? Change to: 
//   void sort(char a[][20], int n, int (*p)(const char*, const char*),void(*q)(char* char*))

17      int i,j;
18      for(i=0;i<n-1;i++)
19          for(j=i+1;j<n;j++)
20              if(*p)(a[i],a[j]>0)
21                  (*q)[a(i),a(j)];
22              return;
23  }
24
25  void swap(char *s1, char *s2){
26      char t[20];
27      strcpy(t,s1);
28      strcpy(s1,s2);
29      strcpy(s2,t);
30      return;
31  }   
//[Warning] line31:5: warning: no newline at end of file    
// Your compiler likes to have an extra blank line at the end of the file.
// It is unhappy to find the blank line missing.
// Add one (or more) blank lines to the end of the file to eliminate warning.

代码中还有其他一些需要注意的事情。继续努力,你可以通过练习做得更好。