Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with string fields in a C struct?

Tags:

I'm having trouble making a database based on a singly-linked list in C, not because of the linked list concept but rather the string fields in the struct themselves.

This is an assignment in C and as far as I know (I'm a newbie), C doesn't recognize 'string' as a data type.

This is what my struct code looks like:

typedef struct  {   int number;   string name;   string address;   string birthdate;   char gender; } patient;  typedef struct llist {   patient num;   struct llist *next; } list; 

I was thinking of making a struct for the strings themselves so that I can use them in the struct, like this:

typedef struct string  {    char *text; } *string; 

Then I will malloc() each one of them when it is required to make new data of the string type (array of char).

typedef struct string {   char *text; } *string;  int main() {     int length = 50;     string s = (string) malloc(sizeof string);     s->text = (char *) malloc(len * sizeof char);     strcpy(s->text, patient.name->text); } 

Can someone help me figure this out?
Thank you.

like image 526
fleuracia Avatar asked Apr 15 '12 12:04

fleuracia


People also ask

Can we use string in structure in C?

Unlike arrays, we do not need to print a string, character by character. The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to directly print and read strings.

Can we use string in struct?

The answer is yes unless you are using an obsolete compiler that does not support initialization of structures with string class members.

What is a typedef struct?

The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g.,​ int) and user-defined​ (e.g struct) data types. Remember, this keyword adds a new name for some existing data type but does not create a new type.


1 Answers

On strings and memory allocation:

A string in C is just a sequence of chars, so you can use char * or a char array wherever you want to use a string data type:

typedef struct     {   int number;   char *name;   char *address;   char *birthdate;   char gender; } patient; 

Then you need to allocate memory for the structure itself, and for each of the strings:

patient *createPatient(int number, char *name,    char *addr, char *bd, char sex) {    // Allocate memory for the pointers themselves and other elements   // in the struct.   patient *p = malloc(sizeof(struct patient));    p->number = number; // Scalars (int, char, etc) can simply be copied    // Must allocate memory for contents of pointers.  Here, strdup()   // creates a new copy of name.  Another option:   // p->name = malloc(strlen(name)+1);   // strcpy(p->name, name);   p->name = strdup(name);   p->address = strdup(addr);   p->birthdate = strdup(bd);   p->gender = sex;   return p; } 

If you'll only need a few patients, you can avoid the memory management at the expense of allocating more memory than you really need:

typedef struct     {   int number;   char name[50];       // Declaring an array will allocate the specified   char address[200];   // amount of memory when the struct is created,   char birthdate[50];  // but pre-determines the max length and may   char gender;         // allocate more than you need. } patient; 

On linked lists:

In general, the purpose of a linked list is to prove quick access to an ordered collection of elements. If your llist contains an element called num (which presumably contains the patient number), you need an additional data structure to hold the actual patients themselves, and you'll need to look up the patient number every time.

Instead, if you declare

typedef struct llist {   patient *p;   struct llist *next; } list; 

then each element contains a direct pointer to a patient structure, and you can access the data like this:

patient *getPatient(list *patients, int num) {   list *l = patients;   while (l != NULL) {     if (l->p->num == num) {       return l->p;     }     l = l->next;   }   return NULL; } 
like image 171
Adam Liss Avatar answered Sep 18 '22 19:09

Adam Liss