Search
Sponsors

Archive for the ‘C’ Category

Pointers and Structures

Thursday, February 21st, 2008

Pointers and Structures As you may know, we can declare the form of a block of data
containing different data types by means of a structure
declaration. For example, a personnel file might contain
structures which look something like:

struct tag{
char lname[20]; /* last name */
char fname[20]; /* first name */
int age; /* age */
float rate; /* e.g. 12.75 per hour */
};

Let’s say we have a bunch of these structures in a disk file
and we want to read each one out and print out the first and last
name of each one so that we can have a list of the people in our
files. The remaining information will not be printed out. We
will want to do this printing with a function call and pass to
that function a pointer to the structure at hand. For
demonstration purposes I will use only one structure for now. But
realize the goal is the writing of the function, not the reading
of the file which, presumably, we know how to do.

For review, recall that we can access structure members with
the dot operator as in:

————— program 5.1 ——————
#include <stdio.h>
#include <string.h>

struct tag{
char lname[20]; /* last name */
char fname[20]; /* first name */
int age; /* age */
float rate; /* e.g. 12.75 per hour */
};

struct tag my_struct; /* declare the structure m_struct */

int main(void)
{
strcpy(my_struct.lname,”Jensen”);
strcpy(my_struct.fname,”Ted”);
printf(”\n%s “,my_struct.fname);
printf(”%s\n”,my_struct.lname);
return 0;
}
————– end of program 5.1 ————–

Now, this particular structure is rather small compared to
many used in C programs. To the above we might want to add:

date_of_hire; (data types not shown)
date_of_last_raise;
last_percent_increase;
emergency_phone;
medical_plan;
Social_S_Nbr;
etc…..

If we have a large number of employees, what we want to do
manipulate the data in these structures by means of functions.
For example we might want a function print out the name of the
employee listed in any structure passed to it. However, in the
original C (Kernighan & Ritchie, 1st Edition) it was not possible
to pass a structure, only a pointer to a structure could be
passed. In ANSI C, it is now permissible to pass the complete
structure. But, since our goal here is to learn more about
pointers, we won’t pursue that.

Anyway, if we pass the whole structure it means that we must
copy the contents of the structure from the calling function to
the called function. In systems using stacks, this is done by
pushing the contents of the structure on the stack. With large
structures this could prove to be a problem. However, passing a
pointer uses a minimum amount of stack space.

(more…)

More on Strings

Thursday, February 21st, 2008

More on Strings Well, we have progressed quite a way in a short time! Let’s
back up a little and look at what was done in Chapter 3 on
copying of strings but in a different light. Consider the
following function:

char *my_strcpy(char dest[], char source[])
{
int i = 0;

while (source[i] != ‘\0′)
{
dest[i] = source[i];
i++;
}
dest[i] = ‘\0′;
return dest;
}

Recall that strings are arrays of characters. Here we have
chosen to use array notation instead of pointer notation to do
the actual copying. The results are the same, i.e. the string
gets copied using this notation just as accurately as it did
before. This raises some interesting points which we will
discuss.

Since parameters are passed by value, in both the passing of
a character pointer or the name of the array as above, what
actually gets passed is the address of the first element of each
array. Thus, the numerical value of the parameter passed is the
same whether we use a character pointer or an array name as a
parameter. This would tend to imply that somehow:

source[i] is the same as *(p+i);

In fact, this is true, i.e wherever one writes a[i] it can
be replaced with *(a + i) without any problems. In fact, the
compiler will create the same code in either case. Thus we see
that pointer arithmetic is the same thing as array indexing.
Either syntax produces the same result.

This is NOT saying that pointers and arrays are the same
thing, they are not. We are only saying that to identify a given
element of an array we have the choice of two syntaxes, one using
array indexing and the other using pointer arithmetic, which
yield identical results.

(more…)

Pointers and Strings

Thursday, February 21st, 2008

Pointers and Strings The study of strings is useful to further tie in the
relationship between pointers and arrays. It also makes it easy
to illustrate how some of the standard C string functions can be
implemented. Finally it illustrates how and when pointers can and
should be passed to functions.

In C, strings are arrays of characters. This is not
necessarily true in other languages. In BASIC, Pascal, Fortran
and various other languages, a string has its own data type. But
in C it does not. In C a string is an array of characters
terminated with a binary zero character (written as ‘\0′). To
start off our discussion we will write some code which, while
preferred for illustrative purposes, you would probably never
write in an actual program. Consider, for example:

char my_string[40];

my_string[0] = ‘T’;
my_string[1] = ‘e’;
my_string[2] = ‘d’:
my_string[3] = ‘\0′;

While one would never build a string like this, the end
result is a string in that it is an array of characters
_terminated_with_a_nul_character_. By definition, in C, a string
is an array of characters terminated with the nul character. Be
aware that “nul” is _not_ the same as “NULL”. The nul refers to a zero
as is defined by the escape sequence ‘\0′. That is it occupies
one byte of memory. The NULL, on the other hand, is the value of
an uninitialized pointer and pointers require more than one byte
of storage. NULL is #defined in a header file in your C
compiler, nul may not be #defined at all.

Since writing the above code would be very time consuming, C
permits two alternate ways of achieving the same thing. First,
one might write:

char my_string[40] = {’T', ‘e’, ‘d’, ‘\0′,};

But this also takes more typing than is convenient. So, C
permits:

char my_string[40] = “Ted”;

When the double quotes are used, instead of the single quotes
as was done in the previous examples, the nul character ( ‘\0′ )
is automatically appended to the end of the string.

In all of the above cases, the same thing happens. The
compiler sets aside an contiguous block of memory 40 bytes long
to hold characters and initialized it such that the first 4
characters are Ted\0.

Now, consider the following program:

——————program 3.1————————————-
#include <stdio.h>

char strA[80] = “A string to be used for demonstration purposes”;
char strB[80];

int main(void)
{
char *pA; /* a pointer to type character */
char *pB; /* another pointer to type character */
puts(strA); /* show string A */
pA = strA; /* point pA at string A */
puts(pA); /* show what pA is pointing to */
pB = strB; /* point pB at string B */
putchar(’\n’); /* move down one line on the screen */
while(*pA != ‘\0′) /* line A (see text) */
{
*pB++ = *pA++; /* line B (see text) */
}
*pB = ‘\0′; /* line C (see text) */
puts(strB); /* show strB on screen */
return 0;
}
——— end program 3.1 ————————————-

In the above we start out by defining two character arrays of
80 characters each. Since these are globally defined, they are
initialized to all ‘\0’s first. Then, strA has the first 42
characters initialized to the string in quotes.

Now, moving into the code, we declare two character pointers
and show the string on the screen. We then “point” the pointer pA
at strA. That is, by means of the assignment statement we copy
the address of strA[0] into our variable pA. We now use puts()
to show that which is pointed to by pA on the screen. Consider
here that the function prototype for puts() is:

int puts(const char *s);

For the moment, ignore the “const”. The parameter passed to
puts is a pointer, that is the _value_ of a pointer (since all
parameters in C are passed by value), and the value of a pointer
is the address to which it points, or, simply, an address. Thus
when we write:

puts(strA); as we have seen, we are passing the

address of strA[0]. Similarly, when we write:

puts(pA); we are passing the same address, since

we have set pA = strA;

Given that, follow the code down to the while() statement on
line A. Line A states:

While the character pointed to by pA (i.e. *pA) is not a nul
character (i.e. the terminating ‘\0′), do the following:

line B states: copy the character pointed to by pA to the
space pointed to by pB, then increment pA so it points to the
next character and pB so it points to the next space.

When we have copied the last character, pA now points to the
terminating nul character and the loop ends. However, we have not
copied the nul character. And, by definition a string in C
_must_ be nul terminated. So, we add the nul character with line
C.

(more…)

Pointer types and Arrays

Thursday, February 21st, 2008

Pointer types and Arrays Okay, let’s move on. Let us consider why we need to identify
the “type” of variable that a pointer points to, as in:

int *ptr;

One reason for doing this is so that later, once ptr “points
to” something, if we write:

*ptr = 2;

the compiler will know how many bytes to copy into that memory
location pointed to by ptr. If ptr was declared as pointing to an
integer, 2 bytes would be copied, if a long, 4 bytes would be
copied. Similarly for floats and doubles the appropriate number
will be copied. But, defining the type that the pointer points
to permits a number of other interesting ways a compiler can
interpret code. For example, consider a block in memory
consisting if ten integers in a row. That is, 20 bytes of memory
are set aside to hold 10 integers.

Now, let’s say we point our integer pointer ptr at the first
of these integers. Furthermore lets say that integer is located
at memory location 100 (decimal). What happens when we write:

ptr + 1;

Because the compiler “knows” this is a pointer (i.e. its
value is an address) and that it points to an integer (its
current address, 100, is the address of an integer), it adds 2 to
ptr instead of 1, so the pointer “points to” the _next_
_integer_, at memory location 102. Similarly, were the ptr
declared as a pointer to a long, it would add 4 to it instead of
1. The same goes for other data types such as floats, doubles,
or even user defined data types such as structures. This is
obviously not the same kind of “addition” that we normally think
of. In C it is referred to as addition using “pointer
arithmetic”, a term which we will come back to later.

Similarly, since ++ptr and ptr++ are both equivalent to
ptr + 1 (though the point in the program when ptr is incremented
may be different), incrementing a pointer using the unary ++
operator, either pre- or post-, increments the address it stores
by the amount sizeof(type) where “type” is the type of the object
pointed to. (i.e. 2 for an integer, 4 for a long,
etc.).

Since a block of 10 integers located contiguously in memory
is, by definition, an array of integers, this brings up an
interesting relationship between arrays and pointers.

Consider the following:

int my_array[] = {1,23,17,4,-5,100};

Here we have an array containing 6 integers. We refer to
each of these integers by means of a subscript to my_array, i.e.
using my_array[0] through my_array[5]. But, we could
alternatively access them via a pointer as follows:

int *ptr;

ptr = &my_array[0]; /* point our pointer at the first
integer in our array */

And then we could print out our array either using the array
notation or by dereferencing our pointer. The following code
illustrates this:
——————————————————
#include <stdio.h>

int my_array[] = {1,23,17,4,-5,100};
int *ptr;

int main(void)
{
int i;
ptr = &my_array[0]; /* point our pointer to the first
element of the array */
printf(”\n\n”);
for(i = 0; i < 6; i++)
{
printf(”my_array[%d] = %d “,i,my_array[i]); /*<– A */
printf(”ptr + %d = %d\n”,i, *(ptr + i)); /*<– B */
}
return 0;
}
—————————————————-
Compile and run the above program and carefully note lines A
and B and that the program prints out the same values in either
case. Also observe how we dereferenced our pointer in line B,
i.e. we first added i to it and then dereferenced the new
pointer. Change line B to read:

printf(”ptr + %d = %d\n”,i, *ptr++);

and run it again… then change it to:

printf(”ptr + %d = %d\n”,i, *(++ptr));

and try once more. Each time try and predict the outcome and
carefully look at the actual outcome.

(more…)

Pointers tutorial 1

Thursday, February 21st, 2008

CHAPTER 1: What is a pointer? One of the things beginners in C find most difficult to
understand is the concept of pointers. The purpose of this
document is to provide an introduction to pointers and their use
to these beginners.

I have found that often the main reason beginners have a
problem with pointers is that they have a weak or minimal feeling
for variables, (as they are used in C). Thus we start with a
discussion of C variables in general.

A variable in a program is something with a name, the value
of which can vary. The way the compiler and linker handles this
is that it assigns a specific block of memory within the computer
to hold the value of that variable. The size of that block
depends on the range over which the variable is allowed to vary.
For example, on PC’s the size of an integer variable is 2 bytes,
and that of a long integer is 4 bytes. In C the size of a
variable type such as an integer need not be the same on all
types of machines.

When we declare a variable we inform the compiler of two
things, the name of the variable and the type of the variable.
For example, we declare a variable of type integer with the name
k by writing:

int k;

On seeing the “int” part of this statement the compiler sets
aside 2 bytes (on a PC) of memory to hold the value of the
integer. It also sets up a symbol table. And in that table it
adds the symbol k and the relative address in memory where those
2 bytes were set aside.

Thus, later if we write:

k = 2;

at run time we expect that the value 2 will be placed in that
memory location reserved for the storage of the value of k. In C
we refer to a variable such as the integer k as an “object”.

In a sense there are two “values” associated with the object
k, one being the value of the integer stored there (2 in the
above example) and the other being the “value” of the memory
location where it is stored, i.e. the address of k. Some texts
refer to these two values with the nomenclature rvalue (right
value, pronounced “are value”) and lvalue (left value, pronounced
“el value”) respectively.

In some languages, the lvalue is the value permitted on the
left side of the assignment operator ‘=’ (i.e. the address where
the result of evaluation of the right side ends up). The rvalue
is that which is on the right side of the assignment statement,
the ‘2′ above. Rvalues cannot be used on the left side
of the assignment statement. Thus: 2 = k; is illegal.

Actually, the above definition of “lvalue” is somewhat
modified for C. According to K&R-2 (page 197): [1]

“An _object_ is a named region of storage; an _lvalue_ is an
expression referring to an object.”

However, at this point, the definition originally cited above is
sufficient. As we become more familiar with pointers we will go
into more detail on this.

Okay, now consider:

int j, k;
k = 2;
j = 7; <– line 1
k = j; <– line 2

In the above, the compiler interprets the j in line 1 as the
address of the variable j (its lvalue) and creates code to copy
the value 7 to that address. In line 2, however, the j is
interpreted as its rvalue (since it is on the right hand side of
the assignment operator ‘=’). That is, here the j refers to the
value _stored_ at the memory location set aside for j, in this
case 7. So, the 7 is copied to the address designated by the
lvalue of k.

(more…)

Translate