首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

从PHP转学C请问各路神人一个qsort里面的有关问题

2013-02-19 
从PHP转学C请教各路神人一个qsort里面的问题VC++6环境下报错说undeclared identifier难道不能(*(fun *)ae)

从PHP转学C请教各路神人一个qsort里面的问题
VC++6环境下报错说undeclared identifier
难道不能(*(fun *)ae).lis这样写,还是我Struct 定义出现问题,
struct 应该怎样写呢?


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct fun
{
int lis;
char strs[50];
}a[5];


int cmp ( const void *ae , const void *be )
{

int i;
long k1=(long)ae;
long k2=(long)be; 
printf("checking [%d:%d]==>[%6d|%6d]\n",k1,k2,(*(fun *)ae).lis,(*(fun *)be).lis);//这里出现问题!!!!!!!!!!!!!!
printf("[1]%s\n",(*(fun *)ae).strs);
printf("[2]%s\n",(*(fun *)be).strs);
printf("-------------------\n");
return (*(fun *)ae).lis>(*(fun *)be).lis?1 : -1; //强制转换类型
};

void main()
{
//读入
int i;
for (i=0;i<=5;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i].lis);
printf("[]str=");
scanf("%s",&a[i].strs);
}
printf("=====================\n");
//指针块排
qsort(a,6,sizeof(a[0]),cmp);
//输出
for (i=0;i<=5;i++)
{
printf("[%3i][%d]-->%5d|&s\n",i,&a[i],a[i].lis,a[i].strs);
}
}
c
[解决办法]
(*(struct fun *)ae).lis 试试
[解决办法]
你这PHP都没毕业啊, 循环越界了不知道吗, 优先级也不对啊.

#include <stdio.h>
#include <stdlib.h>

struct fun {
        int lis;
        char strs[50];
} a[5];

int cmp(const void *ae, const void *be)
{

        int i;
        long k1 = (long)ae;
        long k2 = (long)be;
    /* C不能省略struct, 除非typedef */
        printf("checking [%d:%d]==>[%6d
[解决办法]
%6d]\n", k1, k2, (*(struct fun *) ae).lis, (*(struct fun *) be).lis);   //这里出现问题!!!!!!!!!!!!!!
        printf("[1]%s\n", (*(struct fun *) ae).strs);
        printf("[2]%s\n", (*(struct fun *) be).strs);
        printf("-------------------\n");

    /* 相等时候返回0呢? */
        return (*(struct fun *) ae).lis > (*(struct fun *) be).lis ? 1 : -1;    //强制转换类型 
};

void main()
{
//读入
        int i;
        for (i = 0; i <= 5; i++) { /* 越界 */
                printf("a[%d]=", i);
        fflush(stdout); /* 刷新缓冲 */
                scanf("%d", &a[i].lis);


                printf("[]str=");
        fflush(stdout); /* 刷新缓冲 */
                scanf("%s", &a[i].strs); /*内存溢出漏洞, 替换为fgets*/
        }
        printf("=====================\n");
//指针块排
        qsort(a, 6, sizeof(a[0]), cmp); /* 就5个元素, 你排序6个? */
//输出
        for (i = 0; i <= 5; i++) { /* 越界 */
                printf("[%3i][%d]-->%5d
[解决办法]
&s\n", i, &a[i], a[i].lis, a[i].strs); /* &s还是%s ?*/
        }
}

热点排行