Declare Multi-Dimensional Array

This is a discussion on Declare Multi-Dimensional Array within the C Programming forums, part of the General Programming Boards category; Hi, Can somebody help me with this. How can I declare an array of this table. NUMBER DESC TEXT ------------------------------------------------- ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    1

    Declare Multi-Dimensional Array

    Hi,

    Can somebody help me with this.

    How can I declare an array of this table.

    NUMBER DESC TEXT
    -------------------------------------------------
    1 AAA BBB
    2 CCC DDD
    3 EEE FFF
    ...
    ...
    9 QQQ RRR

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It looks like you want an array of structures:
    Code:
    struct record {
      int number;
      char desc[MAX_DESC];
      char text[MAX_TEXT];
    };
    
    struct record array[MAX_RECORDS]; /* N is the number of records you have*/
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Why not char* array[3][9], and he can convert char* to int later?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why not char* array[3][9], and he can convert char* to int later?
    Because multidimensional arrays are trickier? Because there's not a definite breakdown of records and fields in a problem that clearly defines that relationship? Because you have to go to the trouble of converting a string to an integer? Because named fields make life easier? Because column major order is back-asswards in C? Should I go on?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    If I want to have fancy tables without array of arrays or array of structures, then I would rather use C++ maps. In C, I'll always have to take care about array dimensions, pointers, so each solution is far from the ideal one.
    Regards!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >then I would rather use C++ maps
    As long as we're suggesting other languages for a C problem, why not just use Perl lists and hashes?

    >In C, I'll always have to take care about array dimensions, pointers, so each solution is far from the ideal one.
    How limited your perspective is. You realize that there are other data structures than arrays, right? You realize that there are libraries that implement these other data structures, right? Then again, I get the impression that you're biased against C, so any solution using C is far from ideal in your eyes.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Because C is (almost) subset of C++, he can use maps without understanding OOP.
    No, I've never heard of other data structures and I've never seen any such library. Using C, he have to deal with pointers sooner or later, no C library will help him. As I mentioned, if you don't want pointers, then use C++ and its standard library.
    Regards!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What? There aren't pointers in C++? Can you hook me up with your dealer?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Did I say that?

  10. #10
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    wtf? no offence, but the above post makes absolutely no sense to me.

    it sounds like you think C++ is a totally different language than C. take a look at its name, think of the expression C++ in C (and in C++, that alone should tell you something), and you will find C++ is a derivitive (superset) of C. pointers in C++ are inherited from C, they are no different. the C standard library has nothing to do with pointers, they are part of the C programming language.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You implied it. You said if you don't want to use pointers, use C++. Thus, the implication is that you wouldn't be using pointers in C++ ever, or that it doesn't have them.

    I hate to tell you this, but there are most definately pointers involved in nearly every STL implementation. I'd bet money every single one uses at least one pointer in its implementation.

    Also, don't you feel it's rather pointless to tell someone to learn another language (C++) instead of learning one aspect of the language they're currently learning? Furthermore, C++ does shield you from learning pointers. Rather, it complicates the matter by introducing reference, which are bastardized pointers, that not only confuse the indirection/dereferencing issue, but function differently with what they can be assigned.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    @sl34k

    > it sounds like you think C++ is a totally different language than C.
    Yes.

    > take a look at its name, think of the expression C++ in C (and in C++, that alone should tell you something),
    So, they are in Java.

    > and you will find C++ is a derivitive (superset) of C.
    Didn't I say that C is (almost) subset of C++?

    > pointers in C++ are inherited from C, they are no different. the C standard library has nothing to do with pointers, they are part of the C programming language.
    How to deal with files, strings, without pointers?

  13. #13
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    @Quzah
    That's why you should use STL, to avoid pointers in their classical form.
    I didn't say to learn C++! I said that in C he have to deal with pointers, and if he want to avoid them (not always, but often), then he should choose C++.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Because C is (almost) subset of C++
    C and C++ are two different languages. Some of the most stupendous programming blunders have been made by people who failed to recognize that.

    >he can use maps without understanding OOP.
    I'm sorry, but you're not entirely correct here. To understand maps, he needs to understand the basics of classes. On top of that, templates, references, namespaces, operator overloading, iterators (which happen to be identical in concept to pointers), and various other minor details that aren't present in C. On top of language details, properly using container classes depends on an understanding of the performance requirements. Usually, that means knowing the requirements and knowing what potential data structures exist under the hood so that choosing the correct container is possible.

    But it gets worse! Since there are three items per record, a simple map won't do. So he will also need to look into the rest of the standard C++ library to find something like pair that will allow him to have multiple keys or multiple values for a single map entry without delving into classes.

    Sure, while he may be able to use maps without understanding such things as inheritance and polymorphism (though that weakens the usefulness of the container), he must understand a large number of concepts that the use of a template container class requires.

    >No, I've never heard of other data structures and I've never seen any such library.
    That's a shame. May I suggest some? An array isn't always the best option, and if you don't have a foundation in data structures, how can you claim to correctly use the libraries given to you in C++? Here are some important data structures:

    1) Hash table
    2) Stack (abstract)
    3) Queue (abstract)
    4) Linked List
    5) Search Tree
    6) Heap

    In terms of C++, the stack and queue container classes are wrappers around a deque by default, though you can choose the internal representation. The next revision of C++ will have a hash table, the list class provides a doubly linked list, the set and map classes typically use a red black binary search tree, and you even have algorithms that work with heaps. Even that wide variety of data structures is still only scratching the surface. If you don't study data structures, you're missing out on a huge and interesting part of programming.

    >Using C, he have to deal with pointers sooner or later, no C library will help him.
    The same goes for C++. He'll have to use pointers or pointer-like objects very quickly, so C++ will not protect him from that particular lesson.

    >As I mentioned, if you don't want pointers, then use C++ and its standard library.
    Iterators are pointer-like objects, so if you use them then you're still using pointers. The C++ container classes are pretty darn useless without iterators. Come to think of it, so are the algorithms.

    >it sounds like you think C++ is a totally different language than C.
    It is.

    >take a look at its name, think of the expression C++ in C
    Think of the fact that both C and C++ have gone along separate evolutionary paths since the 1980s.

    >and you will find C++ is a derivitive (superset) of C
    At one point that was true, but at that point C++ was called "C with Classes".

    >pointers in C++ are inherited from C, they are no different.
    I'll cut you some slack and agree. There are differences, but they're more related to the type system than actual pointers themselves.

    >That's why you should use STL, to avoid pointers in their classical form.
    That's more like it. But that's not why you should use the standard C++ library. You should use it because many useful data structures and algorithms have been standardized, debugged, and thoroughly tested. There's a certain measure of safety in using iterators rather than direct pointers, but that falls out of the more primary benefit of a generic library that everybody recognizes and will work (usually!) everywhere.
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Response for the question about data structures was irony. Of course that knowing of the some elementary data structures is necessary, thanks for the advice.

    I didn't intend to talk about C++, STL, pointers etc. I only tried to say:
    - That he has to deal with pointers and arrays in C.
    - With C++ he can learn IO stream, string, vector etc. without knowing how they are made. Later, he will read about pointers and references, C strings, iterators and will see that containers are made with pointers. (C++ Primer book is good example of this approach.)
    - C and C++ are different languages.

    I tried to say only elementary things, but the discussion has gone off-topic. We can talk about graphs, generic programming, design patterns or whatever, but no purpose to talk here, maybe in some other topic.

    Regards!
    Last edited by karas; 07-27-2006 at 08:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem within multi dimensional array
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 07:02 AM
  2. Multi Dimensional Array Compare
    By guitarist809 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2006, 05:03 PM
  3. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. how do i declare array of pointers to base clase reobject
    By icantprogram in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2002, 01:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21