4
#include <stdio.h>
#include <stdlib.h>

typedef struct StupidAssignment{
    long length;
    char* destination_ip;
    char* destination_port;
    long timestamp;
    long uid;
    char* message;
}packet;

void main(){
    int number_of_packets=10;int i;
    packet* all_packets[number_of_packets];
    for(i=0;i<number_of_packets;i+=1)all_packets[i]=malloc(sizeof packet);
}

The above snippet does not compile with the following error:-

reciever.c: In function ‘main’:
reciever.c:16:64: error: expected expression before ‘packet’
  for(i=0;i<number_of_packets;i+=1)all_packets[i]=malloc(sizeof packet);

However, the following code does compile:-

#include <stdio.h>
#include <stdlib.h>

typedef struct StupidAssignment{
    long length;
    char* destination_ip;
    char* destination_port;
    long timestamp;
    long uid;
    char* message;
}packet;

void main(){
    int number_of_packets=10;int i;
    packet* all_packets[number_of_packets];
    for(i=0;i<number_of_packets;i+=1)all_packets[i]=malloc(sizeof(packet));
}

The only difference being sizeof(packet) and sizeof packet.

On a previous answer, I came to learn that sizeof is just an operator like return so the parenthesis was optional.

I obviously missed something so can someone explain this behaviour to me?

asds_asds
  • 990
  • 1
  • 10
  • 19

3 Answers3

6

When using the sizeof operator with a type, you must place the type in parentheses.

When using the sizeof operator with a variable, you may omit the parentheses.


See §6.5.3 Unary operators and §6.5.3.4 The sizeof and _Alignof operators in the C11 draft. Credit to @JonathanLeffler for identifying the sections.

rtx13
  • 2,580
  • 1
  • 6
  • 22
  • 7
    But since parentheses always work and omitting parentheses sometimes doesn't work, it is reasonable to always use parentheses and then you don't need to know about the special cases. Not everyone agrees with this viewpoint, but I've held it for 30+ years and not had any difficulties with it as a result — except from those few who've argued (vehemently) that you should omit the parentheses when you can. – Jonathan Leffler Mar 31 '20 at 16:37
  • 2
    You could link to C11 [§6.5.3 Unary operators](http://port70.net/~nsz/c/c11/n1570.html#6.5.3) and [§6.5.3.4 The sizeof and _Alignof operators](http://port70.net/~nsz/c/c11/n1570.html#6.5.3.4). – Jonathan Leffler Mar 31 '20 at 16:40
  • @JonathanLeffler I agree it's safest to always use parentheses. In my experience the perceived need for parentheses is so prevalent that I have come across many C developers who are surprised to learn that parentheses are not required when `sizeof` is used with a variable. – rtx13 Mar 31 '20 at 16:41
  • @JonathanLeffler I think the links provided may point to sites showing copyrighted material without permission... ? – rtx13 Mar 31 '20 at 16:43
  • I provide links via that site quite a lot. That's not an opinion on copyright — it's an observation of fact. – Jonathan Leffler Mar 31 '20 at 16:45
  • 3
    Yeah.. I've never understood developers who continually check precedence tables to try and write complex expressions correctly when they could just shove in brackets, redundant or not. Same with sizeof() – Martin James Mar 31 '20 at 16:45
  • This looks like an official [draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf). Same section numbers. – rtx13 Mar 31 '20 at 16:45
  • The HTML is derived from the PDF — look at the URL of the HTML. But you can link to the paragraphs in the HTML; you can't in the PDF. – Jonathan Leffler Mar 31 '20 at 16:52
  • @JonathanLeffler yes you are right; the PDFs show these are draft standards which should be freely accessible. I will update the links in the answer. Thanks. – rtx13 Mar 31 '20 at 16:57
  • 1
    If #1 C expert tells what I always repeat, I can finally stop feeling guilty about it. I knew about `sizeof var` not needing parenthesis, I had no idea it did not applied to types because I always used that everywhere. I agree with you, @MartinJames, and I add another statement: modern editors have a wonderful feature that doubleclicking on a parenthesis automatically show their scope. This also remove the weak _readability reason_ to people not using parenthesis. For complex expressions is just a suicide. _Parenthesis are free!_ is my slogan. – Roberto Caboni Mar 31 '20 at 17:02
3

sizeof is an operator. It just uses parenthesis to differentiate between data types and variables.

sizeof packet; //Error
sizeof(packet); //Compiles

packet p;
sizeof p; //No error
Abhay Aravinda
  • 878
  • 6
  • 17
1

According to the documentation:

sizeof( type )
sizeof expression

So, types need parentheses, expressions do not.

Azeem
  • 11,148
  • 4
  • 27
  • 40