Problème lors de la modification d’un pointeur dans une fonction C

Bonjour, j’essaie d’implémenter une opération llist appréciée dans C. Mon approche consiste à implémenter toutes les opérations dans un fichier d’en-tête C et à l’inclure dans un fichier principal afin de gérer les listes chaînées.

Voici le fichier d’en-tête linkedlist.h , contenant les implémentations d’opérations

 //Here is my code for the node structure: struct node { struct node * next; char cinfo; }; //Here is a method to do a simple insertion in the begining of the list void beginInsertSL(char val, struct node ** root){ struct node *p = malloc(sizeof(struct node*)); p->cinfo = val; if(*root == NULL){ p->next = NULL; } else { p->next = *root; } *root = p; }; //Here is a method to do many insertions void beginInsertSLN(char *ch, struct node **root){ root = malloc(sizeof(struct node**)); int size, i; size = strlen(ch); i = 0; if(size > 0){ while(i cinfo); while(p->next != NULL) {p = p->next; printf("->%c", p->cinfo);} printf(" ]\n"); } } 

Voici la fonction principale pour utiliser ces méthodes

 #include "linkedlist.h" #include  int main(int argc, char *argv[]){ // declaring the root of my linkedlist struct node **root = NULL; if(argc > 0){ // inserting char in the linked list beginInsertSLN(argv[1], root); // print the linked list to see the result printSL(*root); } } 

En exécutant ce code, j’obtiens le résultat suivant:

 ./lltest stackoverflow [ s ] [ t->s ] [ a->t->s ] [ c->a->t->s ] [ k->c->a->t->s ] [ o->k->c->a->t->s ] [ v->o->k->c->a->t->s ] [ e->v->o->k->c->a->t->s ] [ r->e->v->o->k->c->a->t->s ] [ f->r->e->v->o->k->c->a->t->s ] [ l->f->r->e->v->o->k->c->a->t->s ] [ o->l->f->r->e->v->o->k->c->a->t->s ] [ w->o->l->f->r->e->v->o->k->c->a->t->s ] Erreur de segmentation (core dumped) 

Quelqu’un peut-il m’aider à comprendre ce qui me manque ici?

Il y a un désordre avec des pointeurs dans votre code

Essayez ce qui suit. Ne tenez compte que du fait que je n’ai pas divisé le programme en modules et utilisé une chaîne explicitement spécifiée. Vous pouvez le supprimer ou décommenter la définition de chaîne dans main pour tester le programme.

 #include  #include  //Here is my code for the node structure: struct node { struct node *next; char cinfo; }; //Here is a method to do a simple insertion in the begining of the list void beginInsertSL( struct node **root, char c ) { struct node *p = malloc( sizeof( struct node ) ); p->cinfo = c; p->next = *root; *root = p; } void printSL( struct node *root ); //Here is a method to do many insertions void beginInsertSLN( struct node **root, const char *s ) { while ( *s ) { beginInsertSL( root, *s++ ); printSL( *root ); } } //Here is a method to print a list, to be able to see void printSL( struct node *root ) { if ( root == NULL ) { printf( "[ ]\n" ); return; } printf( "[ %c", root->cinfo ); while( ( root = root->next ) != NULL ) { printf( "->%c", root->cinfo ); } printf(" ]\n"); } //And finally a method to clear the list void clear( struct node **root ) { while ( *root ) { struct node *tmp = *root; *root = ( *root )->next; free( tmp ); } } int main( int argc, char * argv[] ) { struct node *root = NULL; if ( argc > 1 ) beginInsertSLN( &root, argv[1] ); // char *s = "stackoverflow"; // beginInsertSLN( &root, s ); printSL( root ); clear( &root ); return 0; } 

La sortie est

 [ s ] [ t->s ] [ a->t->s ] [ c->a->t->s ] [ k->c->a->t->s ] [ o->k->c->a->t->s ] [ v->o->k->c->a->t->s ] [ e->v->o->k->c->a->t->s ] [ r->e->v->o->k->c->a->t->s ] [ f->r->e->v->o->k->c->a->t->s ] [ l->f->r->e->v->o->k->c->a->t->s ] [ o->l->f->r->e->v->o->k->c->a->t->s ] [ w->o->l->f->r->e->v->o->k->c->a->t->s ] [ w->o->l->f->r->e->v->o->k->c->a->t->s ] 

Vous modifiez une copie locale de root dans beginInsertSLN . Cela ne change pas la valeur de root dans main .

Cela devrait fonctionner:

 int main(int argc, char *argv[]){ // declaring the root of my linkedlist struct node *root = NULL; // ^^ Use node *root, not node **root if(argc > 0){ // inserting char in the linked list beginInsertSLN(argv[1], &root); // ^^ Pass the address of root, not root. // print the linked list to see the result printSL(root); } } 

Réduisons votre code au premier bogue que j’ai trouvé:

 void beginInsertSLN(char *ch, struct node **root){ root = malloc(sizeof(struct node**)); } int main(int argc, char *argv[]){ struct node **root = NULL; beginInsertSLN("stackoverflow", root); assert(root == NULL); } 

Notez que compte tenu de ce code, la root restra égale à NULL . Ce qui n’est certainement pas ce que vous vouliez.

Vous voulez probablement quelque chose comme ça:

 #include  #include  #include  struct node { struct node * next; char cinfo; }; void beginInsertSL(char val, struct node **root){ struct node *p = malloc(sizeof *p); p->cinfo = val; p->next = *root; *root = p; } void beginInsertSLN(char *str, struct node **root){ int i; for (int i=0; icinfo); while(p->next != NULL) { p = p->next; printf("->%c", p->cinfo); } printf(" ]\n"); } int main(int argc, char *argv[]){ struct node *root = NULL; beginInsertSLN("stackoverflow", &root); printSL(root); } 

En outre, cela vaut la peine d’appeler explicitement cet autre bogue:

 struct node *p = malloc(sizeof(struct node*)); 

Vous allouez la mauvaise quantité d’espace! Ce devrait être malloc(sizeof(struct node)) .