Article View: comp.lang.c.moderated
Article #2346Re: a problem with printf
From: Jon Custer
Date: Wed, 20 Mar 1996 04:31
Date: Wed, 20 Mar 1996 04:31
79 lines
2813 bytes
2813 bytes
In <4i6hn8$pc9@solutions.solon.com>, motte@ens.lifl.fr writes:
>I post the code as you asked me.
>The problem is in the main function.
No it isn't.
>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.
No problem, my french is a bit rusty these days...
< much code munched in various places >
< dangers of
>void creefichier ( fichier *ff )
>{
> int i;
> time_t t;
>
> srand((unsigned) time(&t));
> /* initialise le generateur de nbre aleatoires */
>
You would be better off initailizing this once elsewhere
(say in the main routine).
Also, for more general use, you never allocated space to hold the
name strings. The char* in the fichier structure will not do that
automatically. It only points somewhere, it doesn't create that somewhere.
>void echanger ( fichier *f1, fichier *f2 )
>{
> fichier *tmp;
> *tmp=*f1;
And just what does tmp point to? Here is the problem.
> *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++; }
> }
>
>
>}
OK, you have set up an array of structures, and an array of pointers
to each structure. On the basis of an element in each structure,
you want to rearrange the pointers so that when you traverse the array
of pointers, the structures that they point to are in some defined order.
Does this section of code actually do that? No, I think not. It does,
however, write stuff into the random places tmp happens to be pointing to
(generating an access violation in OS/2 which terminates the process).
To swap the pointers in echanger, you have to pass pointers to the pointers
so that you will actually change them (pass-by-value, remember).
Or, do it inside the function trier so you avoid the call overhead.
The details are left as an exercise for the reader.
Jon Custer
Technical Details
Message-ID:
<4io1mr$ntd@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4i6hn8$pc9@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: jscuste@sandia.gov (Jon Custer) Newsgroups: comp.lang.c.moderated Subject: Re: a problem with printf Date: 19 Mar 1996 22:31:23 -0600 Organization: Sandia National Laboratories Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4io1mr$ntd@solutions.solon.com> References: <4i6hn8$pc9@solutions.solon.com> Reply-To: jscuste@sandia.gov NNTP-Posting-Host: solutions.solon.com X-Nntp-Posting-Host: custerjs.mdl.sandia.gov X-Newsreader: IBM NewsReader/2 v1.00