Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Recensione HONOR Magic V6: spessore record e super batteria. È lui il fold da battere?
Recensione HONOR Magic V6: spessore record e super batteria. È lui il fold da battere?
HONOR Magic V6 è arrivato in Italia a 2.299,90 euro con una promessa precisa: unire 4 mm di spessore da aperto (8,75 mm chiuso nel modello White, 9 mm negli altri colori) a una batteria da 6.660 mAh, la più capiente mai vista su un pieghevole. Lo abbiamo usato per oltre una settimana in versione Red 16/512 GB per capire se lo Snapdragon 8 Elite Gen 5 tiene testa alla concorrenza anche fuori dai benchmark ufficiali
Redmi Pad 2 9.7: ampio display, economico e peso contenuto, ma qualche limite nelle prestazioni
Redmi Pad 2 9.7: ampio display, economico e peso contenuto, ma qualche limite nelle prestazioni
Redmi Pad 2 9.7 punta su un display ampio e fluido, una batteria capace di accompagnare l'uso quotidiano senza ansie da ricarica e un prezzo accessibile, a partire da 179,90 euro per la versione con 64 GB di storage. Lo Snapdragon 6s 4G Gen 2 e i 4 GB di RAM della configurazione più diffusa frenano però chi cerca reattività e multitasking spinto: ecco il bilancio dopo due settimane di prova diretta
Peugeot Polygon Concept: ecco il futuro delle utilitarie
Peugeot Polygon Concept: ecco il futuro delle utilitarie
Polygon è la concept car di Peugeot che mostra il futuro delle soluzioni del segmento B: tra design compatti e innovativi affiancati da dimensioni compatte uno scherzo dalla manovrabilità incredibile per le manovre a bassa velocità
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 08-07-2008, 12:58   #1
Jack-Carter
Member
 
Iscritto dal: Oct 2007
Messaggi: 55
[C]Problema Realloc Struttura

Salve a tutti, avevo postato già qualche giorno fa per un problema riguardo ad una malloc, senza riscuotere molto successo. Provo a riproporre il problema che si presenta questa volta con i campi di una struttura. Il programma deve leggere da un file la sequenza di mosse di alcune partite di scacchi, e poi aprire un secondo file, leggere una sequenza di mosse, e dire in quali partite si è trovata quella sequenza in ordine cronologico. Il file è di questo tipo:

Quote:
5
#Roma 23.12.1999 Jack Mike 5
c4 c1
c5 c6
c7 c8
c9 c9
c4 c2
#Cairo 14.05.1996 Luke David 3
c4 c1
c5 c6
c7 c8
#Parigi 03.10.1998 Mark Marcel 7
c9 c9
c4 c2
c3 c7
c1 c8
c6 c2
c8 c9
c5 c7
#Londra 01.02.1997 Albert Mitch 10
c6 c2
c8 c9
c5 c7
c9 c9
c4 c2
c3 c7
c7 c1
c9 c5
c6 c3
c4 c8
#Berlino 28.01.1997 Charlotte Sophie 14
c4 c1
c5 c6
c7 c8
c4 c1
c5 c6
c7 c8
c4 c1
c5 c6
c7 c8
c9 c9
c4 c2
c3 c7
c9 c5
c6 c3
Mentre il codice del programma scritto finora è questo:

Quote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct match{
char *loc;
char *white;
char *black;
int day, month, year;
char **moves;
int nmoves;
};

int swap(struct match *sorted, int x);

int main()
{
FILE *input, *seq;
struct match *mc, *sorted;
int i, j, day, month, year, nmoves, n, nseq, nsorted, w, x, l;
char inputn[100], c, loc[100], white[100], black[100], moves[22];
input=fopen("input.txt", "r");
fscanf(input, "%d%c", &n, &c);
mc=(struct match*)malloc(n*sizeof(struct match));
for(i=0;i<n;i++)
{
fscanf(input, "%c%s %d%c%d%c%d%c %s %s %d", &c, loc, &day, &c, &month, &c, &year, &c, white, black, &nmoves);
mc[i].nmoves=nmoves;
mc[i].day=day;
mc[i].month=month;
mc[i].year=year;
mc[i].loc=(char*)malloc((strlen(loc)+1)*sizeof(char));
mc[i].white=(char*)malloc((strlen(white)+1)*sizeof(char));
mc[i].black=(char*)malloc((strlen(black)+1)*sizeof(char));
mc[i].moves=(char**)malloc(nmoves*sizeof(char*));
strcpy(mc[i].loc, loc);
strcpy(mc[i].white, white);
strcpy(mc[i].black, black);
printf("%s %d %d %d %s %s\n", loc, day, month, year, white, black);
fscanf(input, "%c", &c);
for(j=0;j<nmoves;j++)
{
fgets(moves, 22, input);
mc[i].moves[j]=(char*)malloc((strlen(moves)+1)*sizeof(char));
strcpy(mc[i].moves[j], moves);
}

}
fclose(input);
char **seqmov;
do{
//printf("Inserisci il nome del file con la sequenza di mosse ");
//scanf("%s", inputn);
if(strcmp(inputn, "fine")!=0)
{
seq=fopen("mov1.txt", "r");
fscanf(seq, "%d%c", &nseq, &c);
seqmov=(char**)malloc(nseq*sizeof(char*));
for(i=0;i<nseq;i++)
{
fgets(moves, 22, seq);
seqmov[i]=(char*)malloc((strlen(moves)+1)*sizeof(char));
strcpy(seqmov[i], moves);
}
l=strlen(seqmov[i-1]);
seqmov[i-1][l]='\n';
seqmov[i-1][l+1]='\0';
nsorted=0;
for(i=0;i<n;i++)
{
for(j=0;j<mc[i].nmoves;j++)
{
if(strcmp(mc[i].moves[j], seqmov[0])==0)
{
int z=j+1, y=1;
while((z<j+nseq)&&(y!=0)&&(z<mc[i].nmoves))
{
if(strcmp(mc[i].moves[z], seqmov[y])==0)
{
//printf("stringa uguale, i=%d, z=%d, y=%d\n", i, z, y);
z++;
y++;
}
else
{
y=0;
}
}
if(y==nseq)
{
if(nsorted==0)
{
sorted=(struct match*)malloc(sizeof(struct match));
sorted[nsorted].day=mc[i].day;
sorted[nsorted].month=mc[i].month;
sorted[nsorted].year=mc[i].year;
sorted[nsorted].nmoves=j;
sorted[nsorted].loc=(char*)malloc(sizeof(char));
sorted[nsorted].white=(char*)malloc(sizeof(char));
sorted[nsorted].black=(char*)malloc(sizeof(char));
strcpy(sorted[nsorted].loc, mc[i].loc);
strcpy(sorted[nsorted].white, mc[i].white);
strcpy(sorted[nsorted].black, mc[i].black);

}
else
{
sorted=(struct match*)realloc(sorted, (nsorted+2));
sorted[nsorted].day=mc[i].day;
sorted[nsorted].month=mc[i].month;
sorted[nsorted].year=mc[i].year;
sorted[nsorted].nmoves=j;
sorted[nsorted].loc=(char*)malloc(sizeof(char));
sorted[nsorted].white=(char*)malloc(sizeof(char));
sorted[nsorted].black=(char*)malloc(sizeof(char));
strcpy(sorted[nsorted].loc, mc[i].loc);
strcpy(sorted[nsorted].white, mc[i].white);
strcpy(sorted[nsorted].black, mc[i].black);


}
for(w=1;w<nsorted+1;w++)
{
for(x=0;x<nsorted;x++)
{
if(sorted[x].year>sorted[x+1].year)
{
swap(sorted, x);
printf("%d", sorted[x].year);
}
else if(sorted[x].year==sorted[x+1].year)
{
if(sorted[x].month>sorted[x+1].month)
{
swap(sorted, x);
}
else if(sorted[x].month==sorted[x+1].month)
{
if(sorted[x].day>sorted[x+1].day)
{
swap(sorted, x);
}
}
}
}
}
nsorted++;
j=z;
}

}

}
}
for(i=0;i<nsorted;i++)
{
printf("%s %d.%d.%d %s %s %d\n", sorted[i].loc, sorted[i].day, sorted[i].month, sorted[i].year, sorted[i].white, sorted[i].black, sorted[i].nmoves);
}
free(sorted);
free(seqmov);
fclose(seq);
}
}while(n==0);
//while(strcmp(inputn, "fine")!=0);
return 0;
}


int swap(struct match *sorted, int x)
{
struct match temp;

temp.day=sorted[x].day;
temp.month=sorted[x].month;
temp.year=sorted[x].year;
temp.nmoves=sorted[x].nmoves;
temp.loc=(char*)malloc(sizeof(char));
temp.white=(char*)malloc(sizeof(char));
temp.black=(char*)malloc(sizeof(char));
strcpy(temp.loc, sorted[x].loc);
strcpy(temp.white, sorted[x].white);
strcpy(temp.black, sorted[x].black);

sorted[x].day=sorted[x+1].day;
sorted[x].month=sorted[x+1].month;
sorted[x].year=sorted[x+1].year;
sorted[x].nmoves=sorted[x+1].nmoves;
sorted[x].loc=(char*)realloc(sorted[x].loc, sizeof(strlen(sorted[x+1].loc)+1));
sorted[x].white=(char*)realloc(sorted[x].white, sizeof(strlen(sorted[x+1].white)+1));
sorted[x].black=(char*)realloc(sorted[x].black, sizeof(strlen(sorted[x+1].black)+1));
strcpy(sorted[x].loc, sorted[x+1].loc);
strcpy(sorted[x].white, sorted[x+1].white);
strcpy(sorted[x].black, sorted[x+1].black);

sorted[x+1].day=temp.day;
sorted[x+1].month=temp.month;
sorted[x+1].year=temp.year;
sorted[x+1].nmoves=temp.nmoves;
sorted[x+1].loc=(char*)realloc(sorted[x+1].loc, strlen(temp.loc)+1);
sorted[x+1].white=(char*)realloc(sorted[x+1].white, strlen(temp.white)+1);
sorted[x+1].black=(char*)realloc(sorted[x+1].black, strlen(temp.black)+1);
strcpy(sorted[x+1].loc, temp.loc);
strcpy(sorted[x+1].white, temp.white);
strcpy(sorted[x+1].black, temp.black);

return 0;
}
Il problema si presenta nelle realloc della funzione swap. ho provato anche a scrivere una cosa tipo
Quote:
sorted[x].loc=(char*)realloc(sorted[x].loc, sizeof(strlen(sorted[x+1].loc)+1)*(char));
Ma non è cambiato nulla.
Jack-Carter è offline   Rispondi citando il messaggio o parte di esso
Old 09-07-2008, 19:04   #2
Jack-Carter
Member
 
Iscritto dal: Oct 2007
Messaggi: 55
Up.
Jack-Carter è offline   Rispondi citando il messaggio o parte di esso
Old 10-07-2008, 09:05   #3
banryu79
Senior Member
 
L'Avatar di banryu79
 
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
Ciao Jack-Carter, permettimi di suggerirti di wrappare il codice sorgente postato tra i tag [code] invece che i tag [quote] in modo da non perdere l'identazione: così è più probabile che qualcuno si prenda la briga di leggerlo.
Ciao.
__________________

As long as you are basically literate in programming, you should be able to express any logical relationship you understand.
If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it.
(Chris Crawford)
banryu79 è offline   Rispondi citando il messaggio o parte di esso
Old 10-07-2008, 11:06   #4
Jack-Carter
Member
 
Iscritto dal: Oct 2007
Messaggi: 55
[quote=banryu79;23251594]Ciao Jack-Carter, permettimi di suggerirti di wrappare il codice sorgente postato tra i tag [code] invece che i tag
Quote:
in modo da non perdere l'identazione: così è più probabile che qualcuno si prenda la briga di leggerlo.
Ciao.
Grazie mille, uso il forum da poco e non avevo neanche notato che c'era il comando CODE. Comunque sono finalmente riuscito a risolvere il problema .
Jack-Carter è offline   Rispondi citando il messaggio o parte di esso
Old 11-07-2008, 19:16   #5
!k-0t1c!
Member
 
Iscritto dal: Jul 2008
Messaggi: 237
Quote:
Codice:
sorted[x].loc=(char*)realloc(sorted[x].loc, sizeof(strlen(sorted[x+1].loc)+1)*(char));
Questa linea non ha senso!
Di seguito c'è qualcosa che può assumere un senso...
Codice:
sorted[x].loc = (char*)realloc(sorted[x].loc, strlen(sorted[x+1].loc+1*sizeof(char));
Magari non è quello che cerchi, ma non avevo voglia di leggere tutto il listato.
Ah, infine controlla sempre il risultato di malloc e realloc. Possono fallire e ritornare NULL, e se il tuo codice non prevede questo caso puoi andare incontro a bug fastidiosi. (meglio 2 linee di error checking che 2 ore di debugging!)
!k-0t1c! è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Recensione HONOR Magic V6: spessore record e super batteria. È lui il fold da battere? Recensione HONOR Magic V6: spessore record e sup...
Redmi Pad 2 9.7: ampio display, economico e peso contenuto, ma qualche limite nelle prestazioni Redmi Pad 2 9.7: ampio display, economico e peso...
Peugeot Polygon Concept: ecco il futuro delle utilitarie Peugeot Polygon Concept: ecco il futuro delle ut...
Reno16 Pro: il compatto di OPPO punta su fotocamera da 200MP e il nuovo Bubble! La recensione Reno16 Pro: il compatto di OPPO punta su fotocam...
 Hisense 55U7SE: tuttofare e accessibile, il MiniLED per film, sport e gioco Hisense 55U7SE: tuttofare e accessibile, il Min...
HP blocca anche le cartucce di inchiostr...
Acquisizione Apple: SigScalr chiude, gli...
Cooler Master HAF II 500 integra le vent...
I musulmani non dovrebbero usare le crip...
AMD Radeon 9000 pronte a sorpassare le N...
Soulframe, il nuovo gameplay chiarisce m...
La Commissione UE presenterà una ...
Niente più eliche: con due ali, i...
Waze si aggiorna con nuove funzioni IA: ...
Oltre 200 economisti ed esperti di IA la...
I migliori produttori del 2026? Facci sa...
Claude Code consuma fino a 33 mila token...
Si chiama Starfire il chip con cui Intel...
Chrome espone l'OS reale dietro ogni ric...
Intel investe altri 5 miliardi di euro p...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 06:14.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v