11.07.2014 Views

C programming notes - School of Physics

C programming notes - School of Physics

C programming notes - School of Physics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C <strong>programming</strong> <strong>notes</strong><br />

file:///F:/my_docs/web_phys2020/C<strong>programming</strong><strong>notes</strong>.html<br />

32 <strong>of</strong> 40 19/03/2007 10:06 AM<br />

together, taking account <strong>of</strong> any overflow in the seconds and the minutes. The assert command is also used to ensure<br />

that the times that are input are also valid.<br />

To refer to an individual element <strong>of</strong> a structure, you use the following syntax:<br />

t1[0].hour = 12;<br />

t1[0].minutes = 0;<br />

t1[0].second = t2.second;<br />

t1[1] = t2;<br />

Alternatively, and somewhat confusingly, if "p", say, is a pointer to a structure, then you can access the elements <strong>of</strong> the<br />

structure using notation like "p->hour". For example:<br />

struct time *p;<br />

// p is a pointer to a time structure<br />

struct time t = {1,2,3.0}; // t is a time structure<br />

p = &t;<br />

// p now points to t<br />

p->hour = 6;<br />

// this statement is exactly equivalent to...<br />

t.hour = 6; // ...this one<br />

Structures can contain any type, including arrays and other structures. A common use is to create a linked list by having<br />

a structure contain a pointer to a structure <strong>of</strong> the same type, for example,<br />

struct person {<br />

char *name[80];<br />

char *address[256];<br />

struct person *next_person;<br />

};<br />

where "person.next_person" is actually a pointer to a "person" struct. Sounds complicated, but it is simple when you<br />

get the hang <strong>of</strong> it.<br />

Structures are the first step towards object-oriented <strong>programming</strong> (OOP), where most <strong>of</strong> the <strong>programming</strong> effort is<br />

devoted to defining the best representation <strong>of</strong> the data, and the operations which can be performed on the data.<br />

In physics, a structure can be used to represent the state <strong>of</strong> a physical system. For example, here is a structure that<br />

might be used to represent the state <strong>of</strong> a particle in three-dimensional space:<br />

struct particle {<br />

double mass;<br />

double position[3];<br />

double velocity[3];<br />

};<br />

We could then define an array <strong>of</strong> 1000 particles as simply as:<br />

struct particle p[1000];<br />

You can take this one step further and define your own type (like "int", "double") by doing:<br />

typedef struct {<br />

double mass;<br />

double position[3];<br />

double velocity[3];<br />

} particle;<br />

In which case you can define the array <strong>of</strong> 1000 particles as:<br />

particle p[1000];<br />

String functions<br />

The standard C libraries include a bunch <strong>of</strong> functions for manipulating strings (i.e., arrays <strong>of</strong> chars).<br />

Note that before using any <strong>of</strong> these functions, you should include the line "#include " in your program. This<br />

include-file defines prototypes for the following functions, as well as defining special types such as "size_t", which are<br />

operating-system dependent.<br />

char *strcat(s1, s2) Concatenates s2 onto s1. null-terminates s1.<br />

Returns s1.<br />

char *strchr(s, c)<br />

Searches string s for character c. Returns a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!