Skip to content
  • 0 Votes
    9 Posts
    206 Views
    JonBJ

    @SimonSchroeder said in Creating C language bindings for Qt - Where do I start?:

    using just a plain void* instead of introducing a C struct

    @Platinum-Lucario should read and be aware of the discussion we had recently in QGraphicsTextItem * and void * conversion error and the conclusion at https://forum.qt.io/post/815140. The gist is that where Qt uses C++ multiple inheritance you lose information and go wrong if you are not careful about what you pass as a C void *.

  • 0 Votes
    1 Posts
    923 Views
    No one has replied
  • How to use fprintf?

    Unsolved General and Desktop
    9
    0 Votes
    9 Posts
    7k Views
    kshegunovK

    @JohanSolo

    Your first trial should have difficulties to compile, or should at least crash when running, the char and int sizes are different: your read_buff is char*, i.e. each element is 1 byte, whilst the "%d" expects an object of at least 4 bytes.

    While I agree in principle, most compilers don't care. This is C with variadic function(s) which basically means you can forget type-safety. What's happening is that fprintf if just reading memory it doesn't own (which most compilers will allow freely, i.e. it won't crash).

    @Anas_Deshmukh

    int empId1; // some integer FILE * pun; // file pointer char read_buff[] = { 0xAA, 0x2D, 0xFF }; // char array holding value as 0xAA, 0x2D, 0xFF . . . int read_buff_size = 3; //< Size of the above buffer (in elements)

    You can do it like this:

    QFile file; if (!file.open(pun, QFile::WriteOnly)) { // Can't open, error handle accordingly. } QTextStream out(&file); out << empId1; for (qint32 i = 0; i < read_buff_size; i++) out << "~0x" << QString::number(read_buff[i], 16); out << endl; file.close(); // Do not forget to close the FILE *

    Kind regards.

  • 0 Votes
    9 Posts
    4k Views
    SGaistS

    Hi @asthana-ujjwal and welcome to devnet,

    Can you show your .pro file ?