Search code examples
cvariables

What is a Scalar Variable in C?


I was reading 'Pointers on C' by Kenneth Reek and saw this line:

A  structure  variable  is  a  scalar,  so  you  can  perform  the  same  kinds  of operations  with  it  that  you  can  with  other  scalars.

So what does it mean?

I found a similar question on SO but it was related to some other language (I guess SQL)

Thank you.


Solution

  • Section 6.2.5 of the C11 standard explains:

    Arithmetic types and pointer types are collectively called scalar types. Array and structure types are collectively called aggregate types.

    Continuing throughout the standard you find what operations and assignments are available for scalar and aggregate types.

    Your statement:

    A structure variable is a scalar, so you can perform the same kinds of operations with it that you can with other scalars.

    Directly contradicts with the C-standard as structures are aggregate types not scalar. That said, there are limited cases where structures do possess the same properties as scalars. For example, you can assign two of the same type structures and the copy-constructor provides a shallow copy (assignment) between the two structs. There are other circumstances as well, but note they are the exception and not the rule.

    I suspect the statement is made regarding one of those circumstances where a struct can be treated as a scalar for that particular operation. Without knowing what operation is being discussed, I cannot say further.