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 />

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

}<br />

By using the GNU debugger, gdb, we can identify the actual memory locations that the C compiler uses to store the<br />

variable in the above program. It is then possible to draw up a table (see below) showing the detailed operation <strong>of</strong> the<br />

program. You should follow this example very carefully so that you understand what is going on.<br />

Variable Starting Value after the specified line has executed<br />

location 7 8 9 10 11 12 13 14<br />

------------------------------------------------------------------<br />

m 0xbffff98c 0 0 1 1 1 1 1 1<br />

n 0xbffff988 1 1 1 1 1 1 1 1<br />

k 0xbffff984 2 2 2 1 1 1 1 1<br />

p 0xbffff980 ? 98c 98c 98c 98c 98c 98c 98c<br />

msg 0xbffff970 hello hello hello hello hello Hello Hello HellO<br />

cp 0xbffff96c ? ? ? ? 970 970 976 976<br />

The memory addresses are shown in hexadecimal, and only the rightmost three digits are shown (e.g., 98c) for<br />

convenience in displaying the table. Memory addresses are always given in units <strong>of</strong> bytes. The above table shows, for<br />

example, that the variable "m" occupies the memory addresses 0xbffff98c, 0xbffff98d, 0xbffff98e, and 0xbffff98f. You<br />

will note that 16 bytes are reserved for "msg", even though "msg" only needs 6 bytes (5 for the characters in "hello"<br />

and one for the trailing null byte). The C compiler won't guarantee any particular placement in memory, and may leave<br />

gaps, particularly to align numbers with 4-byte boundaries (i.e., so that their memory addresses are divisable by 4).<br />

Note the very important point that the name <strong>of</strong> an array ("msg" in the above example), if used without an index, is<br />

considered to be a pointer to the first element <strong>of</strong> the array. In fact, an array name followed by an index is exactly<br />

equivalent to a pointer followed by an <strong>of</strong>fset. For example (pointerexample2.c),<br />

#include <br />

int main(void) {<br />

char msg[] = "hello world";<br />

char *cp;<br />

cp = msg;<br />

cp[0] = 'H';<br />

*(msg+6) = 'W';<br />

}<br />

printf("%s\n", msg);<br />

printf("%s\n", &msg[0]);<br />

printf("%s\n", cp);<br />

printf("%s\n", &cp[0]);<br />

return 0;<br />

Note, however, that "cp" is a variable, and can be changed, whereas "msg" is a constant, and is not an lvalue (i.e., it can<br />

not be used on the left <strong>of</strong> an equals sign).<br />

Pointer arithmetic<br />

You can add and subtract numbers to/from pointers, but the results may surprise you. For example, if "p" is a pointer to<br />

an "int" (i.e., "int *p"), then "p++" actually adds 4 to "p"! The logic behind this is that adding one to "p" should make it<br />

point to the next integer, which has a memory address that is 4 bytes greater.<br />

Pointers used as arguments to functions<br />

We have already seen that C functions can not alter their arguments. The simple reason for this is that when you call a<br />

function, the compiler passes copies <strong>of</strong> the values <strong>of</strong> the arguments to the function. For example, if you have a function<br />

as follow:<br />

void myfunc(int n) {<br />

n = 6;<br />

}<br />

and you call it with<br />

int i = 0;<br />

myfunc(i);<br />

Then "myfunc" is passed the number 0. "myfunc" knows nothing whatsoever about where this number came from. The

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

Saved successfully!

Ooh no, something went wrong!