[問題] 結構指標是Call by value ?\
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
dev c++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
指標不是應該為call by refence
餵入的資料(Input):
兩個指標,一個矩陣 求最大值與最小值
預期的正確結果(Expected Output):
最大值的指標與最小值的指標能正確地指到 正確的位置
錯誤結果(Wrong Output):
指的位置為初始化的值
程式碼(Code):(請善用置底文網頁, 記得排版)
#include<iostream>
#include <iomanip>
#define SIZE 10
using namespace std;
typedef struct{
int key;
}element;
typedef struct stack *stackPointer;
typedef struct stack{
element data;
stackPointer link;
}stack;
void maxAndmin(stack list[],stackPointer max,stackPointer min);
int main(){
stack list[SIZE]; /*一個stack矩陣*/
stackPointer max,min;
srand(time(NULL));
max=&list[0];min=&list[0];
for(int i=0;i<SIZE;i++){ /*亂數初始化矩陣內容*/
list[i].link=&list[i+1];
list[SIZE-1].link=NULL;
list[i].data.key=rand()%49+1;
}
maxAndmin(list,max,min); /*函式呼叫*/
for(stackPointer i=&list[0];i!=NULL;i=i->link)
cout<<setw(3)<< i->data.key;
cout<<"\n最大值為:"<<setw(3)<<max->data.key;
cout<<"\n最小值為:"<<setw(3)<<min->data.key;
getchar();
}
void maxAndmin(stack list[],stackPointer max,stackPointer min){
for(stack *i=&list[0];i!=NULL;i=i->link){
if((i->data.key) > (max->data.key)){
max=i;
}
if(i->data.key <min->data.key)
min=i;
}
}
補充說明(Supplement):
不知道錯在哪,自己debug的結果感覺是call by value
但指標不是call by refence嗎?
請高手們指點迷津!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.248.222.223
→
02/28 17:53, , 1F
02/28 17:53, 1F
推
02/28 17:54, , 2F
02/28 17:54, 2F
→
02/28 17:59, , 3F
02/28 17:59, 3F
→
02/28 17:59, , 4F
02/28 17:59, 4F
→
02/28 17:59, , 5F
02/28 17:59, 5F
→
02/28 18:00, , 6F
02/28 18:00, 6F
→
02/28 18:09, , 7F
02/28 18:09, 7F
→
02/28 18:10, , 8F
02/28 18:10, 8F
→
02/28 18:23, , 9F
02/28 18:23, 9F
→
02/28 18:24, , 10F
02/28 18:24, 10F
推
02/28 18:25, , 11F
02/28 18:25, 11F
推
02/28 18:42, , 12F
02/28 18:42, 12F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章