[問題] C語言FUNCTION的引數問題

看板C_and_CPP (C/C++)作者 (nopicnic)時間16年前 (2009/09/09 07:42), 編輯推噓5(503)
留言8則, 5人參與, 最新討論串1/1
不好意思,想請問板上先進一個問題。 該題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)

09/09 08:18, , 1F
函式引數不用指標的話 他是複製一份local variable進去
09/09 08:18, 1F

09/09 08:18, , 2F
所以函式scope外面的值是不會變的
09/09 08:18, 2F

09/09 08:25, , 3F
感激!!突然了解~~因為之前都是自學沒人點,真的很謝謝你
09/09 08:25, 3F

09/09 09:43, , 4F
賣一下瓜XD 本板文章編號 #1A5iL3A- 小弟寫過解釋:)
09/09 09:43, 4F

09/09 12:08, , 5F
我認真看完了,非常詳細,謝謝你>"<
09/09 12:08, 5F

09/09 20:32, , 6F
我剛剛也看完,但有個疑問我po在V大的文章內,煩請解惑
09/09 20:32, 6F

09/09 20:59, , 7F
討厭啦~~又暴露了人家英文不好這件事了啦....(羞)
09/09 20:59, 7F

09/12 11:40, , 8F
call by value 跟 call by reference的不同..
09/12 11:40, 8F
文章代碼(AID): #1AfklkrQ (C_and_CPP)
文章代碼(AID): #1AfklkrQ (C_and_CPP)