Thread: Help Me!!

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    Help Me!!

    I am mystified:

    I am simply outputting a 2D array to a file but not all of the data is being copied into the file with the output sometimes even stopping in the middle of an array element:

    example:

    1234.00 1234.00
    1234.00 1234.00
    1234.00 12(just stops here)

    I am certain the output loop is running through the correct number of iterations and if I also print to screen in the same loop the correct values appear in their entirety. Anybody help?

    Cheers

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you want help with code, post code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    heres the code

    Sorry about that

    %%%preamble........

    Code:
    void write2Darray(FILE *, float **, int);
    void blobs_calculate(int, int *);
    void interpolate(float **,int,int);
    
    int main (){
      FILE *ofp;
      int     i,overlap,no_parts;
      float **blobs_overlap;
    
      blobs_calculate(overlap,&no_parts);
    
      blobs_overlap=(float **)calloc(no_parts,sizeof(float *));
      for(i=0;i<no_parts;i++){
        blobs_overlap[i]=(float *)calloc(2,sizeof(float));
      }
    
      interpolate(blobs_overlap,overlap,no_parts);
    
      write2Darray(ofp,blobs_overlap,no_parts);
    
      return 0;
    }
    
    void write2Darray(FILE *ofp, float **array, int length1){
     int i;
    
      for(i=0;i<length1;i++){
         fprintf(ofp,"%e %e\n",array[i][0],array[i][1]);
        }
    
      for(i=0;i<length1;i++){
         printf("%d %e %e\n",i,array[i][0],array[i][1]);
        }
    }
    Last edited by goxley; 06-15-2006 at 11:55 AM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A fully compileable snippet will only get your answer quicker.
    Code:
    blobs_overlap[i]==(float *)calloc(2,sizeof(float));
    Lose the casting in C. And calloc may not work as expected for floats -- why not just use malloc?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    >void main ()
    main returns int
    See the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by Dave_Sinkula
    And calloc may not work as expected for floats -- why not just use malloc?
    Why wouldn't calloc work for floats?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by bivhitscar
    Why wouldn't calloc work for floats?
    Because all-bits-zero doesn't necessarily mean a floating point zero. Same for pointers.
    http://c-faq.com/malloc/calloc.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Interesting, I had no idea that a null pointer could be nonzero. Cheers.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed