NXC: syntax for passing a struct to a function?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: syntax for passing a struct to a function?

Post by HaWe »

hi,
I try to pass a structure to a function but I don't succeed- what is the right NXC syntax? (edit: the code is an excerpt from an original C file)

Code: Select all


void DBG () {
  return;
}

/*                  */
/* Complex Numbers. */
/* ================ */

/* complex number */
struct C {
  float re,
          im;
}  ;

/* multply real, complex */
#define C_MUL( f, z)                   \
  do {                                 \
    z.re *= f;  z.im *= f;             \
  }  while (FALSE)

/* complex product constructor */
#define C_PRO( z1, z2) {               \
   re: z1.re * z2.re - z1.im * z2.im,  \
   im: z1.re * z2.im + z1.im * z2.re   \
}

/* bit reverse permutation */
void BRP (unsigned char ldN,
          struct C  vec []) /* 2^ldN */   // <<========================== ERROR !
{
  unsigned int N;
  unsigned int l, n, r;
  unsigned char b;

  N  = 1 << ldN;
  for (l = 0;  N > l;  ++ l)
  {
     n = l;
     r = 0;
     for (b = 0;  ldN > b;  ++ b) {
        r <<= 1;
        if (0 !=  1 & n ) { r |= 1; }
        n >>= 1;
     }  // b
     if (l < r) {
        struct C     const z = vec [l];
        vec [l] = vec [r];
        vec [r] = z;
     }  // right
  }  // k
  return;
}  // BRP
      

task main() {
      
}
# Error: Unexpected character encountered
File "c:\Temp\temp.nxc" ; line 30
# struct C v
#----------------------------------------------------------
# Error: Variable name expected
File "c:\Temp\temp.nxc" ; line 30
# struct C v
#----------------------------------------------------------
# Error: Unknown datatype
File "c:\Temp\temp.nxc" ; line 30
# struct C v
#----------------------------------------------------------
# Error: Variable name expected
File "c:\Temp\temp.nxc" ; line 32
# unsigned int const N
#----------------------------------------------------------
# Error: ';' expected
File "c:\Temp\temp.nxc" ; line 32
# unsigned int const N =
#----------------------------------------------------------
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: syntax for passing a struct to a function?

Post by spillerrec »

You should only use "struct" at the declaration, just use "C" at the other places. The syntax by using "struct" each time it is needed actually looks weird to me but looking it up it really is the way it is supposed to be done in C... Did they change it in C++, or have I just always used classes instead?
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: syntax for passing a struct to a function?

Post by HaWe »

I'm actually not sure if it's C or C++ code -
I just picked up the code while searching for implementations of a DFT(FFT) and now I despair of the error messages trying to use that code for NXC... ;)

edit: got the complex calculations working now, thx! :)

Code: Select all

    struct C {
      float re;
      float im;
    } ;

    // complex scalar product
    inline void  C_MUL(float f, C z, C &rz) {
      rz.re = z.re * f;
      rz.im = z.im * f;
    }

    // complex product
    inline void  C_PRO(C z1, C z2, C &rz)  {
       rz.re = z1.re * z2.re - z1.im * z2.im;
       rz.im = z1.re * z2.im + z1.im * z2.re;
    }
ps
Now I finally understood what this actually should mean in the original code:
pass an ARRAY (!) of struct to the function!

Code: Select all

void BRP (unsigned char ldN,
          C  vec [] ) // a vector of complex numbers = an array of 2-dim structs!
{...
bullestock
Posts: 27
Joined: 29 Sep 2010, 19:34
Location: Denmark
Contact:

Re: NXC: syntax for passing a struct to a function?

Post by bullestock »

spillerrec wrote:You should only use "struct" at the declaration, just use "C" at the other places. The syntax by using "struct" each time it is needed actually looks weird to me but looking it up it really is the way it is supposed to be done in C... Did they change it in C++, or have I just always used classes instead?
Yes, in Standard C a type defined with 'struct' must always be referred to as 'struct X'. This is why you often see code such as

Code: Select all

typedef struct
{
   ...
} X;
- in that way you can just say 'X x;' instead of 'struct X x;'.

In C++, types defined with class and struct can be referred to with or without 'class' and 'struct'.

I have no idea as to how NXC handles this, though.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest