18

I am wondering why I can't compile an example from book. I simplify the example here to avoid posting example from a copyrighted book.

#include <stdio.h>

BYTE *data = "data";

int main()
{
     printf("%s", data);
     return 0;
}

When compile with g++, i get error,

error: invalid conversion from 'const char*' to 'BYTE*'

The program works by simply replacing BYTE with char, but I must be doing something wrong since the example comes from a book.

Please help pointing out the problem. Thanks.

3
  • 1
    The question is tagged C, but you say that you are using g++ which is a C++ compiler. Which language are you using? Commented Oct 20, 2009 at 6:15
  • 2
    I'm using the C part of C++ . Isnt C++ backward compatible with C, so if I'm only using the C part, it doesn't matter g++ or gcc? Commented Oct 20, 2009 at 8:34
  • 2
    C++ isn't a quite a superset of C(a few things are missing). However, it doesn't matter for this question. Commented Apr 18, 2015 at 22:33

1 Answer 1

47

BYTE isn't a part of the C language or C standard library so it is totally system dependent on whether it is defined after including just the standard stdio.h header file.

On many systems that do define a BYTE macro, it is often an unsigned char. Converting from a const char* to an unsigned char* would require an explicit cast.

Sign up to request clarification or add additional context in comments.

4 Comments

Yep - there's your answer idazwaika - custom datatype definitions can cause trouble.
You may also want to note that the C standard allows the compiler to make char to be either signed char or unsigned char which can lead to some unexpected results.
Thanks. Looks like I have to hunt for the definition of BYTE macro. Is there a way I can get the output of C pre-processor where macros are translated already?
idazuwaika: Use gcc -E (Preprocess only; do not compile, assemble or link)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.