Dsy Network www | forum | my | didattica | howto | wiki | el goog | stats | blog | dona | rappresentanti
Homepage
 Register   Calendar   Members  Faq   Search  Logout 
.dsy:it. : Powered by vBulletin version 2.3.1 .dsy:it. > Didattica > Corsi A - F > Algoritmi e strutture dati > stringhe con scanf e puntatori
  Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
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!

Last edited by Cronovirus on 16-11-2014 at 13:21

16-11-2014 12:03
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cronovirus
dsy core staff

User info:
Registered: Jun 2012
Posts: 471 (0.10 al dì)
Location:
Corso: Magistrale in Informatica
Anno: 2
Time Online: 4 Days, 2:45:03: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
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?

16-11-2014 12:48
Click Here to See the Profile for Cronovirus Click here to Send Cronovirus a Private Message Find more posts by Cronovirus Add Cronovirus to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cronovirus
dsy core staff

User info:
Registered: Jun 2012
Posts: 471 (0.10 al dì)
Location:
Corso: Magistrale in Informatica
Anno: 2
Time Online: 4 Days, 2:45:03: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

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);
}

16-11-2014 12:54
Click Here to See the Profile for Cronovirus Click here to Send Cronovirus a Private Message Find more posts by Cronovirus Add Cronovirus to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

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?

16-11-2014 13:04
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cronovirus
dsy core staff

User info:
Registered: Jun 2012
Posts: 471 (0.10 al dì)
Location:
Corso: Magistrale in Informatica
Anno: 2
Time Online: 4 Days, 2:45:03: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Guarda sopra ;)

16-11-2014 13:07
Click Here to See the Profile for Cronovirus Click here to Send Cronovirus a Private Message Find more posts by Cronovirus Add Cronovirus to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

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

16-11-2014 13:12
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cronovirus
dsy core staff

User info:
Registered: Jun 2012
Posts: 471 (0.10 al dì)
Location:
Corso: Magistrale in Informatica
Anno: 2
Time Online: 4 Days, 2:45:03: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Si certo XD

16-11-2014 13:13
Click Here to See the Profile for Cronovirus Click here to Send Cronovirus a Private Message Find more posts by Cronovirus Add Cronovirus to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

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

16-11-2014 13:17
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cronovirus
dsy core staff

User info:
Registered: Jun 2012
Posts: 471 (0.10 al dì)
Location:
Corso: Magistrale in Informatica
Anno: 2
Time Online: 4 Days, 2:45:03: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Di niente :D

16-11-2014 13:19
Click Here to See the Profile for Cronovirus Click here to Send Cronovirus a Private Message Find more posts by Cronovirus Add Cronovirus to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

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;
}

16-11-2014 13:39
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Cronovirus
dsy core staff

User info:
Registered: Jun 2012
Posts: 471 (0.10 al dì)
Location:
Corso: Magistrale in Informatica
Anno: 2
Time Online: 4 Days, 2:45:03: [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

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. "

16-11-2014 13:55
Click Here to See the Profile for Cronovirus Click here to Send Cronovirus a Private Message Find more posts by Cronovirus Add Cronovirus to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
albakiara
.illuminato.

User info:
Registered: Sep 2010
Posts: 157 (0.03 al dì)
Location: Pavia
Corso: informatica
Anno: 3
Time Online: 19:31:35 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

ah già, grazie :)

16-11-2014 14:03
Click Here to See the Profile for albakiara Click here to Send albakiara a Private Message Find more posts by albakiara Add albakiara to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 00:50.    Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread | Add to Bookmarks

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

Powered by: vBulletin v2.3.1 - Copyright ©2000 - 2002, Jelsoft Enterprises Limited
Mantained by dsy crew (email) | Collabora con noi | Segnalaci un bug | Archive | Regolamento | Licenze | Thanks | Syndacate
Pagina generata in 0.046 seconds (85.73% PHP - 14.27% MySQL) con 26 query.