Solved

Comma in Sting Array

Posted on 2001-06-26
10
427 Views
Last Modified: 2012-08-13
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
Comment
Question by:prashant_n_mhatre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:anup_s
ID: 6228470
I think this means, there are 3 data string pointers in the above array, with the third one set to null.

- Anup
0
 

Expert Comment

by:wkmatt42
ID: 6228687
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
 
LVL 32

Expert Comment

by:jhance
ID: 6228779
This:

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

and this:

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

are 100% equivalent and the compiler ignores the trailing ",".
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6228935
jhance,

The question is "Why does compiler ignore the comma?"
0
 
LVL 32

Expert Comment

by:jhance
ID: 6228965
Simple:

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

0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6228972
But there must be some reason for this...
0
 
LVL 32

Expert Comment

by:jhance
ID: 6228994
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
 

Accepted Solution

by:
coderat earned 50 total points
ID: 6229018
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
 

Expert Comment

by:wkmatt42
ID: 6229060
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6231649
Thanks all...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question