.dsy:it.
Show 150 posts per page

.dsy:it. (http://www.dsy.it/forum/)
- Algoritmi e strutture dati (http://www.dsy.it/forum/forumdisplay.php?forumid=207)
-- stringhe con scanf e puntatori (http://www.dsy.it/forum/showthread.php?threadid=43865)


Posted by albakiara on 16-11-2014 12:03:

stringhe con scanf e puntatori [RISOLTO]

Salve a tutti,
so che sicuramente sarà una cosa banale, ma non ho capito come fare questo:
leggere una stringa di lunghezza non nota con scanf nel main, e passare il puntatore al primo carattere della stringa ad una funzione! Potete scrivermi il codice del main e il prototipo della funzione?
Grazie!


Posted by Cronovirus on 16-11-2014 12:48:

Re: stringhe con scanf e puntatori

Originally posted by albakiara
Salve a tutti,
so che sicuramente sarà una cosa banale, ma non ho capito come fare questo:
leggere una stringa di lunghezza non nota con scanf nel main, e passare il puntatore al primo carattere della stringa ad una funzione! Potete scrivermi il codice del main e il prototipo della funzione?
Grazie!


Ciao :D nel testo dell'esercizio dice anche quanto può essere lunga al massimo la stringa?


Posted by Cronovirus on 16-11-2014 12:54:

Perchè se non lo chiede puoi farlo così:

#include <stdio.h>
#include <stdlib.h>
void func(const char *p){
while(*p!='\0'){
putchar(*p);
p++;
}
}

int main(void){
char *p = malloc(sizeof(char *));
scanf("%s",p);
func(p);
}


Posted by albakiara on 16-11-2014 13:04:

Grazie per la risposta! :-D
Non è noto niente sulla lunghezza della stringa! io avevo pensato a questo:

printf ("Inserisci stringa:\n");
scanf ("%s", s);

non va bene?


Posted by Cronovirus on 16-11-2014 13:07:

Guarda sopra ;)


Posted by albakiara on 16-11-2014 13:12:

Ho guardato, x questo chiedevo se come ho fatto va bene o no? :)


Posted by Cronovirus on 16-11-2014 13:13:

Si certo XD


Posted by albakiara on 16-11-2014 13:17:

Grazie ho risolto il mio esercizio! In pratica il mio errore era non aver allocato lo spazio per char*!


Posted by Cronovirus on 16-11-2014 13:19:

Di niente :D


Posted by albakiara on 16-11-2014 13:39:

in realtà, ci sarebbe un altro piccolo problema, se inserisco una stringa contenente degli spazi, il programma non funziona!
ecco il main:
int main (void) {
char ch;
char* s = malloc (sizeof (char*));
if (s == NULL)
exit (1);
printf ("Inserisci stringa: \n");
scanf ("%s", s);
printf ("Inserisci il carattere da eliminare: \n");
scanf (" %c", &ch);
printf ("%s\n", cancella_ch(s, ch));
return 0;
}


Posted by Cronovirus on 16-11-2014 13:55:

E' normale: diverse funzioni di libreria si fermano quando leggono il carattere 'spazio'! dovresti usare un array di puntatori http://linux.die.net/man/3/scanf, in particolare dice riguardo a %s: "Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first. "


Posted by albakiara on 16-11-2014 14:03:

ah già, grazie :)


All times are GMT. The time now is 15:26.
Show all 12 posts from this thread on one page

Powered by: vBulletin Version 2.3.1
Copyright © Jelsoft Enterprises Limited 2000 - 2002.