|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 783
|
[C] Passaggio struttura a funzione
Salve a tutti non riesco a passare in modo corretto una struttura ad una funzione. Ho visto degli esempi su internet ma non riesco a scriverla in modo corretto. Ho riportato giusto le righe piu significative di quello che ho scritto
Codice:
#include <stdio.h> blablablabla struct mydata{ double matrice[10][5000]; double info[5]; } int funzione(bla bla, struct mydata struttura) { /*Qui non dovrei riuscire a lavorare con struttura.matrice[][] e con struttura.inf[]???*/ /*Mi da errore. Cosa sbaglio nella sintassi?*/ int main() { struct mydata dati; ............. funzione(bla bla, dati) } Codice:
error C2440: 'function' : cannot convert from 'mydata' to 'double (*)[5000]'
__________________
"May the wind always be at your back and the sun upon your face. And may the wings of destiny carry you aloft to dance with the stars...." Ultima modifica di salvodel : 08-01-2008 alle 09:39. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Secondo me non hai riportato correttamente il problema...perché il codice hai scritto, a parte il ; alla fine della struttura, va alla perfezione.
Puoi riportare il punto esatto in cui ti rileva l'errore ? |
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 783
|
Quote:
![]() ![]() Grazie e scusa il disturbo
__________________
"May the wind always be at your back and the sun upon your face. And may the wings of destiny carry you aloft to dance with the stars...." |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Succede succede
![]() |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 783
|
Non funziona
![]() Codice:
int legge(int punti, struct mydata data) { FILE *fdati; int i=0; int t=0; if((fdati=fopen(stringa,"r"))==NULL) printf("Impossibile aprire il file"); else { printf("Dati letti:\n"); while(fscanf(fdati,"%LE %LE %LE %LE %LE %LE %LE",&data.matrice[0][i],&data.matrice[1][i],&data.matrice[2][i],&data.matrice[3][i],&data.matrice[4][i],&data.matrice[5][i],&data.matrice[6][i])>0) { printf("."); i++; if(i>punti) { printf("\n"); break; } } } fclose(fdati); return i; } Codice:
/*Intestazione della funzione*/ legge(int p, struct mydata *punt_struct) { /*Qui mi muovo con punt_struct->.matrice[][]?*/ } int main() { struct mydata data; legge(100,&data) }
__________________
"May the wind always be at your back and the sun upon your face. And may the wings of destiny carry you aloft to dance with the stars...." |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Togli il . dopo ->...
Devi fare un puntatore a struttura perché cambi i valori all'interno della struttura...il passaggio per copia in questo caso non va bene. |
![]() |
![]() |
![]() |
#7 | |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 783
|
Perche invece su internet ho trovato cosi? Va be questa è una domanda meno importante...solo per curiosità
Quote:
Codice:
#include <stdlib.h> struct struttura { char nome[10]; int eta; }; int inserisci(int num, struct struttura *punt) { int i; for(i=0;i<num;i++) { printf("Nome: "); scanf("%s",punt->nome); printf("Eta: "); scanf("%d",punt->eta); } return 1; } int main() { int num,i; struct struttura elenco; printf("Quanti nominativi vuoi inserire: "); scanf("%d",&num); inserisci(num, &elenco); for(i=0;i<num;i++) printf("Nome: %s.\tEta: %d\n", elenco.nome, elenco.eta); system("PAUSE"); return 1; } Grazie EDIT Sono riuscito a passare anche un array di struttura ma continuo ad avere l'errore nella scanf dell'età. Se la tolgo il programma gira e mi memorizza i nomi correttamente. Codice:
#include <stdio.h> #include <stdlib.h> struct struttura { char nome[10]; int eta; }; int inserisci(int num, struct struttura *punt) { int i; for(i=0;i<num;i++) { printf("Nome: "); scanf("%s",punt[i].nome); printf("Eta: "); scanf("%d",punt[i].eta); } return 1; } int main() { int num,i; struct struttura elenco[5]; printf("Quanti nominativi vuoi inserire: "); scanf("%d",&num); inserisci(num, elenco); for(i=0;i<num;i++) printf("Nome: %s.\tEta: %d\n", elenco[i].nome, elenco[i].eta); system("PAUSE"); return 1; }
__________________
"May the wind always be at your back and the sun upon your face. And may the wings of destiny carry you aloft to dance with the stars...." Ultima modifica di salvodel : 08-01-2008 alle 12:04. |
|
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Manca la & nella scanf
![]() |
![]() |
![]() |
![]() |
#9 |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 783
|
Ordine e Metodo........sono un P
Grazie mille cionci.
__________________
"May the wind always be at your back and the sun upon your face. And may the wings of destiny carry you aloft to dance with the stars...." |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 01:38.