0

When I try to compile the code down below the executable does not open. I also tried debugging and I got this error in the printf of viewNode line: Failed to execute MI command: -data-evaluate-expression (l)->info Error message from debugger back end: Cannot access memory at address 0xeb

Now, I understand that I'm trying to access memory location that I shouldn't, but where's the problem?

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

typedef struct char_node *char_list;

struct char_node{
    char info;
    char_list next;
}char_node;

char_list makesNode(void);
char_list makesValueNode(char value);
char_list makesList(char_list l, char nome[]);
void viewNode(char_list l);
void viewList(char_list l);

int main(){
    char_list nuovo;
    char nome[] = "Ugo";
    makesList(nuovo, nome);
    if(nuovo != NULL)
        viewList(nuovo);

    return 0;
}

char_list makesNode(void){
    return (char_list)malloc(sizeof(struct char_node));
}

char_list makesValueNode(char value){
    char_list li = NULL;
    li = makesNode();
    li -> info = value;
    li -> next = NULL;
    return li;
}

char_list makesList(char_list nuovo, char nome[]){
    char_list head = NULL;
    int l = strlen(nome);
    l = l - 1;
    while(l >= 0 ){
        nuovo = makesValueNode(nome[l]);
            if(nuovo != NULL){
                nuovo -> next = head;
                head = nuovo;
                l = l - 1;
            }
    }
    return nuovo;
}

void viewNode(char_list l){
    printf("%c", l->info);
}

void viewList(char_list l){
    while(l != NULL){
        viewNode(l);
        l = l -> next;
    }
}

  • 3
    Get out of the habit of typedefing pointers, see https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers – Barmar Oct 31 '19 at 16:47

2 Answers2

1

makeList returns the char_list pointer, but you're not assigning it to your variable.

Also, makesList never uses the nuovo parameter, there's no need for it.

So it should be:

int main(){
    char nome[] = "Ugo";
    char_list nuovo = makesList(nome);
    if(nuovo != NULL)
        viewList(nuovo);
    return 0;
}

char_list makesList(char nome[]){
    char_list nuovo;
    char_list head = NULL;
    int l = strlen(nome);
    l = l - 1;
    while(l >= 0 ){
        nuovo = makesValueNode(nome[l]);
        if(nuovo != NULL){
            nuovo -> next = head;
            head = nuovo;
            l = l - 1;
        }
    }
    return nuovo;
}

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you! This is obviously right, but the main problem isn't solved... – Clothing Diares Oct 31 '19 at 16:57
  • You have a tiny typo on line 3 of your example (stray semicolon) – Edd Inglis Oct 31 '19 at 17:00
  • @ClothingDiares, what do you mean "the main problem isn't solved"? You're not storing the shiny new pointer, so you're accessing garbage through a random old one. Fix that, fix the problem, no? – Edd Inglis Oct 31 '19 at 17:04
  • 1
    @ClothingDiares This is essentially the same as the answer you accepted, except he didn't remove the unused argument from `makesList`. – Barmar Oct 31 '19 at 17:06
0

The short answer is you're not storing your newly created list as you think you are, so when you come to print it, you're printing garbage.

int main(){
    char_list nuovo;       // <--- uninitialised pointer
    char nome[] = "Ugo";
    makesList(nuovo, nome);    // <--- not storing return value
    if(nuovo != NULL)
        viewList(nuovo);    // <--- printing some random memory, not your new list

    return 0;
}

If you change to:

    char_list nuovo = NULL;
    ...
    nuovo = makesList(nuovo, nome);

You should find it works as you expect, but it's worth thinking a little about whether you really want it to work this way.

It looks like you might have been hoping that nuovo was magically updated by the function, but you'll need to pass a pointer to it for that (...and it's a different discussion!).

Finally, as @Barmar pointed out, typedef'ing pointers is a bad habit, and makes it all the more easy to leave them uninitialised as you have, simply because they don't look like pointers.

Edd Inglis
  • 1,067
  • 10
  • 22