4

I got the same values by replacing the line marked with (1) in my actual code with:

Date *ptrdate = malloc(12 * sizeof(*ptrdate));

Question: Which one is better and why?

Here is my actual code:

typedef struct {
    int day;
    int mo;
} Date;

void main(){
    Date *ptrdate = malloc(12 * sizeof(Date)); //(1)

    ptrdate[0].day=26;
    ptrdate[0].mo=5;
    printf("Date:%d/%d\n", ptrdate[0].day, ptrdate[0].mo);
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
LED Fantom
  • 1,229
  • 1
  • 12
  • 32

2 Answers2

7

Writing your code as

Date *ptrdate = malloc(12 * sizeof(*ptrdate));

or, a cleaner approach

Date *ptrdate = malloc(12 * sizeof *ptrdate);  //sizeof is operator, no need for extra "()"

is more acceptable and desirable, as it makes code more robust. Even if

  • the type of ptrdate gets changed in future
  • using the code along with any external library which has a seperatetypedefed Date (creating a conflict)[#]

you don't need to change this part(s) of code.

Also, the recommended signature of main() is int main(void).


[#]Thanks to Mr. @Elias Van Ootegem for the comment below]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • sorry @iharob, was editing at the same time. However, i've incorporated your changes. Thanks. :-) – Sourav Ghosh Apr 23 '15 at 11:10
  • Would you mind explaining how the recommended signature should be int main(void) as opposed to int main(int argc, char *argv[])? – Corb3nik Apr 23 '15 at 11:13
  • Also, `sizeof` is not a function so drop the pointless parentheses when possible. `Date *ptrdate = malloc(12 * sizeof *ptrdate);`. – unwind Apr 23 '15 at 11:13
  • 3
    @Cubia `int main(int argc, char *argv[])` can be used when you intend to make use of command line arguments. If you don't need them , `int main(void)` will do fine. Refer chapter `5.1.2.2.1`, `C11` standard. – Sourav Ghosh Apr 23 '15 at 11:14
  • @unwind Thanks for the suggestion sir. Updated accordingly. :-) – Sourav Ghosh Apr 23 '15 at 11:19
  • @SouravGhosh: Next to the actual variable changing type, name conflicts requiring the `typedef` to change is another reason to prefer `sizeof *ptr` over `sizeof(Type)` cf my answer – Elias Van Ootegem Apr 23 '15 at 11:43
  • @EliasVanOotegem Very right sir. I'll add a line to my answer to reflect your comment, with your permission. :-) – Sourav Ghosh Apr 23 '15 at 14:04
1

This is more the matter of taste/style. I'd prefer sizeof(Date) as this seems a little more readable to me. But just do as you like - no real difference here.

Matt
  • 13,674
  • 1
  • 18
  • 27
  • `sizeof(type)` might put you at risk of having to refactor all code that allocates memory for your struct if you decide to change the `typedef` statement (because of a name conflict with some third party dependency, for example). That's why I think most people would prefer `sizeof *ptr_var`, that and not having as many brackets – Elias Van Ootegem Apr 23 '15 at 11:23
  • @EliasVanOotegem Well, it's not an everyday issue. And one still needs search&replace if run into it. So the risks seem to be overestimated. – Matt Apr 23 '15 at 11:28
  • @EliasVanOotegem BTW. I like brackets :-) – Matt Apr 23 '15 at 11:30
  • It's not an everyday scenario when working on _existing code_, agreed. When developing something new, it does happen (at least, when I'm doing the developing it has happened). BTW: Brackets are OK, but I don't want my C code to look like lisp :-P – Elias Van Ootegem Apr 23 '15 at 11:36