| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 570 人关注过本帖
标题:指针在结构体中引用出错 char *[],请各路大神指点!
只看楼主 加入收藏
lanskyxti
Rank: 1
等 级:新手上路
帖 子:14
专家分:4
注 册:2014-8-29
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:4 
指针在结构体中引用出错 char *[],请各路大神指点!
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>

bool str_in(char **);
void str_sort(const char *[],int);
void swap(void **p1,void **p2);
void str_out(char *[],int);
const size_t BUFFER_LEN=256;
const size_t NUM_P=50;
int main(void)
{
    char *pS[NUM_P];
    int count=0;
    printf("\nEnter successive lines,pressing Enter at the end of" "each line.\nJust press Enter to end.\n");
    for(count=0;count<NUM_P;count++)
      if(!str_in(&pS[count]))
      break;
    str_sort( pS,count);    请指正这句出错原因  invalid conversion from `char**' to `const char**'
    str_out( pS,count);
    system("pause");
    return 0;
}
bool str_in(char **pString)
{
     char buffer[BUFFER_LEN];
     if(gets(buffer)==NULL)
       {
         printf("\nError reading string.\n");
         exit(1);
       }
     if(buffer[0]='\0')
      return false;
     *pString=(char*)malloc(strlen(buffer)+1);
     if(*pString==NULL)
     {
       printf("\nOut of menory.");
       exit(1);
     }
     strcpy(*pString,buffer);
     return true;
}

void str_sort(const char *p[],int n)
{
     char *pTemp=NULL;
     bool sorted=false;
     while(!sorted)
     {
       sorted=true;
       int i=0;
       for(i=0;i<n-1;i++)
         if(strcmp(p[i],p[i+1])>0)
         {
           sorted=false;
           swap(&p[i],&p[i+1]);//指向常量的指针只可以交换地址改变变量值,这句正确但是DEVC报错  invalid conversion from `char**' to `void**' initializing argument 1 of `void swap(void**, void**)'
         }
     }
}
void swap(void **p1,void **p2)
{
     void *pt=*p1;
     *p1=*p2;
     *p2=pt;
}
void str_out(char *p[],int n)
{
     printf("\nYour input sorted in older is :\n\n");
     int i=0;
     for(i=0;i<n;i++)
     {
       printf("%s",p[i]);
       free(p[i]);
       p[i]=NULL;
     }
     return;
}
            
已附上代码及DEVC报错提示。
搜索更多相关主题的帖子: 结构体 
2014-10-10 23:37
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:7 
明显参数类型不一样啊  一个是void **  一个是char * []

DO IT YOURSELF !
2014-10-11 08:29
sidooh
Rank: 4
等 级:业余侠客
帖 子:121
专家分:265
注 册:2009-6-26
收藏
得分:7 
void str_sort(const char *p[],int n)
去掉const试试, 这里没必要用const吧
2014-10-11 12:47
lanskyxti
Rank: 1
等 级:新手上路
帖 子:14
专家分:4
注 册:2014-8-29
收藏
得分:0 
回复 楼主 lanskyxti
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>

bool str_in(char **);
void str_sort(char *[],int);
void swap(char **p1,char **p2);
void str_out(char *[],int);
const size_t BUFFER_LEN=256;
const size_t NUM_P=50;
int main(void)
{
    char *pS[NUM_P];
    int count=0;
    printf("\nEnter successive lines,pressing Enter at the end of" " each line.\nJust press Enter to end.\n");
    for(count=0;count<NUM_P;count++)
      if(!str_in(&pS[count]))//读入字符串
      break;
    str_sort( pS,count);    //按首字母排序str_in模块读入的字符串
    str_out( pS,count);     //输出排序后的字符串
    system("pause");
    return 0;
}
bool str_in(char **pString)  //用来读取字符串
{
     char buffer[BUFFER_LEN];
     if(gets(buffer)==NULL)
       {
         printf("\nError reading string.\n");
         return false;     //问题:读取字符串时,没有办法控制读取的个数,导致一直在读取字符串,程序一直在执行这个模块
       }
     if(buffer[0]='\0')
      return false;
     *pString=(char*)malloc(strlen(buffer)+1);
     if(*pString==NULL)
     {
       printf("\nOut of menory.");
       exit(1);
     }
     strcpy(*pString,buffer);
     return true;
}
void str_sort(char *p[],int n)  
{
     char *pTemp=NULL;
     bool sorted=false;
     while(!sorted)
     {
       sorted=true;
       int i=0;
       for(i=0;i<n-1;i++)
         if(strcmp(p[i],p[i+1])>0)
         {
           sorted=false;
           swap(&p[i],&p[i+1]);
         }
     }
}
void swap(char **p1,char **p2)  //用来排序字符串
{
     char *pt=*p1;
     *p1=*p2;
     *p2=pt;
}
void str_out(char *p[],int n)
{
     printf("\nYour input sorted in older is :\n\n"); 
     int i=0;
     for(i=0;i<n;i++)
     {
       printf("%s\n",p[i]);  //问题:改写后的程序无法输出字符串
       free(p[i]);
       p[i]=NULL;
     }
     return;
}
             
程序我改编了一下,现在不报错了,但是还有两个小小的问题,麻烦帮我看看这两个问题怎么解决?/* 程序几个模块的作用我也注释过了 */
2014-10-12 00:59
hackrol
Rank: 4
来 自:世界和平组织
等 级:业余侠客
帖 子:62
专家分:267
注 册:2014-9-6
收藏
得分:7 
主函数要加入判断输入个数修改成这样  ,,别的自己再检查下吧!

while(str_in(&pS[count++])&&count<5);//读入字符串


[ 本帖最后由 hackrol 于 2014-10-12 01:31 编辑 ]
2014-10-12 01:27
快速回复:指针在结构体中引用出错 char *[],请各路大神指点!
数据加载中...
 
   



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

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