1

I have two lists with 14 elements each, I created using the following code:

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h"
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>


// Node Structure of Linked-List
typedef struct nlist{
    char *data;
    struct nlist *next;
}Node;

//Declaring global variables
Node *B02_list;
Node *SCL_list;


// Function to replace a string with another 
// string 
char* str_replace(char* string, const char* substr, const char* replacement) {
    char* tok = NULL;
    char* newstr = NULL;
    char* oldstr = NULL;
    int   oldstr_len = 0;
    int   substr_len = 0;
    int   replacement_len = 0;

    newstr = strdup(string);
    substr_len = strlen(substr);
    replacement_len = strlen(replacement);

    if (substr == NULL || replacement == NULL) {
        return newstr;
    }

    while ((tok = strstr(newstr, substr))) {
        oldstr = newstr;
        oldstr_len = strlen(oldstr);
        newstr = (char*)malloc(sizeof(char) * (oldstr_len - substr_len + replacement_len + 1));

        if (newstr == NULL) {
            free(oldstr);
            return NULL;
        }

        memcpy(newstr, oldstr, tok - oldstr);
        memcpy(newstr + (tok - oldstr), replacement, replacement_len);
        memcpy(newstr + (tok - oldstr) + replacement_len, tok + substr_len, oldstr_len - substr_len - (tok - oldstr));
        memset(newstr + oldstr_len - substr_len + replacement_len, 0, 1);

        free(oldstr);
    }

    free(string);

    return newstr;
}


// Function to insert an element to the Linked-List
Node* insert(Node *Head, char *value)
{
    Node *new_string;
    new_string = (Node *)malloc(sizeof(Node));
    new_string->data = malloc(strlen(value)+1);
    strcpy(new_string->data,value);
    Node *check;
    check = (Node *)malloc(sizeof(Node));

    if(Head == NULL){
        Head = new_string;
        Head->next = NULL;
    }
    else{
        check = Head;
        while(check->next != NULL)
            check = check->next;

        check->next = new_string;
        new_string->next = NULL;
    }
    return Head;
}


//Function to check if a string finishes with a suffix
int string_ends_with(const char * str, const char * suffix)
{
    int str_len = strlen(str);
    int suffix_len = strlen(suffix);

    return 
        (str_len >= suffix_len) &&
        (0 == strcmp(str + (str_len-suffix_len), suffix));
}


//Function to show the elements of the Linked-List
void show(Node *Head)
{
    if (Head == NULL){
        return;
    }

    while(Head != NULL) {
        printf("%s\n", Head->data);
        Head=Head->next;
    }
    printf("\n");
}

//Function to find the files in a directory based on a wildcard
Node * recurList(char *basePath, char *suffix)
{
    char path[1000];
    struct dirent *dp;

    DIR *dir = opendir(basePath);
    while ((dp = readdir(dir)) != NULL)
    {
        if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
        {
            strcpy(path, basePath);
            strcat(path, "/");
           strcat(path, dp->d_name);

        struct stat s;
            if (stat(path, &s) == 0)
            {
                if( s.st_mode & S_IFDIR )
                {
                recurList(path, suffix);
                }
                else if(s.st_mode &S_IFREG)
                {
                if (string_ends_with(path, suffix))
                    B02_list = insert(B02_list, path);
                }
            } 
        }
    }
    return B02_list;
    closedir(dir);
}

//Function to find the SCL files in a directory 
Node * recurListSCL(char *basePath, char *suffix)
{
    char path[1000];
    struct dirent *dp;

    DIR *dir = opendir(basePath);
    while ((dp = readdir(dir)) != NULL)
    {
        if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
        {
            strcpy(path, basePath);
            strcat(path, "/");
           strcat(path, dp->d_name);

        struct stat s;
            if (stat(path, &s) == 0)
            {
                if( s.st_mode & S_IFDIR )
                {
                recurListSCL(path, suffix);
                }
                else if(s.st_mode &S_IFREG)
                {
                if (string_ends_with(path, suffix))
                    SCL_list = insert(SCL_list, path);
                }
            } 
        }
    }
    return SCL_list;
    closedir(dir);
}

int getCount(Node* head)
{
    int count = 0;
    Node * current = head;
    while (current != NULL)
    {
        count ++;
        current = current ->next;
    }
    return count;
}

int main()
{
    char path[100];
    char suffix[100];

    printf("Enter path to list files: ");
    scanf("%s", path);
    printf("Enter the wildcard: ");
    scanf("%s", suffix);

    Node *B02;
    Node *SCL;
    B02 = recurList(path, suffix);
    printf("Printing B02_head!\n");
    show(B02);
    char *suffix_scl = "SCL_10m.tif";
    SCL = recurListSCL(path, suffix_scl);
    printf("Printing files in SCL head:\n");
    show(SCL);
    int B02_length = 0;
    int SCL_length = 0;
    B02_length = getCount(B02);
    SCL_length = getCount(SCL);
    printf("BO2 of length: %d\n", B02_length);
    printf("SCL of length: %d\n", SCL_length);

    printf("TESTING FOR LOOP\n");
    Node *index;
    Node *index_scl;
    int ctr = 0;
    for ((index = B02) && (index_scl = SCL);
    (index !=NULL) & (index_scl !=NULL); 
    index=index->next, index_scl->next)
    {
        printf("%d\n", ctr);
        ctr++;
        printf("B02 element is:\n%s\n", index->data);
        printf("SCL element is:\n%s\n", index_scl->data);
        printf("-------------------------------\n");
        printf("\n");
    }
    return 0;
}

I want to iterate in the main() function over the two lists I created. However the code gives me the following output, after compilation.

Enter path to list files: data
Enter the wildcard: B02_10m.tif
Printing B02_head!
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180816T155521_N0206_R011_T17PLK_20180816T213001.SAFE/GRANULE/L2A_T17PLK_A016453_20180816T155524/IMG_DATA/R10m/T17PLK_20180816T155521_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180925T155521_N0206_R011_T17PLK_20180925T204924.SAFE/GRANULE/L2A_T17PLK_A017025_20180925T155550/IMG_DATA/R10m/T17PLK_20180925T155521_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180727T155531_N0206_R011_T17PLK_20180727T204908.SAFE/GRANULE/L2A_T17PLK_A016167_20180727T155526/IMG_DATA/R10m/T17PLK_20180727T155531_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180217T155521_N0206_R011_T17PLK_20180217T221531.SAFE/GRANULE/L2A_T17PLK_A013879_20180217T160029/IMG_DATA/R10m/T17PLK_20180217T155521_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180617T155531_N0206_R011_T17PLK_20180617T204705.SAFE/GRANULE/L2A_T17PLK_A015595_20180617T155525/IMG_DATA/R10m/T17PLK_20180617T155531_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180806T155531_N0206_R011_T17PLK_20180806T204419.SAFE/GRANULE/L2A_T17PLK_A016310_20180806T155756/IMG_DATA/R10m/T17PLK_20180806T155531_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180309T155521_N0206_R011_T17PLK_20180309T204813.SAFE/GRANULE/L2A_T17PLK_A014165_20180309T155521/IMG_DATA/R10m/T17PLK_20180309T155521_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20181224T155521_N0207_R011_T17PLK_20181224T191439.SAFE/GRANULE/L2A_T17PLK_A018312_20181224T155529/IMG_DATA/R10m/T17PLK_20181224T155521_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180508T155531_N0206_R011_T17PLK_20180508T223957.SAFE/GRANULE/L2A_T17PLK_A015023_20180508T155528/IMG_DATA/R10m/T17PLK_20180508T155531_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20181124T155521_N0207_R011_T17PLK_20181124T191524.SAFE/GRANULE/L2A_T17PLK_A017883_20181124T155518/IMG_DATA/R10m/T17PLK_20181124T155521_B02_10m.tif
data/selected_files_l2a/S2B_MSIL2A_20180113T155519_N0206_R011_T17PLK_20180113T190318.SAFE/GRANULE/L2A_T17PLK_A004470_20180113T160002/IMG_DATA/R10m/T17PLK_20180113T155519_B02_10m.tif
data/selected_files_l2a/S2B_MSIL2A_20180103T155519_N0206_R011_T17PLK_20180103T221514.SAFE/GRANULE/L2A_T17PLK_A004327_20180103T160032/IMG_DATA/R10m/T17PLK_20180103T155519_B02_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20181025T155521_N0206_R011_T17PLK_20181025T192616.SAFE/GRANULE/L2A_T17PLK_A017454_20181025T155522/IMG_DATA/R10m/T17PLK_20181025T155521_B02_10m.tif

Printing files in SCL head:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180816T155521_N0206_R011_T17PLK_20180816T213001.SAFE/GRANULE/L2A_T17PLK_A016453_20180816T155524/IMG_DATA/R10m/T17PLK_20180816T155521_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180925T155521_N0206_R011_T17PLK_20180925T204924.SAFE/GRANULE/L2A_T17PLK_A017025_20180925T155550/IMG_DATA/R10m/T17PLK_20180925T155521_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180727T155531_N0206_R011_T17PLK_20180727T204908.SAFE/GRANULE/L2A_T17PLK_A016167_20180727T155526/IMG_DATA/R10m/T17PLK_20180727T155531_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180217T155521_N0206_R011_T17PLK_20180217T221531.SAFE/GRANULE/L2A_T17PLK_A013879_20180217T160029/IMG_DATA/R10m/T17PLK_20180217T155521_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180617T155531_N0206_R011_T17PLK_20180617T204705.SAFE/GRANULE/L2A_T17PLK_A015595_20180617T155525/IMG_DATA/R10m/T17PLK_20180617T155531_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180806T155531_N0206_R011_T17PLK_20180806T204419.SAFE/GRANULE/L2A_T17PLK_A016310_20180806T155756/IMG_DATA/R10m/T17PLK_20180806T155531_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180309T155521_N0206_R011_T17PLK_20180309T204813.SAFE/GRANULE/L2A_T17PLK_A014165_20180309T155521/IMG_DATA/R10m/T17PLK_20180309T155521_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20181224T155521_N0207_R011_T17PLK_20181224T191439.SAFE/GRANULE/L2A_T17PLK_A018312_20181224T155529/IMG_DATA/R10m/T17PLK_20181224T155521_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20180508T155531_N0206_R011_T17PLK_20180508T223957.SAFE/GRANULE/L2A_T17PLK_A015023_20180508T155528/IMG_DATA/R10m/T17PLK_20180508T155531_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20181124T155521_N0207_R011_T17PLK_20181124T191524.SAFE/GRANULE/L2A_T17PLK_A017883_20181124T155518/IMG_DATA/R10m/T17PLK_20181124T155521_SCL_10m.tif
data/selected_files_l2a/S2B_MSIL2A_20180113T155519_N0206_R011_T17PLK_20180113T190318.SAFE/GRANULE/L2A_T17PLK_A004470_20180113T160002/IMG_DATA/R10m/T17PLK_20180113T155519_SCL_10m.tif
data/selected_files_l2a/S2B_MSIL2A_20180103T155519_N0206_R011_T17PLK_20180103T221514.SAFE/GRANULE/L2A_T17PLK_A004327_20180103T160032/IMG_DATA/R10m/T17PLK_20180103T155519_SCL_10m.tif
data/selected_files_l2a/S2A_MSIL2A_20181025T155521_N0206_R011_T17PLK_20181025T192616.SAFE/GRANULE/L2A_T17PLK_A017454_20181025T155522/IMG_DATA/R10m/T17PLK_20181025T155521_SCL_10m.tif

BO2 of length: 14
SCL of length: 14
TESTING FOR LOOP
0
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

1
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180816T155521_N0206_R011_T17PLK_20180816T213001.SAFE/GRANULE/L2A_T17PLK_A016453_20180816T155524/IMG_DATA/R10m/T17PLK_20180816T155521_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

2
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180925T155521_N0206_R011_T17PLK_20180925T204924.SAFE/GRANULE/L2A_T17PLK_A017025_20180925T155550/IMG_DATA/R10m/T17PLK_20180925T155521_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

3
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180727T155531_N0206_R011_T17PLK_20180727T204908.SAFE/GRANULE/L2A_T17PLK_A016167_20180727T155526/IMG_DATA/R10m/T17PLK_20180727T155531_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

4
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180217T155521_N0206_R011_T17PLK_20180217T221531.SAFE/GRANULE/L2A_T17PLK_A013879_20180217T160029/IMG_DATA/R10m/T17PLK_20180217T155521_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

5
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180617T155531_N0206_R011_T17PLK_20180617T204705.SAFE/GRANULE/L2A_T17PLK_A015595_20180617T155525/IMG_DATA/R10m/T17PLK_20180617T155531_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

6
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180806T155531_N0206_R011_T17PLK_20180806T204419.SAFE/GRANULE/L2A_T17PLK_A016310_20180806T155756/IMG_DATA/R10m/T17PLK_20180806T155531_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

7
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180309T155521_N0206_R011_T17PLK_20180309T204813.SAFE/GRANULE/L2A_T17PLK_A014165_20180309T155521/IMG_DATA/R10m/T17PLK_20180309T155521_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

8
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20181224T155521_N0207_R011_T17PLK_20181224T191439.SAFE/GRANULE/L2A_T17PLK_A018312_20181224T155529/IMG_DATA/R10m/T17PLK_20181224T155521_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

9
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20180508T155531_N0206_R011_T17PLK_20180508T223957.SAFE/GRANULE/L2A_T17PLK_A015023_20180508T155528/IMG_DATA/R10m/T17PLK_20180508T155531_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

10
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20181124T155521_N0207_R011_T17PLK_20181124T191524.SAFE/GRANULE/L2A_T17PLK_A017883_20181124T155518/IMG_DATA/R10m/T17PLK_20181124T155521_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

11
B02 element is:
data/selected_files_l2a/S2B_MSIL2A_20180113T155519_N0206_R011_T17PLK_20180113T190318.SAFE/GRANULE/L2A_T17PLK_A004470_20180113T160002/IMG_DATA/R10m/T17PLK_20180113T155519_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

12
B02 element is:
data/selected_files_l2a/S2B_MSIL2A_20180103T155519_N0206_R011_T17PLK_20180103T221514.SAFE/GRANULE/L2A_T17PLK_A004327_20180103T160032/IMG_DATA/R10m/T17PLK_20180103T155519_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

13
B02 element is:
data/selected_files_l2a/S2A_MSIL2A_20181025T155521_N0206_R011_T17PLK_20181025T192616.SAFE/GRANULE/L2A_T17PLK_A017454_20181025T155522/IMG_DATA/R10m/T17PLK_20181025T155521_B02_10m.tif
SCL element is:
data/selected_files_l2a/S2A_MSIL2A_20180408T155531_N0206_R011_T17PLK_20180408T204658.SAFE/GRANULE/L2A_T17PLK_A014594_20180408T155557/IMG_DATA/R10m/T17PLK_20180408T155531_SCL_10m.tif
-------------------------------

I am just wondering if there is method to effectively iterate over two lists in C simultaneously, compared to the built-in methods in Python like zip? How to iterate through two lists in parallel?

My best try was the one in the code, but although I declared the scl_index in the next iteration it has to be the next value in the linked-list, it returns in every iteration the next value of B02 (which is the first desirable output), but sticks with the very first element of the second linked-list all the way until all elements of the first list are depleted.

Have anyone an idea about it. !?

Roger Almengor
  • 442
  • 5
  • 16
  • That doesn't really look like a [**Minimal**, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). And if the code is working (very important) then perhaps this would be better of on the [Code Review SE site](https://codereview.stackexchange.com/tour)? – Some programmer dude Nov 14 '19 at 08:22
  • 1
    That's too much code for me to look through, but I would bet the problem stems from `index=index->next, index_scl->next` not doing what you think it does. `,` is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations. – LegendofPedro Nov 14 '19 at 08:24
  • The whole statement `for ((index = B02) && (index_scl = SCL); (index !=NULL) & (index_scl !=NULL); index=index->next, index_scl->next)` doesn't make much sense, and have some flaws that could lead to serious problems (and *undefined behavior*). So this is definitely not "working" and shouldn't be on the Code Review site. – Some programmer dude Nov 14 '19 at 08:29
  • The compiler should be generating a warning that tells you exactly what the problem is. What compiler are you using? What command line did you use to compile? And what was the output from the compiler? – user3386109 Nov 14 '19 at 08:33
  • I compile using cc -o filename -lgdal -Wall – Roger Almengor Nov 14 '19 at 08:35
  • It's true, I got the following warning: ```list_dir_files_5.c:228:24: warning: value computed is not used [-Wunused-value] for ((index = B02) && (index_scl = SCL); ^~ list_dir_files_5.c:230:23: warning: right-hand operand of comma expression has no effect [-Wunused-value] index=index->next , index_scl->next) ``` – Roger Almengor Nov 14 '19 at 08:35
  • Yup, apparently you meant to write `index_scl = index_scl->next`, but omitted the assignment. – user3386109 Nov 14 '19 at 08:42
  • 1
    Note that while you're learning C, you should compile with `-Werror`. That way, you won't miss the warnings (because the compiler won't give you an executable until you fix all of the warnings). – user3386109 Nov 14 '19 at 08:47

1 Answers1

3

The problem is in the for loop.

You should modify it to

for (index = B02,index_scl = SCL; 
    (index !=NULL) && (index_scl !=NULL); 
     index=index->next,index_scl = index_scl->next)
{
    ...
}
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • 1
    The bitwise AND `&` is probably supposed to be a logical AND `&&` as well. – Some programmer dude Nov 14 '19 at 08:49
  • I modified the code in that way, and it worked. I declared the index_scl within the the for loop, and then assigned the index_scl value to the next in the linked list, and works fine! – Roger Almengor Nov 14 '19 at 08:57
  • ```for (index = B02; index !=NULL; index=index->next) { printf("%d\n", ctr); ctr++; printf("B02 element is:\n%s\n", index->data); printf("SCL element is:\n%s\n", index_scl->data); index_scl = index_scl->next; printf("-------------------------------\n"); printf("\n"); }``` – Roger Almengor Nov 14 '19 at 08:58
  • @RogerAlmengor - In the case you showed in the comment above, if `index_scl` has fewer elements than `index` you will dereference a NULL pointer and most probably get a seg fault. – Rishikesh Raje Nov 14 '19 at 09:01