Solved

Bitmap program compile time error

Posted on 2004-08-23
6
358 Views
Last Modified: 2013-12-03
Hi,

I've got some compile-time errors I can't understand in the following small bitmap program (takes an input bitmap and produces a modified output):

52 C:\myc\iedit.c  : initializer element is not constant
52 C:\myc\iedit.c  : [Warning] data definition has no type or storage class
54 C:\myc\iedit.c  : [Warning] data definition has no type or storage class

with some follow up errors too.  I can't understant why the initializer element needs to be constant.
I previously used the const keyword in the prototype const int doMenu(void) in iedit_menu.c  but removed it when I saw it was wrong.
I don't think the old version of the object file is being used but I could be wrong.

There are 2 source files, a menu and the implementation code  and one header file for the menu.

File 1:
-------------------------------------
Filename : iedit_menu.h
Synopsis : Header for prototypes and defines for the user menu

#define QUIT_OPTION 0
#define GRAYSCALE_OPTION 1
#define INVERT_OPTION 2

int doMenu(void);
--------------------------------

File 2:
---------------------------------
Filename : iedit_menu.c
Synopsis : Header for prototypes and defines for the user menu

#include "iedit_menu.h"

int doMenu(void)
{
  int tempChoice;
 
  for(;;)
  {
    printf("Choose an option:\r\n");
    printf("1 - Change Image to Greyscale");
    printf("2 - Invert Image Colours");
    printf("0 - Quit");
    tempChoice = getchar();
    if(tempChoice <=2 && tempChoice >0)
      return tempChoice;
    else
      printf("Invalid choice");
  }
}
-------------------------------------------------------------

File 3:
------------------------------
Filename : iedit..c
Synopsis :  Contains the main method and code for producing a new modified bitmap from an original.


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "iedit_menu.h"

#define BITMAP_HEADER_SIZE 115
#define DEBUG 0


int main(int argc, char** argv)
{
  FILE *inFile, *outFile;
  unsigned char mask;
  int i, temp, byteCounter, choice;
  int tempPart[4],signature;
  unsigned char fileType[2];
 
  mask = 0xFF;
 
    /*Check args and open source and destination files*/
  if(argc!=3)
  {
    fprintf(stderr,"Usage : iedit <source file> <destination file>");
    return EXIT_FAILURE;
  }
  inFile = fopen(argv[1],"rb");
  if(inFile == NULL)
  {
    fprintf(stderr,"iedit : Unable to open the file '%s' for reading", argv[1]);
    return EXIT_FAILURE;
  }
  outFile = fopen(argv[2],"wb");
  if(outFile == NULL)
  {
    fprintf(stderr,"iedit : Unable to open the file '%s' for reading", argv[2]);
    return EXIT_FAILURE;
  }
   
 
  fileType[0] = getc(inFile);
  fileType[1] = getc(inFile);
  fseek(inFile, 0, SEEK_SET);
  signature = ( (int)fileType[0]<<8 ) + (int)fileType[1];
  if(signature!= 0x424D)
    printf("The file does not have a recognised bitmap signature, do you wish to continue?");
    if(tolower(getChar())=='n')
      return EXIT_SUCCESS;
  }    
 
 
  choice = doMenu();
   
  byteCounter =0;  
 
    /*copy the bitmap header exactly*/
  for(i=0;i<BITMAP_HEADER_SIZE;i++)
  {
     temp = getc(inFile);
     if(temp==EOF)
     {
       fprintf(stderr,"iedit : Error, unexpected EOF found in bitmap header.\r\nSource file is likely not a bitmap or is corrupt");
       return EXIT_FAILURE;
     }
     putc(temp, outFile);    
  }
 
    /*manipulate the image data*/
  switch(choice)
  {
    case QUIT_OPTION:
      printf("Done.");
      return EXIT_SUCCESS;
      break;
    case GRAYSCALE_OPTION:
        while( (tempPart[byteCounter] = getc(inFile))!=EOF )
        {
          byteCounter++;
            /* On the 3rdbyte of a pixel, write output and reset counter*/
          if(byteCounter ==3)
          {
            byteCounter = 0;
             /*get average for grayscale intensity*/
            tempPart[0] = (tempPart[0]+tempPart[1]+tempPart[2])/3;
     
            putc(tempPart[0], outFile);
            putc(tempPart[0], outFile);
            putc(tempPart[0], outFile);
            putc(tempPart[3], outFile); /* translucency byte*/
          }
        }
        break;  
    case default :
      printf("Invalid selection");
      break;
  }
 
}
 

Thanks for any help.
0
Comment
Question by:twobitadder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Author Comment

by:twobitadder
ID: 11873856
just noticed a mistake also in the error messages for opening input and output files, they are both the same doh. (Evils of cut and paste), also once i get this sorted I'm going to modifiy the header so that it shows as greyscale and reduce the number of output bytes from 3 to 1 which is more sensible.  The real problem is the requirement for a constant initializer. I can't see why...
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
ID: 11873941
I think the problem is here:

if(signature!= 0x424D)   {    /* MISSING BRACKET */
    printf("The file does not have a recognised bitmap signature, do you wish to continue?");
    if(tolower(getChar())=='n')
      return EXIT_SUCCESS;
  }    
 
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11873957
The missing bracket after if (.....) is causing the function to close before choice = doMenu(); causing unexpected errors.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Accepted Solution

by:
brettmjohnson earned 250 total points
ID: 11874009
The primary problem is a missing open brace when testing the file signature:
      if(signature!= 0x424D)
      {  <----  add this brace

Using a text editor that auto-indents or matches braces, brackets, and parenthesis
would help find these trivial errors.

      case default:  
should be
      default:

getChar() should be getchar()

and iedit_menu.c should #include <stdio.h>

0
 
LVL 5

Author Comment

by:twobitadder
ID: 11879217
Thanks, everything's fixed except one linker error I can't figure the reason for:
 
[Linker error] undefined reference to `doMenu'

I compile it in dev-cpp which uses gcc, doMenu is declared in iedit_menu.h which is included in iedit.c and it's defined in iedit_menu.c, is this likely just an ide misconfiguration or did i make an error in the program here?

Thanks again.
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11879333
You have to link iedit.o and iedit_menu.o together when building the executable.
For such a simple program, just compile and link them in the same command:

gcc -Wall -o iedit  iedit.c iedit_menu.c



0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question