Subj : Re: strange string pointer to strings...? To : borland.public.cpp.borlandcpp From : maeder Date : Thu Sep 29 2005 09:36 pm "mujeebrm" writes: > i am not in a school. I am learning to program as a hobby thru > c,c++. this is not a school assignment. Please note that there is no programming language called "C,C++". C and C++ have become quite distinct languages, each with its own idioms, best (and worst) practices etc. Others may disagree here, but I think that you should either learn C or C++, but not both at the same time. Whichever you pick, make sure to have a look at the book reviews at http://www.accu.org/ . > this program is taken from example code given in a book which i > could not understand and sought U peoples help in deciphering this > code. Good. No program is bad enough to not work as a bad example. :-) > #include > #include > #include Since you are learning, please note that (and the getch() function probably declared in it) aren't part of the C nor the C++ ISO Standard. I personally think that you would do better with a book that sticks to the language standard. > int main(void) > { > > /* this does not make sense to me that when i keep adding rows and columns > of strings to this array of pointers to strings it just keeps > going on, any explanation for this ? > */ > char *s[][5]= { > "one","qwer", > "two", "rewq", > "three", "qwer", > "four", "rewq", > "five", "qwertttt", > " ", " " > }; Yesterday, it didn't make sense to me either :-) My remark about 5 being too small was wrong. It's a complex statement, defining a complex data structure and initializing it in a rather obfuscated way. First, you have to learn how to read declarators. The right way to read them is to start at the name being declared (which isn't always as obvious as here) and read outward, following operator precedence. In this case, this means: s is defined to be an array of a size determined by the initializer which is part of the definition. The elements of s are arrays of 5 pointers to char. The initializer is the part of the definition surrounded by curly braces. In this particular case, s[0][0] is initialized with "one", s[0][1] with "qwer" etc. s[0][4] is initialized with "three", s[1][0] with "qwer" and so on. Since there are twelve string literals in the initializer, the second " " initializes s[2][1]. s has thus 2 elements, and the remaining elements of s[2] are initialized to be null pointers. Please do look up declarators and initializers in a good text book. This part of the C syntax is generally considered an experiment that failed. And let me repeat that it's a really bad idea to have a pointer to non-const (such as s[0][0] point at a string literal). The type of s should be char const *s[2][5] > char **sp=0,w[10]; > int c; Having read the above-mentioned textbook chapters, what do these statements mean? Going on doesn't make any sense before you can answer this question. .