944,010 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1889
  • C++ RSS
Feb 21st, 2006
0

array problems

Expand Post »
Hello,

I've got a 2 diemsional array that ments to represent this:

Name Address
A 1
B 2
C 3

I've got the following code written for this:

C++ Syntax (Toggle Plain Text)
  1. char * tableArray[10];

should that be:
C++ Syntax (Toggle Plain Text)
  1. char * tableArray[10][10];?

And I've got a pointer that populates only the name:

C++ Syntax (Toggle Plain Text)
  1. char * ptr = tableArray[sti];
But this doesnt work and not sure why.

My overall question is :

*Have I delcare an array that takes a stream of chars properly?
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Feb 21st, 2006
0

Re: array problems

A table of strings with more than one string per row would be a three dimensional array of char. Say the table contained one string for the name and one string for the address per row of the table:

Name Address
John Milwaukee
Jane Miami

Depending on your needs, such a table could be declared using any of the following:
char ***names;
char **names[];
char *names[][];
char names[][][];
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Feb 21st, 2006
0

Re: array problems

fair enough I'm going to use a link list since the idea of a 3 dimensional array is over complex for such a task
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Feb 22nd, 2006
0

Re: array problems

Linked lists and arrays each have their benefits and detriments, depending on the task. With a linked list you will need to create a programmer declared container type to hold the information stored in the list. This can often be accomplished by using a struct/class that contains both the information to be stored and the location/address of the next link/node in the list. You could also use an array of programmer declared type rather than a linked list of programmer declared type so you would only need a single dimensional array rather than a three dimensional array.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Feb 23rd, 2006
0

Re: array problems

Quote originally posted by Lerner ...
You could also use an array of programmer declared type rather than a linked list of programmer declared type so you would only need a single dimensional array rather than a three dimensional array.
I like that but never done it , could you explain in detail?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Feb 23rd, 2006
0

Re: array problems

damn, double-post! see below.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Feb 23rd, 2006
0

Re: array problems

Quote originally posted by Acidburn ...
I like that but never done it , could you explain in detail?
a User Defined Type (UDT for short) would be either a struct, union, class or enum.. Stay away from unions unless you have any good reason to use one, and enums are for numerical values, which leaves you with struct and class (Note - class is C++ only).

in C++, struct and class are both actually the same thing... well, almost.. they have one minor difference which isn't important at the moment.

a struct (the name originally came from 'data structure') is conceptually very similar to a table layout in an MS Access database - in fact, I suspect that most databases use the term "data structure" too.

At the basic level, it will typically contain one or more "fields" (called member variables), and every record (object) created has its own value for each of these fields.

eg,

C++ Syntax (Toggle Plain Text)
  1. struct MyStruct
  2. {
  3. std::string foreame;
  4. std::string surname;
  5. char middleInitial;
  6. int age;
  7. }
  8.  
  9. int main()
  10. {
  11. MyStruct MyPerson;
  12. MyPerson.forename = "Fred";
  13. MyPerson.surname = "Bloggs";
  14. MyPerson.middleInitial = 'E';
  15. MyPerson.age = 36;
  16. // etc.
  17. }

You can make an array of UDTs in exactly the same way as you'd create an array of int or any other built-in type. ie,
C++ Syntax (Toggle Plain Text)
  1. MyStruct PeopleDatabase[50];
  2. PeopleDatabase[0].forename = "Joseph";

Note - above is C++ code, C uses a slightly different syntax for creating an object of MyStruct. If you are using C++, then this is probably your first step into writing Object-orientated code.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: printing meter yards feet
Next Thread in C++ Forum Timeline: Templates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC