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?

Recommended Answers

All 6 Replies

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

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

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.

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?

damn, double-post! see below.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.