-1

I'm reviewing C-code which contains the line,

volatile uart_regs_t* const regs = (uart_regs_t*) cntl->addr 

I'm not familiar with syntax that places a '*' adjacent and to the right of a struct alias (uart_regs_t). Someone explain the pieces of this statement?

R Sahu
  • 204,454
  • 14
  • 159
  • 270
PMG
  • 33
  • 5
  • `(uart_regs_t*)` is a cast, it tells the compiler to treat `cntl->addr` as a pointer of `uart_regs_t`. – Pablo Mar 11 '18 at 03:56
  • [Someone close as dupe of canonical type casting question please](https://stackoverflow.com/questions/7558837/what-exactly-is-a-type-cast-in-c-c). – user202729 Mar 11 '18 at 04:13
  • I found the answer I was looking for in another post: – PMG Mar 11 '18 at 19:58

2 Answers2

2

I'm not familiar with syntax that places a '*' adjacent and to the right of a struct alias (uart_regs_t). Someone explain the pieces of this statement?

This syntax is explicit casting. It is used when the type of an expression is different than the type of the variable to be assigned to.

Type of regs is volatile uart_regs_t* const. Unless the type of cntl->addr can be implicitly converted to that type, you'll have to use the explicit cast.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

I found the answer in another post,

Is there a convention for pointer declarations in C?

The answer is these three syntaxes are the same, and are all type declarations:

int*  ptr;
int  *ptr;
int * pt;
PMG
  • 33
  • 5