[SOLVED] initializing a multidimensional array of strings
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
To quote Bjarne Stroustrup, "The better one knows C, the harder it seems to avoid writing C++ in C style." Definitions in the OpenGL Programming Guide (the Red Book) C: God's programming language. C++: The programming language of an alien deity. My own spin is that for a project written by a single programmer, C is easier. For a project written by multiple programmers in parallel, C++ is almost mandatory. However, that is a topic for a different thread. Relevant to this thread, does .size return something other than 8? If not, then why not?
Quote:
Originally Posted by rtmistler
I don't know the exact code syntax, but C++ is eminently better qualified to manage lists and arrays; similarly is python. For instance, just me tossing it out having not checked, you have a list declared, you also have methods for it such as .size() or .length(), I believe it would be .size() where that will give you an integer representation of the size of the list. Further, the greater part of C++ is that things like String types or something similar; classes actually; allow you to declare something as a String class or list of String classes and not worry about pre-declaring the dimensions. So you might want to keep what you do have, but look a bit at some of the available C++ classes you could use.
And to note further, gcc on a general Linux system should be able to deal with C++ code. Not sure if you're working on that type of platform, but just pointing that detail out.
If I ask for sizeof(subtopic_3), it returns 16. If I ask for (sizeof(subtopic_3) / sizeof(char *)), I get the number of elements in the array.
Since sizeof(subtopic[i]) returns 8 for all [i], apparently it does not recognize that subtopic[i] is an array.
Quote:
Originally Posted by ntubski
sizeof returns size in bytes, your size[] array is counting the number of elements. subtopic is an array of pointers, all pointers are 8 bytes (or 4 bytes on a 32 bit system).
apparently it does not recognize that subtopic[i] is an array.
That's because subtopic[i] is not an array. Consider that subtopic_0 is not the same size as subtopic_1, they have different size (and therefore different types). An array's elements must be of the same type (and therefore same size). To get around this restriction you made subtopic into an array of pointers; it doesn't contain arrays.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.