Article View: comp.lang.c.moderated
Article #2271Re: a problem with printf
From: motte
Date: Wed, 13 Mar 1996 13:14
Date: Wed, 13 Mar 1996 13:14
155 lines
3290 bytes
3290 bytes
----------
X-Sun-Data-Type: text
X-Sun-Data-Description: text
X-Sun-Data-Name: text
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 12
Hello,
I post the code as you asked me.
The problem is in the main function.
Now it does not run but if I let the line impr(essai) it does run very well.
If you do not find the solution, do not worry, my teacher could not solve
this problem.
thanks.
PS : sorry, but I do not speak english very well.
----------
X-Sun-Data-Type: c-file
X-Sun-Data-Description: c-file
X-Sun-Data-Name: tp.c
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 129
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define boolean unsigned char
#define maxi 10 /* taille du tableau de fichiers */
struct fic {
char *nom;
unsigned int taille;
int jj;
int mm;
int aa; };
typedef struct fic fichier;
typedef fichier *pf; /* pointeur de fichiers */
void acces ( fichier *t, pf *a );
boolean inf ( fichier f1, fichier f2 );
void trier ( pf *f );
void creefichier ( fichier *ff ); /* remplit un tableau de fichier */
void impr ( fichier *ff ); /* imprime un fichier */
void echanger ( fichier *f1, fichier *f2 );
void imprime ( pf *p );
int main ( void )
{ fichier *essai;
fichier *sauve;
pf *ficptr;
pf *sauvfp;
essai=(fichier *)malloc(sizeof(fichier)*maxi);
sauve=essai;
creefichier(essai);
ficptr=(pf *)malloc(sizeof(pf)*maxi);
sauvfp=ficptr;
essai=sauve;
/* impr(essai); */ /* HERE IS THE PROBLEM IT RUNS ONLY I LET THIS LINE */
acces(essai,ficptr);
ficptr=sauvfp;
imprime(ficptr);
printf("\n");
ficptr=sauvfp;
trier(ficptr);
ficptr=sauvfp;
imprime(ficptr);
free(ficptr); /* liberation de la memoire */
free(essai);
return 0;
}
void impr ( fichier *ff )
{
printf("%s %d %d %d %d\n",ff->nom,ff->taille,ff->jj,ff->mm,ff->aa);
}
void creefichier ( fichier *ff )
{
int i;
time_t t;
srand((unsigned) time(&t));
/* initialise le generateur de nbre aleatoires */
for (i=0; i<maxi; ++i )
{ ff->nom="cedric";
ff->taille=rand() % 2000;
ff->jj=rand() % 31;
ff->mm=rand() % 12;
ff->aa=(rand() % 96)+1900;
ff++; }
}
void acces ( fichier *t, pf *a )
{
int i;
for (i=0; i<maxi; ++i) (*(a++))=(t++);
}
boolean inf ( fichier f1, fichier f2 )
{
if (f1.taille<f2.taille) return 1;
else return 0;
}
void echanger ( fichier *f1, fichier *f2 )
{
fichier *tmp;
*tmp=*f1;
*f1=*f2;
*f2=*tmp;
}
void trier ( pf *f )
{
int i,j;
fichier *fic1,*fic2;
pf *s=f;
/* tri a bulle */
for (j=maxi; j>1; --j) {
f=s;
for (i=1; i<j; ++i) {
fic1=(*f);
fic2=(*(f+1));
if (inf(*fic2,*fic1)) echanger(fic1,fic2);
f++; }
}
}
void imprime ( pf *p )
{
int i;
fichier *f;
for (i=0; i<maxi; ++i)
{ f=*p;
impr(f);
p++; }
}
Technical Details
Message-ID:
<4i6hn8$pc9@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: motte@ens.lifl.fr Newsgroups: comp.lang.c.moderated Subject: Re: a problem with printf Date: 13 Mar 1996 07:14:16 -0600 Organization: Digital Solutions Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4i6hn8$pc9@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com