| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 400 人关注过本帖
标题:指针数组做函数参数
只看楼主 加入收藏
zph304
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-7-12
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
指针数组做函数参数
#define STUDENTS  5
#define SUBJECTS  4
#include<stdio.h>
#include<string.h>

void get_list(char *string[],int array[][SUBJECTS+1],int m,int n);
void get_sum(int array[][SUBJECTS+1],int m,int n);
void get_rank_list(char *string[],int array[][SUBJECTS+1],int m,int n);
void print_list(char *string[],int array[][SUBJECTS+1],int m,int n);
void swap_int(int *p,int *q);
void swap_string(char s1[],char s2[]);

void main()
{
    char name[STUDENTS][20];
    int marks[STUDENTS][SUBJECTS+1];
   
    printf("Input students names & their marks in four subjects\n");
    get_list(name,marks,STUDENTS,SUBJECTS);
    get_sum(marks,STUDENTS,SUBJECTS+1);
    printf("\n");
    print_list(name,marks,STUDENTS,SUBJECTS+1);
    get_rank_list(name,marks,STUDENTS,SUBJECTS+1);
    printf("\nRanked List\n\n");
    print_list(name,marks,STUDENTS,SUBJECTS+1);
   
}

void get_list(char *string[],int array[][SUBJECTS+1],int m,int n)
{
    int i,j,(*rowptr)[SUBJECTS+1]=array;
    for(i=0;i<m;i++)
    {
        scanf("%s",string[i]);
        for(j=0;j<SUBJECTS;j++)
            scanf("%d",&(*(rowptr+i))[j]);
    }
   
}

void get_sum(int array[][SUBJECTS+1],int m,int n)
{
    int i,j,(*rowptr)[SUBJECTS+1]=array;
    for(i=0;i<m;i++)
    {
        (*(rowptr+1))[n-1]=0;
        for(j=0;j<SUBJECTS;j++)
            (*(rowptr+i))[n-1]+=(*(rowptr+1))[j];
    }
}

void get_rank_list(char *string[],
                   int array[][SUBJECTS+1],
                   int m,
                   int n)
{
    int i,j ,k,(*rowptr)[SUBJECTS+1]=array;
    char*temp;
   
    for(i=1;i<=m-1;i++)
        for(j=1;j<=m-i;j++)
            if((*(rowptr+j-1))[n-1]<(*(rowptr+j))[n-1])
            {
                swap_string(string[j-1],string[j]);
               
                for(k=0;k<n;k++)
                    swap_int(&(*(rowptr+j-1))[k],&(*(rowptr+j))[k]);
            }
}

void print_list(char *string[],
                int array[][SUBJECTS+1],
                int m,
                int n)
{
    int i,j,(*rowptr)[SUBJECTS+1]=array;
    for(i=0;i<m;i++)
    {
        printf("%-20s",string[i]);
        for(j=0;j<n;j++)
            printf("%5d",(*(rowptr+i))[j]);
        printf("\n");
    }
}

void swap_int(int *p,int *q)
{
    int temp;
    temp=*p;
    *p=*q;
    *q=temp;
}

void swap_string(char s1[],char s2[])
{
    char swaparea[256];
    int i;
    for(i=0;i<256;i++)
        swaparea[i]='\0';
    i=0;
    while(s1[i]!='\0'&&i<256)
    {
        swaparea[i]=s1[i];
        i++;
    }
    i=0;
    while(s2[i]!='\0'&&i<256)
    {
        s1[i]=s2[i];
        s1[++i]='\0';
    }
    i=0;
    while(swaparea[i]!='\0')
    {
        s2[i]=swaparea[i];
        s2[++i]='\0';
        
    }
}

为什么出来F:\USER_PHZ\kaoshi.cpp(19) : error C2664: 'get_list' : cannot convert parameter 1 from 'char [5][20]' to 'char *[]'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\USER_PHZ\kaoshi.cpp(22) : error C2664: 'print_list' : cannot convert parameter 1 from 'char [5][20]' to 'char *[]'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\USER_PHZ\kaoshi.cpp(23) : error C2664: 'get_rank_list' : cannot convert parameter 1 from 'char [5][20]' to 'char *[]'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\USER_PHZ\kaoshi.cpp(25) : error C2664: 'print_list' : cannot convert parameter 1 from 'char [5][20]' to 'char *[]'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.对于指针数组做函数参数我现在看的书讲的不怎么样,哪位高手能解决下
搜索更多相关主题的帖子: include 
2013-07-12 17:38
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:10 
我只是帮楼主在函数声明的时候对第一个形参作了修改,因为下面的实参是二维字符串数组,所以将第一个形参修改为二维,至于能不能运行,没有试过!
程序代码:
#include<stdio.h>
#include<string.h>
#define STUDENTS  5
#define SUBJECTS  4
void get_list(char *string[][20],int array[][SUBJECTS+1],int m,int n);//将*string[]改为*string[][20]
void get_sum(int array[][SUBJECTS+1],int m,int n);
void get_rank_list(char *string[][20],int array[][SUBJECTS+1],int m,int n);//将*string[]改为*string[][20]
void print_list(char *string[][20],int array[][SUBJECTS+1],int m,int n);//将*string[]改为*string[][20]
void swap_int(int *p,int *q);
void swap_string(char s1[],char s2[]);

void main()

 {
     char *name[STUDENTS][20];
     int marks[STUDENTS][SUBJECTS+1];
   
    printf("Input students names & their marks in four subjects\n");
     get_list(name,marks,STUDENTS,SUBJECTS);
     get_sum(marks,STUDENTS,SUBJECTS+1);
     printf("\n");
     print_list(name,marks,STUDENTS,SUBJECTS+1);
     get_rank_list(name,marks,STUDENTS,SUBJECTS+1);
     printf("\nRanked List\n\n");
     print_list(name,marks,STUDENTS,SUBJECTS+1);
   
}

void get_list(char *string[],int array[][SUBJECTS+1],int m,int n)

 {
     int i,j,(*rowptr)[SUBJECTS+1]=array;
     for(i=0;i<m;i++)
     {
         scanf("%s",string[i]);
         for(j=0;j<SUBJECTS;j++)
             scanf("%d",&(*(rowptr+i))[j]);
     }
   
}

void get_sum(int array[][SUBJECTS+1],int m,int n)

 {
     int i,j,(*rowptr)[SUBJECTS+1]=array;
     for(i=0;i<m;i++)
     {
         (*(rowptr+1))[n-1]=0;
         for(j=0;j<SUBJECTS;j++)
             (*(rowptr+i))[n-1]+=(*(rowptr+1))[j];
     }

 }

void get_rank_list(char *string[],
                    int array[][SUBJECTS+1],
                    int m,
                    int n)

 {
     int i,j ,k,(*rowptr)[SUBJECTS+1]=array;
     char*temp;
   
    for(i=1;i<=m-1;i++)
         for(j=1;j<=m-i;j++)
             if((*(rowptr+j-1))[n-1]<(*(rowptr+j))[n-1])
             {
                 swap_string(string[j-1],string[j]);
               
                for(k=0;k<n;k++)
                     swap_int(&(*(rowptr+j-1))[k],&(*(rowptr+j))[k]);
             }

 }

void print_list(char *string[],
                 int array[][SUBJECTS+1],
                 int m,
                 int n)

 {
     int i,j,(*rowptr)[SUBJECTS+1]=array;
     for(i=0;i<m;i++)
     {
         printf("%-20s",string[i]);
         for(j=0;j<n;j++)
             printf("%5d",(*(rowptr+i))[j]);
         printf("\n");
     }

 }

void swap_int(int *p,int *q)

 {
     int temp;
     temp=*p;
     *p=*q;
     *q=temp;

 }

void swap_string(char s1[],char s2[])

 {
     char swaparea[256];
     int i;
     for(i=0;i<256;i++)
         swaparea[i]='\0';
     i=0;
     while(s1[i]!='\0'&&i<256)
     {
         swaparea[i]=s1[i];
         i++;
     }
     i=0;
     while(s2[i]!='\0'&&i<256)
     {
         s1[i]=s2[i];
         s1[++i]='\0';
     }
     i=0;
     while(swaparea[i]!='\0')
     {
         s2[i]=swaparea[i];
         s2[++i]='\0';
       
     }

 }

 

思考赐予新生,时间在于定义
2013-07-12 18:32
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:0 
忘了下面的函数定义也要修改过来,这样应该没问题了

思考赐予新生,时间在于定义
2013-07-12 18:43
快速回复:指针数组做函数参数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021167 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved