-1

I have the below struct in my program.

typedef unsigned char BYTE;
struct data
{
BYTE a;
BYTE b;
BYTE c;
};

some lines late..... I create an instance of the struct

data buffer[100][100];

later in the program I have to typecast the "buffer" instance to char * in order to be used by another function.

int bmp_generator(char *filename, int width, int height, BYTE* data)
{
BITMAPFILEHEADER bmp_head;
BITMAPINFOHEADER bmp_info;
int size=width*height*3;

bmp_head.bfType=0x4D42;
bmp_head.bfSize=size + sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bmp_head.bfReserved1=bmp_head.bfReserved2=0;
bmp_head.bfOffBits=bmp_head.bfSize-size;

bmp_info.biSize=40;
bmp_info.biWidth=width;
bmp_info.biHeight=height;
bmp_info.biPlanes=1;
bmp_info.biBitCount=24;// bits per pixel
bmp_info.biCompress=0;
bmp_info.biSizeImage=size;
bmp_info.biXPelsPerMeter=0;
bmp_info.biYPelsPerMeter=0;
bmp_info.biClrUsed=0;
bmp_info.biClrImportant=0;

FILE *fp;
if (!(fp=fopen(filename,"wb"))) return 0;

fwrite(&bmp_head,1, sizeof(BITMAPFILEHEADER), fp);
fwrite(&bmp_info,1,sizeof(BITMAPINFOHEADER), fp);
fwrite (data, 1, size, fp);
fclose(fp);

return 1;
}

[some lines later]

bmp_generator("./test.bmp", 512, 512, (BYTE*)buffer);

However, when I run the above code the gcc compiler gives me a warning saying "warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]"

can someone help me out with this. Thanks in Advance.

Mc Missile
  • 715
  • 11
  • 25
  • 2
    That's not an instance of the struct. That's 10,000 instances of the struct. – Lightness Races in Orbit Jun 22 '18 at 09:41
  • We cannot answer this question without your [MCVE], but the problem has nothing to do with `buffer` or the type `data` — it relates to how you're handling the _first_ argument to the `THEfUNCTION` function/macro, as that's where the string constant/literal is. Please show your [MCVE] and abstract away all unrelated/unnecessary distractions. (This is debugging that you should already have performed.) – Lightness Races in Orbit Jun 22 '18 at 09:42
  • The error there has nothing to do with your `data` structure as far as the code you posted shows. We would need to see the signature of the (obnoxiously capitalized) `THEfUNCTION`. – Max Langhof Jun 22 '18 at 09:42
  • I have added the complete function. the reason why I did not put the question is because then people would start suggesting to use libraries in order to create bitmap, while i cannot do so in the project that i am involved with. Hence THEfUNCTION. – Mc Missile Jun 22 '18 at 09:48
  • Possible duplicate of [convert char\* to const char\* in C++](https://stackoverflow.com/questions/44637171/convert-char-to-const-char-in-c) – Joseph D. Jun 22 '18 at 10:14
  • "later in the program I have to typecast the "buffer" instance to char *" - So when the compiler decides that it wants to add padding into your struct because , your cast will be completely broken and you'll be feeding rubbish into your function. Make it a byte array and you'll never have to worry about padding or nasty nasty casting. – UKMonkey Jun 22 '18 at 10:52

1 Answers1

2

The warning is not about the struct but about the first argument of THEfUNCTION. It seems that the function takes char* as the first argument. Now, the string literal you pass as the argument is converted to char* but this kind of conversion is deprecated.

Generally, a good thing to do is to convert string literals to const char* as long as you have control of the signature of the function you're using.

navyblue
  • 776
  • 4
  • 8