954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

array problems

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:

char * tableArray[10];


should that be:

char * tableArray[10][10];?


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

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?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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[][][];

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
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?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

damn, double-post! see below.

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 
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,

struct MyStruct
{
    std::string foreame;
    std::string surname;
    char middleInitial;
    int age;
}

int main()
{
    MyStruct MyPerson;
    MyPerson.forename = "Fred";
    MyPerson.surname = "Bloggs";
    MyPerson.middleInitial = 'E';
    MyPerson.age = 36;
    // etc.
}


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,

MyStruct PeopleDatabase[50];
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.

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You