1

I have this simple C code, but including the reference variable input parameter in the write and read functions makes the program not compile. If I remove &, the program is fine. I am running this on Microsoft Visual Studio 2017.

#include <stdio.h> 
#include <stdint.h>


typedef struct Cr Cr;
typedef struct Modulation_format_cnfg Modulation_format_cnfg;

struct Modulation_format_cnfg {
    union {
        struct
        {
            uint32_t sc0 : 3;
            uint32_t : 5;
            uint32_t sc1 : 3;
            uint32_t : 5;
            uint32_t sc2 : 3;
            uint32_t : 5;
            uint32_t sc3 : 3;
            uint32_t : 5;
        };
        uint32_t raw;
    };
};

struct Cr
{
    uint32_t const kBlock_base_addr;

    Modulation_format_cnfg volatile modulation_format_cnfg;
};


void write(volatile Modulation_format_cnfg& r, const int val) {
    r.raw = val;
}

uint32_t read(Modulation_format_cnfg volatile& r, const int val) {
    return r.raw;
}

Cr cr[2];

Can somebody please help?

Thanks in advance!

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
S. Scorpion
  • 121
  • 7
  • 2
    C does not have built-in references; `Modulation_format_cnfg volatile& r` is not a valid parameter declaration in C. – Eric Postpischil Apr 21 '19 at 20:39
  • `&`, used when declaring variables/parameters to mean "reference to" is a C++ thing, not a C thing. In C you must use `*` to mean "address of" instead, but there are, if I'm not mistaken, some differences. – Lasse V. Karlsen Apr 21 '19 at 22:25

1 Answers1

3

This (volatile Modulation_format_cnfg& r) is at least one of the problems. In c, you must pass by address (not directly pass by reference as you can do in c++). To do this, maybe change that line to volatile Modulation_format_cnfg* r, pass it a pointer (either &someStackVar or a malloced heap memory), then use the arrow operator in your function.

Mini
  • 445
  • 5
  • 17