C_and_CPP 板


LINE

不好意思,想请问板上先进一个问题。 该题CODE码是从课本复制而来 是一个使用指标函数的气泡排序法。 我的问题是其中swap函式的是void swap( int *element1Ptr, int *element2Ptr ) 当我将函式定义及呼叫swap时所需要的引数都改成(int element1ptr , int element2ptr) 时: 将 void swap( int *element1Ptr, int *element2Ptr ) { int hold; /* temporary holding variable */ hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; } /* end function swap */ 改成 void swap( int element1Ptr, int element2Ptr ) { int hold; /* temporary holding variable */ hold = element1Ptr; element1Ptr = element2Ptr; element2Ptr = hold; } /* end function swap */ 程式可以执行,但是却失去了排序的功能。 我不知道是自己哪里的观念出了问题...只能麻烦板上有空的板友请解答..谢谢。 下面是原本的CODE: #include <stdio.h> #define SIZE 10 /* prototypes */ void bubble( int work[], const int size, int (*compare)( int a, int b ) ); int ascending( int a, int b ); int descending( int a, int b ); int main() { int order; /* 1 for ascending order or 2 for descending order */ int counter; /* counter */ /* initialize array a */ int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; printf( "Enter 1 to sort in ascending order,\n" "Enter 2 to sort in descending order: " ); scanf( "%d", &order ); printf( "\nData items in original order\n" ); /* output original array */ for ( counter = 0; counter < SIZE; counter++ ) { printf( "%5d", a[ counter ] ); } /* end for */ /* sort array in ascending order; pass function ascending as an argument to specify ascending sorting order */ if ( order == 1 ) { bubble( a, SIZE, ascending ); printf( "\nData items in ascending order\n" ); } /* end if */ else { /* pass function descending */ bubble( a, SIZE, descending ); printf( "\nData items in descending order\n" ); } /* end else */ /* output sorted array */ for ( counter = 0; counter < SIZE; counter++ ) { printf( "%5d", a[ counter ] ); } /* end for */ printf( "\n" ); return 0; /* indicates successful termination */ } /* end main */ /* multipurpose bubble sort; parameter compare is a pointer to the comparison function that determines sorting order */ void bubble( int work[], const int size, int (*compare)( int a, int b ) ) { int pass; /* pass counter */ int count; /* comparison counter */ void swap( int *element1Ptr, int *element2ptr ); /* prototype */ /* loop to control passes */ for ( pass = 1; pass < size; pass++ ) { /* loop to control number of comparisons per pass */ for ( count = 0; count < size - 1; count++ ) { /* if adjacent elements are out of order, swap them */ if ( (*compare)( work[ count ], work[ count + 1 ] ) ) { swap( &work[ count ], &work[ count + 1 ] ); } /* end if */ } /* end for */ } /* end for */ } /* end function bubble */ /* swap values at memory locations to which element1Ptr and element2Ptr point */ void swap( int *element1Ptr, int *element2Ptr ) { int hold; /* temporary holding variable */ hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; } /* end function swap */ /* determine whether elements are out of order for an ascending order sort */ int ascending( int a, int b ) { return b < a; /* swap if b is less than a */ } /* end function ascending */ /* determine whether elements are out of order for a descending order sort */ int descending( int a, int b ) { return b > a; /* swap if b is greater than a */ } --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.39.173.77 ※ 编辑: nopicnic 来自: 114.39.173.77 (09/09 08:02)
1F:推 zinn:函式引数不用指标的话 他是复制一份local variable进去 09/09 08:18
2F:→ zinn:所以函式scope外面的值是不会变的 09/09 08:18
3F:→ nopicnic:感激!!突然了解~~因为之前都是自学没人点,真的很谢谢你 09/09 08:25
4F:推 VictorTom:卖一下瓜XD 本板文章编号 #1A5iL3A- 小弟写过解释:) 09/09 09:43
5F:→ nopicnic:我认真看完了,非常详细,谢谢你>"< 09/09 12:08
6F:推 shieldsky:我刚刚也看完,但有个疑问我po在V大的文章内,烦请解惑 09/09 20:32
7F:推 VictorTom:讨厌啦~~又暴露了人家英文不好这件事了啦....(羞) 09/09 20:59
8F:推 MaxHaru:call by value 跟 call by reference的不同.. 09/12 11:40







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP