Array of Strings??

  • Thread starter Simon Mansfield
  • Start date
S

Simon Mansfield

Hey everyone, I have created a struct called employee, as follows:

struct employee {
int pid;
char name[3];
char job[40];
int hourlyRate;
struct employee *next;
}

Which will become a linked list (obviously!).. Now I want to add one more
field, a "appointment" field... Now the idea is that when a customer wants
to book the particular employee they put their name in the corresponding
array slot. ( eg. Ms. Saunders wants 10 am should be appointments [10] =
"Ms. Saunders" ) however I have no idea of how to create an array of
strings.. or even if it's possible.. Bit of a noob, i know! (But u gotta
start at some point!!)

Any other suggestions will be welcomed and the entired project can be sent
via e-mail upon request if you wish to have a closer look.. :)

Simon
 
K

karl malbrain

I have no idea of how to create an array of
strings.. or even if it's possible

An array of strings is created the same way an array of anything else is
created: you start with your BASE element and extend an array.

For example, a BASE element, and an array of 5 integers:

int ele;
int group[5];

Here ele is the name of a BASE, and group is the name of an array of
elements.

Character strings are no different. Your BASE element is declared as:
char *string;

karl m
 
T

Tatu Portin

Simon said:
"Ms. Saunders" ) however I have no idea of how to create an array of
strings.. or even if it's possible.. Bit of a noob, i know! (But u gotta
start at some point!!)

I had this problem too.

an array of strings:

char **array;
int nstr; /* Number of strings */
int wstr; /* String width */

nstr = 8;
/* Allocate memory for the array of pointers
* (which point to the strings) */
array = (char **) calloc (nstr, sizeof (char *));

/* Memory is statically allocated for string */
array[0] = "text";

/* Memory is dynamically allocated for string */
wstr = 80;
array[2] = (char *) calloc (wstr, sizeof (char));

/* Reads a string from stdin */
fgetc (array[2], wstr, stdin);
 
C

code_wrong

Simon Mansfield said:
Hey everyone, I have created a struct called employee, as follows:

struct employee {
int pid;
char name[3];
char job[40];
int hourlyRate;
struct employee *next;
}

Which will become a linked list (obviously!).. Now I want to add one more
field, a "appointment" field... Now the idea is that when a customer wants
to book the particular employee they put their name in the corresponding
array slot. ( eg. Ms. Saunders wants 10 am should be appointments [10] =
"Ms. Saunders" ) however I have no idea of how to create an array of
strings.. or even if it's possible.. Bit of a noob, i know! (But u gotta
start at some point!!)

Any other suggestions will be welcomed and the entired project can be sent
via e-mail upon request if you wish to have a closer look.. :)

How about

struct employee {
int pid;
char name[3];
char job[40];
int hourlyRate;

char appointment[12][40]; /*12 appointments with room for 39 characters
in each string*/

struct employee *next;
}
 
S

Simon Mansfield

Thanks a lot everyone... I think i'm going to try to attempt something along
the lines of what code_wrong suggested, since it seems to be the simplest
solution and I am trying to keep things simple :)

Thanks all.. I'll let you know if I have any further problems.

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top