8

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.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Karan Singh
  • 1,114
  • 1
  • 13
  • 30
  • Opinion based, unless the book mentions it explicitly. And if it does you should just keep on reading. – StoryTeller - Unslander Monica Jan 22 '17 at 08:08
  • 1
    "scalar" usually means a non-vector. There is a good definition in algebra. In this case the author probably means something that is not an array, a single value. – Sulthan Jan 22 '17 at 08:11
  • Kenneth Reek has a remarkable quote on his homepage: *Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. Brian Kernighan* – chqrlie Jan 22 '17 at 11:33

2 Answers2

9

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.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • The C standard sometimes misuses mathematical terms. Pointers aren't really "scalars", they are edges in graphs. You can't add a pointer to another one. – Malcolm McLean Jan 22 '17 at 10:52
  • Agreed, I don't think anything along the scalar definition line is absolutely without exception throughout the standard. – David C. Rankin Jan 22 '17 at 10:58
  • 3
    Not sure if I should ask this here, but why are questions downvoted without a reason?I cannot ask anymore questions because my previous 2 questions(this one and another onee I asked a year ago were not received well). I am a beginner and such things aren't as obvious to me as they are for the people with high rep on this site.I posted only after checking that a similar question wasnt here already. Feels bad to be curious :/ – Karan Singh Jan 22 '17 at 16:16
  • 1
    Well, that's somewhat a tough one, but do not take it personally. In my experience downvotes generally come from duplicate questions, questions that can be answered be referral to readily available references, or questions that don't show much effort on the questioners part. Then there are some downvotes you never really know what the hell they were for, you just take the 2pt. hit and carry on. Like I said, don't take it personal, it all works out in the end. – David C. Rankin Jan 24 '17 at 23:38
  • @DavidC.Rankin thanks a lot. Because of your comment I gained confidence to ask yet another question and thankfully it was received well :) – Karan Singh Jan 31 '17 at 15:34
  • 2
    Yes, we have all had it happen to us as well. It's much better than it was a year ago. I swear there used to be roving gangs of downvote ravaged tweens that has some mistaken view that downvoting others would somehow make themselves look better. Thankfully, most have grown up by now or have left to find something different. Glad I could help -- this site is really worth all the hard work you put into it, and level and depth of knowledge among the SO bunch is second to none. – David C. Rankin Feb 01 '17 at 10:58
1

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

Ananth Reddy
  • 299
  • 1
  • 5
  • 16