編輯:解決方案也許是在頁面的底部。我用解決方案回答了我的問題。我希望這可以幫助其他人。返回指針數組的指針
我在這裏有一個小問題。我正在編程一個簡單的端口掃描,但我遇到了帶參數的函數的問題。
我將在代碼解釋:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
//this function handle the arguments.
char* ret[2]= {"NULL","NULL"}; //declaring this in global because of segmention fault?
char** arguments_handle(int argc,char **arg)
{
if(argc!=5)
{
printf("Usage:./file -p PORT-RAGE -h HOST.IP\n");
exit(1);
}
//make sure the user type the correct arguments. in this case just -h and -p
if(strcmp(arg[1],"-p")==0 || strcmp(arg[1],"-h")==0 && strcmp(arg[3],"-p")==0 || strcmp(arg[3],"-h")==0)
{
//if in the arguments we got -h or -p run this
//if is -p
if(strcmp(arg[1],"-p")==0)
{
//take the next argument in this case is the port range and put in our array
strcpy(ret[0],arg[2]);
}
else
{
strcpy(ret[1],arg[2]);
}
if(strcmp(arg[3],"-h")==0)
{
//now for the -h
strcpy(ret[1],arg[4]);
}
else
{
strcpy(ret[0],arg[4]);
}
}
return ret;
}
int main(int argc, char **argv)
{
char** ipnport;
ipnport = arguments_handle(argc,argv);
printf("IP is :%s port range is %s\n",ipnport[0],ipnport[1]);
//the rest of the code about port scan goes here. I'm just cutting
return 0x0;
}
這裏的問題是,我可以編譯正確的,但我得到了分段故障。我看不到我錯在哪裏。我想這是關於緩衝區或堆棧溢出的處理。
所以我在這裏做的這個函數是採取argv並將其發送到arguments_handle函數。 這樣做是看到參數「-p」和「-h」以及「存儲」在char數組中的正確順序。 喜歡此CHAR: 「字符指針到該數組包含字符數組」
pointer pointer pointer
pointer to this-> ["firstarg","secondarg","etc"]
在這種情況下,「指針的指針的指針」將字符串的第一個字符。
總結:我想創建一個字符串數組,並將其從arguments_handle返回給main函數。
任何想法? :)
真誠,
INT3
你訪問'ret'超出導致不確定的行爲數組的邊界。 –
我認爲你的strcpy是問題所在。看看[Mysticial]發佈的答案(http://stackoverflow.com/questions/8716714/bus-error-10-error) –