array problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

array problems

 
0
  #1
Feb 21st, 2006
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:

  1. char * tableArray[10];

should that be:
  1. char * tableArray[10][10];?

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

  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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: array problems

 
0
  #2
Feb 21st, 2006
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[][][];
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: array problems

 
0
  #3
Feb 21st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: array problems

 
0
  #4
Feb 22nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: array problems

 
0
  #5
Feb 23rd, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 498
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 50
Bench's Avatar
Bench Bench is online now Online
Posting Pro in Training

Re: array problems

 
0
  #6
Feb 23rd, 2006
damn, double-post! see below.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 498
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 50
Bench's Avatar
Bench Bench is online now Online
Posting Pro in Training

Re: array problems

 
0
  #7
Feb 23rd, 2006
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,

  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,
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC