Mattia Righetti
Mattia Righetti

Reputation: 1360

String manipulation library

EDIT: Have included only the relevant code

I have to manipulate an input string that looks like this

create /foo
create /foo/bar
write /foo/bar "text"
create /foo/bar/baz

And I've created this program (you don't need to look at all of it) and the problem I have is with the function printAllFolders() which is called in the main() and it is defined right under the main() function. The problem must be in that function. Is it correct to pass the string in the struct path[] giving the parameter comando->path ?

When I put that function in the main it does give me segmentation fault issue. The rest works just fine.

EDIT: just to be clear, the printAllFolders() does print all the strings in the path array, so I just need to pass the path[255] array, not the one with all two indexes.

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

typedef struct _command {
    unsigned char command[10];
    unsigned char path[255][255];
    int pathLevels;
} command;

command* createCommandMul(unsigned char* str);
void printfAllFolders(unsigned char* stringhe, int lengthArray);


int main() {
    command* comando = (command*) malloc(sizeof(command));
    unsigned char* upPath = NULL;
    unsigned char* allPath = NULL;
    FILE* fp;
    unsigned char* line = NULL;
    size_t len = 0;
    ssize_t read;

    fp = fopen("/Users/mattiarighetti/Downloads/semplice.txt", "r");
    if (fp == NULL)
        exit(EXIT_FAILURE);
    while ((read = getline(&line, &len, fp)) != -1) {
            comando = createCommandMul(line);
            upPath = upperPath(comando);
            allPath = fullPath(comando);
            printfAllFolders(comando->path, comando->pathLevels);
    }
    fclose(fp);
    if (line)
        free(line);
    return 0;
}

void printfAllFolders(unsigned char* stringhe, int lengthArray) {
    unsigned char* stringa = stringhe;
    int length = lengthArray;
    if (length == 0) printf("Cartella %s", stringa[length]);
    for (int i = 0; i < length+1; i++) {
        printf("Cartella %d %s\t", i, stringa[i]);
    }
}

command* createCommandMul(unsigned char* str) {
    unsigned char* c = str;
    command* commandPointer = (command*) malloc(sizeof(command));
    int commandIndex = 0;
    int pathLevel = 0;
    int pathIndex = 0;
    /* Parte Comando */
    while(*c != ' ' && commandIndex<10) {
        commandPointer->command[commandIndex] = *c++;
        commandIndex++;
    }
    commandPointer->command[commandIndex] = '\0';
    while(*c == ' ') c++;
    while(*c == '/') c++; 
    /* Parte Path*/
    while(*c!='\0') {
        if (*c == '/') {
            commandPointer->path[pathLevel][pathIndex] = '\0';
            pathLevel++;
            pathIndex = 0;
            c++;
        } else {
            commandPointer->path[pathLevel][pathIndex] = *c++;
            pathIndex++;
        }
    }
    commandPointer->path[pathLevel][pathIndex] = '\0';
    commandPointer->pathLevels = pathLevel;
    return commandPointer;
}

Upvotes: 0

Views: 96

Answers (1)

Mahonri Moriancumer
Mahonri Moriancumer

Reputation: 6003

Perhaps there is some confusion about the difference between an array:

path[255][255]

and a pointer

unsigned char* stringhe

When passing the array -as- a pointer:

printfAllFolders(comando->path, ...

the printfAllFolders() function sees stringhe as a memory address where there happens to be an unsigned char stored. The printAllFolders() function does not know that stringhe actually points to a more complex object (array of unsigned char with [255][255] dimensions).

One fix to the question code is to change:

    void printfAllFolders(unsigned char* stringhe, int lengthArray)

to the following:

void printfAllFolders(unsigned char stringhe[255][255], int lengthArray)

This passes additional information to the function needed to understand the more complex nature of stringhe.

Of course, the following line (again) removes this needed information:

   unsigned char* stringa = stringhe;

Perhaps this line ^above^ should be eliminated?

Then change the line:

if (length == 0) printf("Cartella %s", stringa[length]);

to:

if (length == 0) printf("Cartella %s", stringhe[length]);

and then change line:

        printf("Cartella %d %s\t", i, stringa[i]);

to:

        printf("Cartella %d %s\t", i, stringhe[i]);

Upvotes: 1

Related Questions