[問題] 怎麼把主程式變數帶去副程式
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 )
( 未必需要依照此格式,文章條理清楚即可 )
遇到的問題:我想要把主程式的變數帶進去副程式
希望得到的正確結果:副程式的XY應該要=主程式的xy
程式跑出來的錯誤結果:印出卻是不一樣的數字
開發平台: Dev-C++*[m
有問題的code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define string_size 100
#define search_size 100
#define failure_size 100
int kmp_search(int *str, int *sea ,int X,int Y);
int main(void)
{
int x;
int y;
int a;
int i;
int string[string_size];
int search[search_size];
int failure[failure_size];
scanf("%d",&x);
for(y=0;y<x;y++)
{
int temp;
scanf("%d",&temp);
string[y]=temp;
}
scanf("%d",&a);
for(y=0;y<a;y++)
{
int temp;
scanf("%d",&temp);
search[y]=temp;
}
kmp_search(string,search,x,y);
printf("kmp_search=%d\n",&kmp_search);/不懂副程式回傳的涵義 所以印出/
system("pause");
return 0;
}
int kmp_search(int *str , int *sea, int X , int Y )
{ /KMP字串搜尋的演算法/
int i=0,j=0;
int len_string=X;
int len_search=Y;
printf("X:%d\nY:%d\n",&len_string,&len_search);
\*XY印出來應該是陣列的大小(主程式裡面的xy大小才對)
可是印出來卻不是我要的主程式xy數值*\
int failure[failure_size];
while( i < len_string && j < len_search )
{
if(str[i]==sea[j])
{
i++;
j++;
;
}
else if(j==0) i++;
else j = failure[j-1] +1;
}
return( ( j == len_search )?( i-len_search ) : - 1 );
}
因為副程式是課本的範例 我也有一個疑問就是副程式return回去的意義是什麼
我主程式裡面需要定義return的數值去分辨我副程式所得到的正確與否嗎?
謝謝大家
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.160.109.36
推
09/30 01:20, , 1F
09/30 01:20, 1F
→
09/30 01:21, , 2F
09/30 01:21, 2F
推
09/30 01:21, , 3F
09/30 01:21, 3F
→
09/30 01:21, , 4F
09/30 01:21, 4F
→
09/30 01:22, , 5F
09/30 01:22, 5F
→
09/30 01:22, , 6F
09/30 01:22, 6F
→
09/30 01:22, , 7F
09/30 01:22, 7F
→
09/30 01:22, , 8F
09/30 01:22, 8F
推
09/30 01:26, , 9F
09/30 01:26, 9F
→
09/30 01:29, , 10F
09/30 01:29, 10F
推
09/30 01:31, , 11F
09/30 01:31, 11F
→
09/30 01:32, , 12F
09/30 01:32, 12F
→
09/30 01:32, , 13F
09/30 01:32, 13F
→
09/30 01:44, , 14F
09/30 01:44, 14F
→
09/30 01:52, , 15F
09/30 01:52, 15F
→
10/03 00:08, , 16F
10/03 00:08, 16F