C语言 返回指针数组的指针



编辑:解决方案可能在页面底部。我用解决方案回答了我的问题。我希望这对其他人有所帮助。

我在 Linux 中遇到了一个小问题。我正在编写一个简单的端口扫描,但我对接受参数的函数有问题。

我将在代码上解释:

#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.IPn");
        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 %sn",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 指针"

                    pointer     pointer    pointer
pointer to this-> ["firstarg","secondarg","etc"]

在这种情况下,"指针指针"将是字符串的第一个字符。

总结:我想创建一个字符串数组并将其从arguments_handle返回到main的函数。

有什么想法吗?:)

真诚地

int3

问题是您没有为从命令行获取的字符串分配正确的内存空间。

char* ret[2]= {"NULL","NULL"};

这将创建一个数组,其中包含两个大小为 4 + 结束字符 ('\0') 的字符串。这是你想要的吗?或者,您要创建两个NULL指针。如果输入字符串的大小大于 4 会发生什么情况?您可能会访问错误的内存,从而导致分段错误。此外,您不应该使用strcpystrcmp,而应该使用strncpystrncmp

代码应更改如下:

    char * ret[2];
    if(strncmp(arg[3],"-h", 3)==0)
    {   
        string_size = strlen(arg[4]) + 1; 
        ret[1]= malloc(sting_size); 
        memset(ret[1], 0, string_size); 
        strncpy(ret[1],arg[4], string_size);
        // or ret[1]=arg[4] as suggested by Roland
    }

但是,不需要为输入参数编写解析器,因为函数getopt为您执行此操作。这是手册,最后有一个很好的例子:http://man7.org/linux/man-pages/man3/getopt.3.html

代码的快速示例:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#define NUMBER_ARGUMENTS 2 
#define IP 1
#define PORT 0
char* ret[NUMBER_ARGUMENTS];  
int main(int argc, char **argv)
{
    int opt; 
    while ((opt = getopt(argc, argv, "p:h:")) != -1) {
               switch (opt) {
               case 'p':
                    ret[PORT]=optarg; 
                   break;
               case 'h':
                    ret[IP]=optarg; 
                   break;
               default: /* '?' */
                   fprintf(stderr, "Usage: %s -p PORT -h HOSTn",
                           argv[0]);
                   exit(EXIT_FAILURE);
               }
    } 
    printf("IP is :%s port range is %sn",ret[IP],ret[PORT]);
    //the rest of the code about port scan goes here. I'm just cutting
    return 0x0;
}

我解决了问题!

非常感谢朱塞佩·佩斯!我不习惯malloc和memset之类的东西。但这解决了问题。我知道它试图将数据放在不应该:D的地方

这是我的最终代码:

#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.
//well i didnt edit strcpy and strcmp to strncpy yet but I will :D
char* ret[2]; //GLOBAL ofc.
char** arguments_handle(int argc,char **arg)
{
    if(argc!=5)
    {
        printf("Usage:./file -p PORT-RAGE -h HOST.IPn");
        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)
{
    //tested on windows we need to put a cast before malloc
    //in linux it works fine on my raspberrypi !! :D
    ret[0] = (char*)malloc(20); //some compilers maybe will throw here an error
    ret[1] = (char*)malloc(20); //because malloc returns a void pointer and ret is a char*
    memset(ret[0],0,20);
    memset(ret[1],0,20);
    char** ipnport;
    ipnport = arguments_handle(argc,argv);
    printf("IP is :%s port range is %sn",ipnport[1],ipnport[0]);
    //the rest of the code about port scan goes here. I'm just cutting
    return 0x0;
}

再次感谢,我希望这段代码能帮助其他人看到这篇文章:)

真诚地

int3

相关内容

  • 没有找到相关文章

最新更新