• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

Comma in Sting Array

What is the use of defining a string array as
char *str[]={"Good","Bye",} ;
What is the purpose of the comma after the last string?
Compiler compiles it without any warning or error.
0
prashant_n_mhatre
Asked:
prashant_n_mhatre
  • 3
  • 3
  • 2
  • +2
1 Solution
 
anup_sCommented:
I think this means, there are 3 data string pointers in the above array, with the third one set to null.

- Anup
0
 
wkmatt42Commented:
Permitting the final comma is just makes it easier to add elements or move them around. In the example above, the array of character pointers will actually only have two entries.
0
 
jhanceCommented:
This:

char *str[]={"Good","Bye",} ;

and this:

char *str[]={"Good","Bye"} ;

are 100% equivalent and the compiler ignores the trailing ",".
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
prashant_n_mhatreAuthor Commented:
jhance,

The question is "Why does compiler ignore the comma?"
0
 
jhanceCommented:
Simple:

Because the ANSI C and C++ LRM (Language Reference Manuals) say that it must.

0
 
prashant_n_mhatreAuthor Commented:
But there must be some reason for this...
0
 
jhanceCommented:
I'm not that much of a C historian but my take on this is because that's the way the original Kernighan and Ritchie C compiler worked and K&R "C" became quite widely used long before the standardization process began.

I've heard the defense that wkmat42 noted above but I don't think that would have been a good enough reason to put it in the spec.  This is undoubtedly one of those things what, if changed, would have broken a lot of existing C code and angered a lot of people.

As always in the standarization process, it's a lot of give and take and compromise between the early pioneers, the academic purists, and the commercial interests.
0
 
coderatCommented:
i think it was provided so for the benefit of the programmer. something like a coding guideline.

if i dont declare
 { "first","second"  no comma here}  then
later on if i add another entry
 "third" , and by mistake forget a comma

 then char * str[]= { "first", "second" "third" }
  the above is equivalent to
 char * str[]= { "first", "secondthird" }
and u know why.

  so as a good programing practice,put comma after
the last entry, and c++ ignores the last comma
to allow good programming habits.
0
 
wkmatt42Commented:
Not all compilers have always supported the extraneous comma. I can vaguely recall a time when compilers didn't support the use of an extraneous comma. When Java was released, I remember thinking it was really cool that it was part of its standards and thought we ought to have it too.

Of course it may have already been a part of the standard, but by then I had been brainwashed by all the compiler errors that told me to stop screwing it up. Even the compiler we were using on our AIX boxes was, until recently, old enough to lack support for it. But that's an even longer and more painful story.
0
 
prashant_n_mhatreAuthor Commented:
Thanks all...
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now