指针数组的一个例题问题

libcheng 2009-11-25 11:21:09
刚学c,做到下面一个例题,是单词排序的一个程序;
但如果不按注释修改就运行出错,(也就是删除k=j和if(k!=i)两行,然后把上一个if语句中的k改成i;)
谁能帮帮忙,解释一下,谢谢。


#include "stdio.h"
#include"string.h"


void main()
{
void sort(char *p[],int n);
void print(char *p[],int n);
char *p[6]={"Microsoft","help","Visual","studio","micro","Help"};
int n=6;
sort(p,n);
print(p,n);
}

void sort(char *p[],int n)
{
char *temp;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(p[k],p[j])>0)/*把k改成i,和删除下两行后正确运行*/
k=j; /*删除这行和下一行后可以正确运行*/
if(k!=i) /*删除这行和执行以上两行的操作后就可以运行,否则运行出错(应用程序出错)*/

{temp=p[i];p[i]=p[j];p[j]=temp;}
}
}

void print(char *p[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%s\n",p[i]);
}
...全文
140 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacksonfjf 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 libcheng 的回复:]
*******************************************************************************

        CODESEG

        public  strcmp
strcmp  proc \
        str1:ptr byte, \
        str2:ptr byte

        OPTION PROLOGUE:NONE, EPILOGUE:NONE

        .FPO    ( 0, 2, 0, 0, 0, 0 )

        mov    edx,[esp + 4]  ; edx = src(我逐语句调试,执行到k=j这句时出错,这行是提示出错的结果,1楼的能帮我看下是什么意思吗,我看不懂)
        mov    ecx,[esp + 8]  ; ecx = dst

        test    edx,3
        jnz    short dopartial

        align  4
dodwords:
        mov    eax,[edx]

        cmp    al,[ecx]
        jne    short donene
        or      al,al
        jz      short doneeq
        cmp    ah,[ecx + 1]
        jne    short donene
        or      ah,ah
        jz      short doneeq

        shr    eax,16

        cmp    al,[ecx + 2]
        jne    short donene
        or      al,al
        jz      short doneeq
        cmp    ah,[ecx + 3]
        jne    short donene
        add    ecx,4
        add    edx,4
        or      ah,ah
        jnz    short dodwords

        align  4
doneeq:
        xor    eax,eax
        ret

        align  4
donene:
        ; The instructions below should place -1 in eax if src < dst,
        ; and 1 in eax if src > dst.

        sbb    eax,eax
        sal    eax,1
        add    eax,1
        ret

        align  4
dopartial:
        test    edx,1
        jz      short doword

        mov    al,[edx]
        add    edx,1
        cmp    al,[ecx]
        jne    short donene
        add    ecx,1
        or      al,al
        jz      short doneeq

        test    edx,2
        jz      short dodwords


        align  4
doword:
        mov    ax,[edx]
        add    edx,2
        cmp    al,[ecx]
        jne    short donene
        or      al,al
        jz      short doneeq
        cmp    ah,[ecx + 1]
        jne    short donene
        or      ah,ah
        jz      short doneeq
        add    ecx,2
        jmp    short dodwords

strcmp  endp

        end

[/Quote]
跳到标准库函数里去了如果是vc 点窗口上方的函数选择
选择main就可以了
jacksonfjf 2009-11-26
  • 打赏
  • 举报
回复
未控制sort函数中第二个for的范围
这样写for就只控制了
if(strcmp(p[k],p[j])>0)
k=j;
这两行语句
当最后j=6时
跳出for循环,但仍然执行下一句
if(k!=i)
{temp=p[i];p[i]=p[j];p[j]=temp;}
}
这样仍然把字符串与一个随机地址的内容交换,造成错误
正确的的代码为:
#include "stdio.h"
#include"string.h"


void main()
{
void sort(char *p[],int n);
void print(char *p[],int n);
char *p[6]={"Microsoft","help","Visual","studio","micro","Help"};
int n=6;
sort(p,n);
print(p,n);
}

void sort(char *p[],int n)
{
char *temp;
int i,j,k;
for(i=0;i <n-1;i++)
{
k=i;
for(j=i+1;j <n;j++)
{if(strcmp(p[k],p[j])>0)
k=j;
if(k!=i)
{temp=p[i];p[i]=p[j];p[j]=temp;}
}
}
}
补上两个大括号即可
void print(char *p[],int n)
{
int i;
for(i=0;i <n;i++)
printf("%s\n",p[i]);
}
liyudefly 2009-11-26
  • 打赏
  • 举报
回复
还有,很少很少有问题必须要看汇编才能解决,不要动不动就拿出一堆汇编去找问题,那样只会平添烦恼。
liyudefly 2009-11-26
  • 打赏
  • 举报
回复
LZ不要拿这种笔误的问题来忽悠人啊,你看看你 sort()函数的最后
if(k!=i) /*删除这行和执行以上两行的操作后就可以运行,否则运行出错(应用程序出错)*/
{temp=p[i];p[i]=p[j];p[j]=temp;}
i和j换?你抄错了吧?你把j换成k,问题就可解决。
libcheng 2009-11-26
  • 打赏
  • 举报
回复
不好意思,给大家添麻烦了。
libcheng 2009-11-25
  • 打赏
  • 举报
回复
*******************************************************************************

CODESEG

public strcmp
strcmp proc \
str1:ptr byte, \
str2:ptr byte

OPTION PROLOGUE:NONE, EPILOGUE:NONE

.FPO ( 0, 2, 0, 0, 0, 0 )

mov edx,[esp + 4] ; edx = src(我逐语句调试,执行到k=j这句时出错,这行是提示出错的结果,1楼的能帮我看下是什么意思吗,我看不懂)
mov ecx,[esp + 8] ; ecx = dst

test edx,3
jnz short dopartial

align 4
dodwords:
mov eax,[edx]

cmp al,[ecx]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 1]
jne short donene
or ah,ah
jz short doneeq

shr eax,16

cmp al,[ecx + 2]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 3]
jne short donene
add ecx,4
add edx,4
or ah,ah
jnz short dodwords

align 4
doneeq:
xor eax,eax
ret

align 4
donene:
; The instructions below should place -1 in eax if src < dst,
; and 1 in eax if src > dst.

sbb eax,eax
sal eax,1
add eax,1
ret

align 4
dopartial:
test edx,1
jz short doword

mov al,[edx]
add edx,1
cmp al,[ecx]
jne short donene
add ecx,1
or al,al
jz short doneeq

test edx,2
jz short dodwords


align 4
doword:
mov ax,[edx]
add edx,2
cmp al,[ecx]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 1]
jne short donene
or ah,ah
jz short doneeq
add ecx,2
jmp short dodwords

strcmp endp

end
jixingzhong 2009-11-25
  • 打赏
  • 举报
回复
学习一下调试技巧,可以断点后单步执行程序,并跟踪查看各局部变量的值,然后分析

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧