【问题标题】:Linked list with structures using command line arguments使用命令行参数的结构链表
【发布时间】:2019-11-30 16:20:56
【问题描述】:

我已经编写了 C 代码来使用链表存储和使用员工数据。

逻辑完美。当我使用函数调用(注释部分)传递输入时,我得到了正确的输出。但我想尝试使用命令行参数。我不知道该怎么做,我得到了分段错误。有人可以帮我学习如何做或指出'main()'函数是否有任何错误。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct employee
{
    char name[20];
    int age;
}info;

struct node
{
    info data;
    struct node* next;
};

struct node* head;

void insert(char name[20], int age)
{
    struct node* temp= NULL;
    temp=(struct node*)malloc(sizeof(struct node));
    strcpy(temp->data.name,name);
    temp->data.age=age;

    if(head==NULL)
    {
        head=temp;
        head->next=NULL;
        return;
    }

    struct node* temp1 = head;

    if((temp->data.age) <= (temp1->data.age))
    {
        temp->next=temp1;
        head=temp;
        return;
    }
    else
    {
        while( (temp->data.age)>(temp1->data.age) && (temp1->next != NULL) )
        {
            if((temp->data.age) < (temp1->next->data.age))
            {
                break;
            }
            temp1 = temp1->next;
        }
        temp->next = temp1->next;
        temp1->next = temp;
    }
}

void print(int n)
{
    if((head==NULL)||(n<1))
        return;

    struct node* temp = head;

    if(n==1)
    {
        printf("%s %d\n", temp->data.name, temp->data.age);
        return;
    }

    for(int i=1; i<=n-1; i++)
    {
        temp = temp->next;
        if(temp==NULL)
        {
            printf("Specified number exceeds linked list limit\n");
            return;
        }
    }
    printf("%s %d\n", temp->data.name, temp->data.age);
}

void remove_(int n)
{
    if((head==NULL)||(n<1))
        return;

    struct node* temp = head;

    if(n==1)
    {
        head = temp->next;
        free(temp);
        return;
    }

    for(int i=1; i<=n-2; i++)
    {
        temp = temp->next;
        if((temp->next)==NULL)
            return;
    }

    struct node* temp1=NULL;
    temp1 = temp->next;
    temp->next = temp1->next;
    free(temp1);
}


int main(int argc, char* argv[])
{

    head= NULL;

    if(!(strcmp(argv[1],"insert")))
    {
        int age=atoi(argv[3]);                  // ./main insert sunil 40
        insert(argv[2], age);  
    }


    if(!(strcmp(argv[1],"print")))              // ,.main print 2
    {
        int n=atoi(argv[2]);
        print(n);
    }


//  insert("williamson",40);
//  insert("micheal",30);
//  print(5);
//  print(2);
//  print(1);
//  insert("John",24);
//  remove_(3);
//  print(3);
//  print(1);
//  print(2);

 return 0;
}

我得到分段错误或有时没有结果

【问题讨论】:

  • 代码似乎运行良好,没有段错误?你能用 GDB 之类的东西包含一个堆栈跟踪吗?
  • @gunsofboom 注释行未注释时是否会出现分段错误?
  • @VladfromMoscow 当注释行未注释时,该程序运行良好,没有任何错误。只是不知道如何使用命令行参数方法传递输入
  • @gunsofboom 正如我在回答中指出的那样,函数插入无效并且具有未定义的行为。

标签: c linked-list segmentation-fault command-line-arguments singly-linked-list


【解决方案1】:

在访问它们之前,您必须检查是否有足够的参数。例如,如果你想“插入”

int main(int argc, char* argv[])
{
    head= NULL;
    if(argc > 3)
        if(!(strcmp(argv[1],"insert")))
        {
            int age=atoi(argv[3]);                  // ./main insert sunil 40
            insert(argv[2], age);  
        }
}

【讨论】:

  • 感谢您添加此内容 :) 但我可以知道如何为该程序传递参数
  • 如果您将可执行文件命名为“my_code”,那么您必须将其执行为:./my_code insert williamson 40
【解决方案2】:

我认为当注释行未注释时会发生分段错误。

问题是函数insert 定义不正确并且具有未定义的行为。

在这个循环中

    while((temp->data.age) > (temp1->data.age))
    {
        if((temp->data.age) < (temp1->next->data.age))
        {
            break;
        }
        temp1 = temp1->next;
    }

你不检查temp1是否等于NULL

您还应该检查为函数 main 提供的参数数量是否有效。

【讨论】:

  • 嗨,我检查一下'head'是否为空。如果 head 不为 null,则 head 分配给 temp1,否则返回
  • @gunsofboom 在循环中执行 temp1 = temp1->next; temp1->next 可以等于 NULL,所以和 temp1 将等于 NULL。
  • 哦,是的,感谢您的指认:)我会纠正它
  • 将之前的 while 编辑为 "while( (temp->data.age)>(temp1->data.age) && (temp1->next != NULL) )"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 2014-04-03
  • 1970-01-01
  • 2014-01-01
相关资源
最近更新 更多