What is a Scalar Variable in C?

﹥>﹥吖頭↗ 提交于 2020-06-10 06:27:30

问题


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.


回答1:


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.




回答2:


In this context scalar variable is a variable that holds only one value at a time.



来源:https://stackoverflow.com/questions/41788669/what-is-a-scalar-variable-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!