Thread: Help with pointers and ppm images

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    23

    Help with pointers and ppm images

    Hello everyone. I have a program that is getting errors when I try to call in functions. The functions are in a parse.o file that has been given to me. I am getting the following errors and I am having trouble understanding how to fix them. If anyone could help me with this I would be very grateful. Thank you

    transform.c: In function âmainâ:
    transform.c:14:14: error: expected expression before âFILEâ
    transform.c:18:25: error: invalid operands to binary * (have âstruct pixel_t (*)[(long unsigned int)(g_width)]â and âlong unsigned intâ)
    transform.c:20:13: error: expected expression before âFILEâ
    transform.c:20:13: error: too few arguments to function âparseImageâ
    transform.h:21:6: note: declared here



    Here is the main file:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "transform.h"
    
    //void mirror(pixel_t *image, int x, int y);
    
    int main(int argc, char *argv[])
    {
        int n;
        int m;
        char *input;
        input=argv[1];
    
        parseHeader(FILE *input);
    
        pixel_t theArray[g_height][g_width];
    
        input = malloc(theArray*sizeof(pixel_t));
    
        parseImage(FILE *input, pixel *theArray);
    
        //char r = theArray[x][y].r
        //char g = theArray[x][y].g
        //char b = theArray[x][y].b
        
        printf("P6");
        printf("%d %d\n 255", g_height, g_width);
    
    
        switch (*argv[1])
            {
                   case '1':
                        for(n=0; n <= g_height-1; n++)
                {
                    for(m=0; m <= g_width-1; m++)
                    {
                        printf("%c%c%c", theArray[n][m].r, theArray[n][m].g, theArray[n][m].b);
                        ++m;
                    }
                }
            
                    //mirror(image, width, height);
                    break;
            case '2':
                    //flip(image, width, height);
                    break;
            case '3':
                    //rotate(image, height, width);
                    break;
            default:
                    printf("Invalid command line argument. User must enter a 1, 2, or 3.\n");
                    break;
        }
    
    }
    and here is my header file

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
        unsigned char r;
        unsigned char b;
        unsigned char g;
    }     pixel_t;
     
     
    pixel_t *input;
    
    struct pixel {
        char r, g, b;
    };
    
    int g_width, g_height;
    
    void parseHeader( FILE *input );
    void parseImage( FILE *input, struct pixel *theArray);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > input=argv[1];

    > parseHeader(FILE *input);
    You seem to be confused as to the difference between prototyping a function and calling a function.

    For a start, you need to OPEN the file, then pass a FILE* variable to parseHeader.
    input (at this stage) is just a char* pointer, presumably the file you want to fopen.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    Okay, so I changed the file so it includes fopen. I'm still having trouble with the second argument in parseImage.

    transform.c: In function âmainâ:
    transform.c:26:2: error: incompatible type for argument 2 of âparseImageâ
    transform.h:21:6: note: expected âstruct pixel *â but argument is of type âpixel_tâ
    transform.c:61:1: error: expected declaration or statement at end of input



    Code:
    int main(int argc, char *argv[])
    {
        int n;
        int m;
        FILE *inFile;
        char *input;
        input=argv[1];
    
        inFile = fopen("tiger.ppm", "r");
            if (inFile == NULL){
            fprintf(stderr,"Output failure. Exiting program\n");
            exit(2);
    
        parseHeader(inFile);
    
        pixel_t theArray[g_height][g_width];
    
        struct pixel * input = (struct pixel *) malloc(sizeof(struct pixel)* g_width * g_height);
    
        parseImage(inFile, theArray[g_height][g_width]);

  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
    > pixel_t theArray[g_height][g_width];
    > struct pixel * input = (struct pixel *) malloc(sizeof(struct pixel)* g_width * g_height);
    Which of these two...

    best matches the prototype of this function?
    > parseImage(inFile, theArray[g_height][g_width]);
    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. images
    By SDH in forum C Programming
    Replies: 5
    Last Post: 03-21-2013, 11:32 AM
  2. Images go bye bye
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 10-12-2006, 07:18 AM
  3. Images-again...
    By AProg in forum C Programming
    Replies: 8
    Last Post: 05-26-2003, 05:22 PM
  4. Images
    By krappykoder in forum Game Programming
    Replies: 2
    Last Post: 08-01-2002, 03:28 PM

Tags for this Thread