c++ - What is the significance of comma in array and structure initialization? -
this question has answer here:
while browsing through codes, came across method of initialization:
#include<stdio.h> struct trial{ int x, y; }; int main(){ int a[10] = {0,1, };//comma here struct trial z = {1, };//comma here return 0; }
what significance of comma operator? not find difference in method of initialization if comma operator removed.
it makes sense if generate such code scripts. keeps script simple. no edge-cases. in particular, don't bother whether need add ,
first, before writing 1 more item; write 1 item followed comma , you're done!
you don't care first item or last item. items same if there trailing comma.
think code-generation point of view. start making sense.
see python script generates such code:
print ("int a[] = {") item in items: print (item + ",") print ("};")
it simple. try writing code without trailing comma. wouldn't that simple.
the standard allows trailing-comma in enum definition:
enum { x, y, z, //last item : comman okay };
hope helps.
Comments
Post a Comment