Pagina 1 di 1
Liste dinamiche di stringhe in C[risolto]
Inviato: sabato 7 giugno 2014, 16:09
da peppeg94
Ciao ragazzi, ho bisogno di una mano.... potreste controllare se questo codice è giusto?? mi da errore, non so come uscirne.
Codice: Seleziona tutto
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct entry {
char stringa;
struct entry *next;
};
char* inserisci_nodo(char*puntatoreprec, char *puntatoresucc){
struct entry *puntatore;
puntatore=(struct entry*)malloc(sizeof(struct entry));
puntatoreprec->next=puntatore;
puntatore->next=puntatoresucc;
printf("inserisci la stringa");
scanf("%s",puntatore->stringa);
}
int main () {
char a,b,c;
int x;
struct entry n1,n2,n3;
struct entry *start=&n1;
printf("inserisci le tre stringhe");
scanf("%s%s%s",&a,&b,&c);
n1.stringa=a;
n1.next=&n2;
n2.stringa=b;
n2.next=&n2;
n3.stringa=b;
n3.next=NULL;
printf("dove vuoi inserire il nuovo nodo?\npremere 0 se lo si vuole in testa;\n altrimenti premere il numero corrispondente al nodo precedente; ");
scanf("%d",&x);
switch(x) {
case(0): {
struct entry *puntatore_nuovo;
puntatore_nuovo=(struct entry*)malloc(sizeof(struct entry));
printf("inserire una nuova stringa");
scanf("%s",&puntatore_nuovo->stringa);
while(puntatore_nuovo!=NULL)
printf("%s\n",puntatore_nuovo->stringa);
puntatore_nuovo=puntatore_nuovo->next;
break;
}
case(1): {
inserisci_nodo(&n1,&n2);
while(start!=NULL){
printf("%s",start->stringa);
start=start->next;
}
break;
}
case(2):{
inserisci_nodo(&n2,&n3);
while(start!=NULL){
printf("%s",start->stringa);
start=start->next;
}
break;
}
case(3): {
struct entry *end=NULL;
inserisci_nodo(&n3,end);
while (start!=NULL){
printf("%s",start->stringa);
start=start->next;
}
break;
}
}
return 0;
}
Grazie in anticipo.
Re: Liste dinamiche di stringhe in C
Inviato: sabato 7 giugno 2014, 20:15
da vbextreme
al primo colpo d'occhio tu usi "char stringa" come fosse una stringa ma in realta è semplicemente una variabile ad 8 bit,dovresti dichiararlo cme vettore di char ad una grandezza prestabilita tipo "char stringa[80]"
il resto non l'ho analizzato forse mi rimane un pelo troppo contorto
Re: Liste dinamiche di stringhe in C
Inviato: sabato 7 giugno 2014, 20:39
da 1001001
Ciao!
Il compilatore dice:
Codice: Seleziona tutto
mattia@replicant:Scrivania$ gcc foo.c -Wall
foo.c: In function ‘inserisci_nodo’:
foo.c:15:17: error: request for member ‘next’ in something not a structure or union
foo.c:16:19: warning: assignment from incompatible pointer type [enabled by default]
foo.c:18:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c: In function ‘main’:
foo.c:53:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:59:10: warning: passing argument 1 of ‘inserisci_nodo’ from incompatible pointer type [enabled by default]
foo.c:11:7: note: expected ‘char *’ but argument is of type ‘struct entry *’
foo.c:59:10: warning: passing argument 2 of ‘inserisci_nodo’ from incompatible pointer type [enabled by default]
foo.c:11:7: note: expected ‘char *’ but argument is of type ‘struct entry *’
foo.c:61:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:69:10: warning: passing argument 1 of ‘inserisci_nodo’ from incompatible pointer type [enabled by default]
foo.c:11:7: note: expected ‘char *’ but argument is of type ‘struct entry *’
foo.c:69:10: warning: passing argument 2 of ‘inserisci_nodo’ from incompatible pointer type [enabled by default]
foo.c:11:7: note: expected ‘char *’ but argument is of type ‘struct entry *’
foo.c:71:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:81:10: warning: passing argument 1 of ‘inserisci_nodo’ from incompatible pointer type [enabled by default]
foo.c:11:7: note: expected ‘char *’ but argument is of type ‘struct entry *’
foo.c:81:10: warning: passing argument 2 of ‘inserisci_nodo’ from incompatible pointer type [enabled by default]
foo.c:11:7: note: expected ‘char *’ but argument is of type ‘struct entry *’
foo.c:83:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c: In function ‘inserisci_nodo’:
foo.c:21:1: warning: control reaches end of non-void function [-Wreturn-type]
L'errore è qua:
puntatoreprec è un
char*, non ha nessun campo
next
I warning hanno diverso livello di importanza:
1) (grave)
Stai assegnando un
char* a una variabile che dovrebbe contenere un
entry*
2) (meno grave)
In tutte queste righe, printf/scanf si aspetta una stringa ma tu gli passi un
char
3)
inserisci_nodo() si aspetta due
char* ma tu gli passi due
entry*
4)
la funzione
inserisci_nodo ha tipo di ritorno
char* ma non ritorna niente.
Re: Liste dinamiche di stringhe in C
Inviato: sabato 7 giugno 2014, 21:33
da peppeg94
ciao e grazie per la tua risposta! ho modificato un po il programma basandomi su quello che mi hai detto e me lo fa compilare solo che si blocca appena completo l'operazione. potresti aiutarmi ancora?
Codice: Seleziona tutto
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct entry {
char stringa;
struct entry *next;
};
char* inserisci_nodo(struct entry *puntatoreprec, struct entry *puntatoresucc){
struct entry *puntatore;
puntatore=(struct entry*)malloc(sizeof(struct entry));
puntatoreprec->next=puntatore;
puntatore->next=puntatoresucc;
printf("inserisci la stringa");
scanf("%s",puntatore->stringa);
}
int main () {
char a,b,c;
int x;
struct entry n1,n2,n3;
struct entry *start=&n1;
printf("inserisci le tre stringhe");
scanf("%s%s%s",&a,&b,&c);
n1.stringa='a';
n1.next=&n2;
n2.stringa='b';
n2.next=&n2;
n3.stringa='c';
n3.next=NULL;
printf("dove vuoi inserire il nuovo nodo?\npremere 0 se lo si vuole in testa;\n altrimenti premere il numero corrispondente al nodo precedente; ");
scanf("%d",&x);
switch(x) {
case(0): {
struct entry *puntatore_nuovo;
puntatore_nuovo=(struct entry*)malloc(sizeof(struct entry));
printf("inserire una nuova stringa");
scanf("%s",&puntatore_nuovo->stringa);
while(puntatore_nuovo!=NULL)
printf("%s\n",puntatore_nuovo->stringa);
puntatore_nuovo=puntatore_nuovo->next;
break;
}
case(1): {
inserisci_nodo(&n1,&n2);
while(start!=NULL){
printf("%s",start->stringa);
start=start->next;
}
break;
}
case(2):{
inserisci_nodo(&n2,&n3);
while(start!=NULL){
printf("%s",start->stringa);
start=start->next;
}
break;
}
case(3): {
struct entry *end=NULL;
inserisci_nodo(&n3,end);
while (start!=NULL){
printf("%s",start->stringa);
start=start->next;
}
break;
}
}
return 0;
}
Re: Liste dinamiche di stringhe in C
Inviato: domenica 8 giugno 2014, 15:04
da 1001001
Codice: Seleziona tutto
mattia@replicant:Scrivania$ gcc -Wall foo.c
foo.c: In function ‘inserisci_nodo’:
foo.c:18:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c: In function ‘main’:
foo.c:53:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:61:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:71:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c:83:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
foo.c: In function ‘inserisci_nodo’:
foo.c:21:1: warning: control reaches end of non-void function [-Wreturn-type]
Se il compilatore dà dei warning, un motivo c'è

e il motivo è che sta dicendo: "guarda, secondo me non è giusto, ma se proprio ne sei convinto provo a andare avanti"
Come detto da vbextreme (e da me al punto 2 sopra

), stai trattando un
char (cioè il campo
stringa di
entry) come una stringa...
PS: relativamente all'ultimo warning riportato: perché fai ritornare alla funzione
inserisci_nodo un
char*?
Re: Liste dinamiche di stringhe in C
Inviato: domenica 8 giugno 2014, 15:34
da vbextreme
Dicendo poi che in c non esistono le stringhe!
Re: Liste dinamiche di stringhe in C
Inviato: domenica 8 giugno 2014, 20:41
da peppeg94
ok ho capito quale è l'errore.. ma allora come la posso trattare quella stringa?? capitemi ho iniziato fa poco a fare programmazione

Re: Liste dinamiche di stringhe in C
Inviato: lunedì 9 giugno 2014, 19:46
da 1001001
La soluzione più facile (che non coinvolge la gestione di memoria dinamica) è fare come diceva vbextreme qualche post più in alto:
Codice: Seleziona tutto
struct entry {
char stringa[100];
struct entry *next;
};
(ho messo 100, ma puoi mettere un po' quello che vuoi). Non è sicuramente la soluzione migliore, non fosse altro che per l'ovvio motivo che un array di dimensione fissa è uno spreco di memoria se usi meno di 100 caratteri e impedisce di avere stringhe più lunghe di 100 caratteri, ma se se all'inizio immagino che tu debba ancora affrontare malloc e compagnia

Re: Liste dinamiche di stringhe in C
Inviato: martedì 10 giugno 2014, 0:01
da peppeg94
ok grazie per il vostro aiuto!! troppo gentili!
Re: Liste dinamiche di stringhe in C
Inviato: martedì 10 giugno 2014, 8:16
da jackynet92
Se ritieni risolto il problema, modifica il titolo del
primo post aggiungendo all'inizio [Risolto].
Se vuoi puoi installare
questo script che ti aggiunge un pulsante che ti permette di mettere [Risolto] con un solo click.
Alla prossima

Re: Liste dinamiche di stringhe in C
Inviato: martedì 10 giugno 2014, 10:01
da 1001001
Di nulla! Ciao!
