为什么在以下代码中发生分段错误

时间:2015-07-31 21:06:38

标签: c

此程序尝试为每行输入创建单独的链接列表。第一行包含测试用例的数量,以下行包含空格分隔的整数作为链接列表元素。

示例输入:

2
1 2 4 6
3 9 11 12

预期产出:

LinkList 1 : 1 2 4 6
LinkList 2 : 3 9 11 12
        

此函数将每个以空格分隔的整数行放入一个字符串中,并将其传递给makelist函数

int main()
{
    int t;
    scanf("%d\n",&t);   //number of testcases
    int x;

    while(t--)
    {
        struct node *head1=NULL,*head2=NULL;

        int i=0,j,k;
        int len1=0,len2=0;
        char str1[200],str2[200];

        fgets(str1,200,stdin);   //input first line
        fgets(str2,200,stdin);   // input second line

        len1=strlen(str1);
        len2=strlen(str2);

        printf("length,of strings %d %d",len1,len2);
        makeList(str1,&head1,len1);    //fault occurs here
        makeList(str2,&head2,len2);
    }
}

//program to make linklist from input string
void makeList(char *str,struct node ** head,int len)
{
    printf("Inside makelist");
    int i ;
    struct node *temp, *temp2;
    for(i=0;i<len;i++)
    {
        if((str[i]>=48 )&& (str[i]<=57))   // converting string to integer
        {
            printf("NUmber found");
            temp=makeNode();
            temp->val=str[i];
            temp->next=NULL;

            if(*head==NULL)
                *head=temp;
            else
            {  
                temp2=*head;
                while(temp2->next!=NULL)
                    temp2=temp2->next;
                temp2->next=temp;
            }
        }
    }
    printList(*head);
}

//program for allocating memory for new node 
struct node *makeNode()

{
    struct node *newNode=(struct node *)malloc(sizeof(struct node));
    return newNode;
}

2 个答案:

答案 0 :(得分:0)

如果对两行输入使用2,然后每个循环执行fgets()两次,则需要递减两次。这个t--是错误的。

答案 1 :(得分:0)

在以下(未经测试的)代码中,

我不得不猜测你真正希望程序执行什么。

以下代码:

compiles cleanly, 
performs appropriate error checking,
cleans up after itself,
etc.

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

struct node
{
    int val;
    struct node *next;
};

// prototypes
void makeList(char *str, int whichList);
struct node *makeNode( void );
void printList( struct node *head);

static struct node **listOfNodes = NULL;

int main( void )
{
    int t;
    if( 1 != scanf("%d\n",&t) )   //number of testcases
    {// then scanf failed
        perror( "scanf for number of test cases failed");
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    if( NULL == (listOfNodes = malloc( t* sizeof(struct node) ) ) )
    { // then malloc failed
        perror( "malloc failed for list of nodes");
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // clear listOfNodes to make cleanup easier
    memset( listOfNodes, 0x00, t*sizeof(struct node) );

    for( int i = 0; i<t; i++ )
    {
        char str1[200];

        fgets(str1,200,stdin);   //input line
        makeList(str1, i);
    } // end for

    // cleanup
    for(int i=0; i<t;i++)
    {
        struct node *myNode = listOfNodes[i]; // get linked list ptr
        while( myNode )
        {
            free(myNode);
            myNode = myNode->next;
        }
    } // end for

    free( listOfNodes );
    return 0;
} // end function: main


//program to make linklist from input string
void makeList(char *str, int whichList)
{
    printf("Inside makelist");
    int i =0;
    struct node *temp, *temp2;

    char *token = strtok( str, " "); // setup for first number

    while( token )
    { // then number found
        printf("NUmber found\n");
        if( NULL == (temp=makeNode() ) )
        { // then makeNode failed
            return;
        }

        // implied else, makeNode successful

        temp->val=atoi(&str[i]);
        temp->next=NULL;

        if(NULL == listOfNodes[whichList])
            listOfNodes[whichList]=temp;
        else
        {
            temp2=listOfNodes[whichList];
            while(temp2->next!=NULL)
                temp2=temp2->next;
            temp2->next=temp;
        }

        token = strtok( NULL, " "); // setup for next number
    }
    printList( listOfNodes[whichList]);
} // end function: makeList


//program for allocating memory for new node
struct node *makeNode()
{
    struct node *newNode=malloc(sizeof(struct node));
    return newNode;
} // end function: makeNode