Thread: Reading a PPM image into a 2-dimensional array

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    3

    Reading a PPM image into a 2-dimensional array

    Hi all, I'm trying to read a PPM image into an array. The header will have already been read in and I'm trying to put in a 2-d array to make manipulations easier. This is the code I have so far with LOTS of errors and warnings and I honestly don't know what almost any of them mean.


    Code:
     
    int imageArray( FILE *input, struct pixel *theArray ){
       int i, j;
       int col, row;
    
    
       int *imageArray = (int**)malloc(row * sizeof(int*));
    
    
       for(i=0; i<row; i++){
          imageArray[i]=(int*)malloc(col * sizeof(int));
          for(j=0; j<col; j++){
             fscanf(fpi, "%d", &imageArray[i][j];
          }
       }
    
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    On line 3 you declaring 'col' and 'row'.
    On line 6 and 10 you use 'col' or 'row' for memory allocation, but you have never defined them.

    And please, stop casting the returned value of malloc.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    3
    That should just give me random values then. It'll put whatever is stored in that spot into those integers. Those values will be read in from a different function that gets the header information and has absolutely nothing to do with what any of my errors are. My professor hasn't taught us anything about malloc so I don't really understand that either. I already corrected the error that came from not having a ')' on line 12. I have two warning about me initializing a pointer, the errors are that fpi is undeclared, I thought that was an fscanf thing I could just use like stderr? The errors are all on that line saying that the subscripted value is neither array nor pointer nor vector.
    Last edited by snorem; 11-29-2014 at 09:15 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's really hard to give you good advice when your code has so many errors, and then you bound back with "yeah, well I knew that part already".

    > int *imageArray = (int**)malloc(row * sizeof(int*));
    How about
    a) this is the same name as your function
    b) it should be an int** variable

    > the errors are that fpi is undeclared
    Perhaps because your input FILE* is called input.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a PPM image into a 1d or 2d array
    By Robert Harp in forum C Programming
    Replies: 1
    Last Post: 04-04-2013, 10:18 AM
  2. Problem with reading 2 dimensional array in IO
    By Envynness in forum C Programming
    Replies: 8
    Last Post: 04-13-2011, 05:57 PM
  3. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  4. Reading a file into a two dimensional array!!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 01:32 PM
  5. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 01:31 PM

Tags for this Thread