Data Structure

Course- C IN LINUX >

Sometimes we wish to manage a set of variable as a group, perhaps taking all the values from a database record and passing the whole record around our program to process it. To do this we can group data into structures.

his program uses a struct to deine a set of properties for something called a player. he main function contains a declaration and instantiation of an array of 5 players. We pass a pointer to each array member in turn to a function to rank each one. his uses a switch statement to examine the irst letter of each player name to make an arbitrary ranking. hen we pass a pointer to each array member in turn to a function that prints out the details.

#include <stdio.h>
#include <string.h>

struct player

{
	char name[255];
	char role[255];
	int ranking;

};

void rank(struct player *p)
{

	switch(p->name[0])
	{
		case 'P': p->ranking=4;break; 
		case 'H': p->ranking=1;break;
		case 'R': p->ranking=2;break;
		case 'J': p->ranking=5;break;
		case 'B': p->ranking=3;break;
	}


}


void show(struck player p)
{
printf("Name:%s Role:%s Ranking;%d<br>\n", p.name,p.role,p.ranking);
}

int main(int argc, char *argv[], char *env[])
{
	struct player myteam[5]={{"Pele","striker",0),
		{"Bechkham", "male model", 0},
		{"Roddick", "tennis man", 0},
		{"Haskins", "swimmer", 0},
		{"Jagger", "singer", 0}},

int i=0;
printf("Content-type:text/html\n\n");
for(i=0;i<5;i++) rank(&myteam[i]);
for(i=0;i<5;i++) rank(myteam[i]);
return 0;
}
	
                  
                  

he results are shown here, as usual in a browser: his

C LINUX DATA STRUCTURES

This is a very powerful technique that is quite advanced but you will need to be aware of it. he idea of structures leads directly to the idea of classes and objects.

We can see that using a struct greatly simpliies the business task of passing the data elements around the program to have diferent work done. If we make a change to the deinition of the struct it will still work and we simply have to add code to handle new properties rather than having to change the argument lists or signatures of the functions doing the work.

The deinition of the structure does not actually create any data, but just sets out the formal shape of what we can instantiate. In the main function we can express this instantiation in the form shown creating a list of sequences of data elements that conform to the deinition we have made.

You can probably see that a struct with additional functions or methods is essentially what a class is in Java, and this is also the case in C++. Object Oriented languages start here and in fact many early systems described as “object oriented” were in fact just built using C language structs.

If you take a look for example, at the Apache server development header iles you will see a lot of structs for example in this fragment of httpd.h:

                 struct server_addr_rec{

/**The next server int the list */
server_addr_rec *next;
/** The bound address, for this server */
apr_sockaddr_t *host_addr;
/** The bound port, for this server */
apr_port_t host_port;
/** The name given in "<VirtualHost>" */
char *virthost;
};
                 

Dont worry about what this all means – just notice that this is a very common and very powerful technique, and the design of data structures, just like the design of database tables to which it is closely related are the core, key, vital task for you to understand as a programmer.

You make the philosophical decisions that the world is like this and can be modelled in this way. A heavy responsibility – in philosophy this work is called ontology (what exists?) and epistemology (how we can know about it?). I bet you never thought that this was what you were doing!