c - 返回指针数组的指针

标签 c arrays pointers segmentation-fault malloc

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

我在 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.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”以及“store”以正确的顺序在 char 数组中的位置。 像这个字符: “指向包含字符数组的数组的字符指针”

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

在那种情况下,“指针指针指针”将是字符串的第一个字符。

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

有什么想法吗? :)

此致

整数3

最佳答案

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

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 HOST\n",
                           argv[0]);
                   exit(EXIT_FAILURE);
               }
    } 
    printf("IP is :%s port range is %s\n",ret[IP],ret[PORT]);
    //the rest of the code about port scan goes here. I'm just cutting
    return 0x0;
}

关于c - 返回指针数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19778741/

相关文章:

更改指针的值而不在函数中调用它?

c - 在C中使用数组进行BCD转换

php - 解析错误: unexpected T_DOUBLE_ARROW => foreach array assignment

c++ - 分配给指针的值的存储持续时间

iphone - 将 NSMutableArray 转换为 C 结构体数组 - iPhone - OpenGL ES

c - 求 x 的 n 次方

c++ - 在 C++ 中返回局部变量的引用和标准指针的替代方法是什么?

c - 关于C函数指针的一个问题

c - memcpy 函数定义中缓冲区重叠意味着什么?

arrays - 具有非不同数字的数组中的魔术索引