How to shrink your stomach

How to shrink your stomach created by jack federbroke helpd me to loss weight so i want to feedback him for that thank you jack! How to shrink your stomach

Sunday, March 4, 2012



8.1 What Is A Pointer?

Simply stated, a pointer is an address. Instead of being a variable, it is a pointer to a variable
stored somewhere in the address space of the program. It is always best to use an example so
load the file named pointer.c and display it on your monitor for an example of a program
with some pointers in it.

main( ) /*  illustratrion of pointer use */
int index,*pt1,*pt2;
index = 39; /*  any numerical value */
pt1 = &index; /* the address of index  */
pt2 = pt1;
printf("The value is %d %d %d\n",index,*pt1,*pt2);
*pt1 = 13; /* this changes the value of index */
printf("The value is %d %d %d\n",index,*pt1,*pt2);

For the moment, ignore the declaration statement where we define "index" and two other fields
beginning with a star. It is properly called an asterisk, but for reasons we will see later, let’s
agree to call it a star. If you observe the first statement, it should be clear that we assign the
value of 39 to the variable "index". This is no surprise, we have been doing it for several
programs now. The next statement however, says to assign to "pt1" a strange looking value,
namely the variable "index" with an ampersand in front of it. In this example, pt1 and pt2 are
pointers, and the variable "index" is a simple variable. Now we have problem. We need to
learn how to use pointers in a program, but to do so requires that first we define the means of
using the pointers in the program.

The following two rules will be somewhat confusing to you at first but we need to state the
definitions before we can use them. Take your time, and the whole thing will clear up very

8.2 Two Very Important Rules

The following two rules are very important when using pointers and must be thoroughly

1. A variable name with an ampersand in front of it defines the address of the variable and
therefore points to the variable. You can therefore read line six as "pt1 is assigned the value of
the address of "index".

2. A pointer with a "star" in front of it refers to the value of the variable pointed to by the pointer.
Line nine of the program can be read as "The stored (starred) value to which the pointer "pt1"
points is assigned the value 13". Now you can see why it is convenient to think of the asterisk
as a star, it sort of sounds like the word store.

8.3 Memory Aids

1. Think of & as an address.
2. Think of * as a star referring to stored.

Assume for the moment that "pt1" and "pt2" are pointers (we will see how to define them shortly).
As pointers, they do not contain a variable value but an address of a variable and can be used
to point to a variable. Line six of the program assigns the pointer "pt1" to point to the variable
we have already defined as "index" to "pt1". Since we have a pointer to "index", we can
manipulate the value of "index" by using either the variable name itself, or the pointer.

Line nine modifies the value by using the pointer. Since the pointer "pt1" points to the variable
"index", then putting a star in front of the pointer name refers to the memory location to which
it is pointing. Line nine therefore assigns to "index" the value of 13. Anyplace in the program
where it is permissible to use the variable name "index", it is also permissible to use the name
"*pt1" since they are identical in meaning until the pointer is reassigned to some other variable.

8.4 Another Pointer

Just to add a little intrigue to the system, we have another pointer defined in this program, "pt2".
Since "pt2" has not been assigned a value prior to statement seven, it doesn’t point to anything,
it contains garbage. Of course, that is also true of any variable until a value is assigned to it.

Statement seven assigns "pt2" the same address as "pt1", so that now "pt2" also points to the
variable "index". So to continue the definition from the last paragraph, anyplace in the program
where it is permissible to use the variable "index", it is also permissible to use the name "*pt2"
because they are identical in meaning. This fact is illustrated in the first "printf" statement since
this statement uses the three means of identifying the same variable to print out the same variable
three times.

8.5 There Is Only One Variable

Note carefully that, even though it appears that there are three variables, there is really only one
variable. The two pointers point to the single variable. This is illustrated in the next statement
which assigns the value of 13 to the variable "index", because that is where the pointer "pt1" is
pointing. The next "printf" statement causes the new value of 13 to be printed out three times.
Keep in mind that there is really only one variable to be changed, not three.

This is admittedly a very difficult concept, but since it is used extensively in all but the most
trivial C programs, it is well worth your time to stay with this material until you understand it

8.6 How Do You Declare A Pointer?

Now to keep a promise and tell you how to declare a pointer. Refer to the third line of the
program and you will see our old familiar way of defining the variable "index", followed by
two more definitions. The second definition can be read as "the storage location to which "pt1"
points will be an int type variable". Therefore, "pt1" is a pointer to an int type variable. Likewise,
"pt2" is another pointer to an int type variable.

A pointer must be defined to point to some type of variable. Following a proper definition, it
cannot be used to point to any other type of variable or it will result in a "type incompatibility"
error. In the same manner that a "float" type of variable cannot be added to an "int" type variable,
a pointer to a "float" variable cannot be used to point to an integer variable.

Compile and run this program and observe that there is only one variable and the single statement
in line 9 changes the one variable which is displayed three times.

8.7 The Second Program With Pointers

In these few pages so far on pointers, we have covered a lot of territory, but it is important
territory. We still have a lot of material to cover so stay in tune as we continue this important
aspect of C. Load the next file named pointer2.c and display it on your monitor so we can
continue our study.

main( )
char strg[40],*there,one,two;
int *pt,list[100],index;
strcpy(strg,"This is a character string.");
one = strg[0]; /* one and two are identical */
two = *strg;
printf("The first output is %c %c\n",one,two);
one = strg[8]; /* one and two are identical */
two = *(strg+8);
printf("The second output is %c %c %c\n",one,two);
there = strg+10; /* strg+10 is identical to strg[10]  */
printf("The third output is %c\n",strg[10]);
printf("The fourth output is %c\n",*there);
for (index = 0;index < 100;index++)
list[index] = index + 100;
pt = list + 27;
printf("The fifth output is %d\n",list[27]);
printf("The sixth output is %d\n",*pt);

In this program we have defined several variables and two pointers. The first pointer named
"there" is a pointer to a "char" type variable and the second named "pt" points to an "int" type
variable. Notice also that we have defined two array variable named "strg" and "list". We will
use them to show the correspondence between pointers and array names.

8.8 A String Variable Is Actually A Pointer

In the programming language C, a string variable is defined to be simply a pointer to the beginning
of a string. This will take some explaining. Refer to the example program on your monitor.

You will notice that first we assign a string constant to the string variable named "strg" so we
will have some data to work with. Next, we assign the value of the first element to the variable
"one", a simple "char" variable. Next, since the string name is a pointer by definition of the C
language, we can assign the same value to "two" by using the star and the string name. The
result if the two assignments are such that "one" now has the same value as "two", and both
contain the character "T", the first character in the string. Note that it would be incorrect to
write the ninth line as "two = *strg[0];" because the star takes the place of the square brackets.

For all practical purposes, "strg" is a pointer. It does, however, have one restriction that a true
pointer does not have. It cannot be changed like a variable, but must always contain the initial
value and therefore always points to its string. It could be thought of as a pointer constant, and
in some applications you may desire a pointer that cannot be corrupted in any way. Even though
it cannot be changed, it can be used to refer to other values than the one it is defined to point to,
as we will see in the next section of the program.

Moving ahead to line 12, the variable "one" is assigned the value of the ninth variable (since
the indexing starts at zero) and "two" is assigned the same value because we are allowed to
index a pointer to get to values farther ahead in the string. Both variables now contain the
character "a".

The C programming language takes care of indexing for us automatically by adjusting the
indexing for the type of variable the pointer is pointing to. In this case, the index of 8 is simply
added to the pointer value variable before looking up the desired result because a "char" type
variable is one byte long. If we were using a pointer to an "int" type variable, the index would
be doubled and added to the pointer before looking up the value because an "int" type variable
uses two bytes per value stored. When we get to the chapter on structures, we will see that a
variable can have many, even into the hundreds or thousands, of characters per variable, but the
indexing will be handled automatically for us by the system.

Since "there" is already a pointer, it can be assigned the value of the eleventh element of "strg"
by the statement in line 16 of the program. Remember that since "there" is a true pointer, it can
be assigned any value as long as that value represents a "char" type of address. It should be
clear that the pointers must be "typed" in order to allow the pointer arithmetic described in the
last paragraph to be done properly. The third and fourth outputs will be the same, namely the
letter "c".

8.9 Pointer Arithmetic

Not all forms of arithmetic are permissible on a pointer. Only those things that make sense,
considering that a pointer is an address somewhere in the computer. It would make sense to
add a constant to an address, thereby moving it ahead in memory that number of places. Likewise,
subtraction is permissible, moving it back some number of locations. Adding two pointers
together would not make sense because absolute memory addresses are not additive. Pointer
multiplication is also not allowed, as this would be a funny number. If you think about what
you are actually doing, it will make sense to you what is allowed, and what is not.

8.10 Now For An Integer Pointer

The array named "list" is assigned a series of values from 100 to 199 in order to have some data
to work with. Next we assign the pointer "pt" the value of the 28th element of the list and print
out the same value both ways to illustrate that the system truly will adjust the index for the "int"
type variable. You should spend some time in this program until you feel you fairly well
understand these lessons on pointers.

Compile and run pointer2.c and study the output.

8.11 Function Data Return With A Pointer

You may recall that back in the lesson on functions we mentioned that there were two ways to
get variable data back from a function. One way is through use of the array, and you should be
right on the verge of guessing the other way. If your guess is through use of a pointer, you are
correct. Load and display the program named twoway.c for an example of this.

main( )
int pecans,apples;
pecans = 100;
apples = 101;
printf("The starting values are %d %d\n",pecans,apples);
/* when we call "fixup"   */
fixup(pecans,&apples); /* we take the value of pecans   */
/* we take the address of apples */
printf("The ending values are %d %d\n",pecans,apples);
fixup(nuts,fruit) /* nuts is an integer value */
int nuts,*fruit; /* fruit points to an integer */
printf("The value are %d %d\n",nuts,*fruit);
nuts = 135;
*fruit = 172;
printf("The values are %d %d\n",nuts,*fruit);

In twoway.c, there are two variables defined in the main program "pecans" and "apples".
Notice that neither of these if defined as a pointer. We assign values to both of these and print
them out, then callthe function "fixup" taking with us both of these values. The variable "pecans"
is simply sent to the function, but the address of the variable "apples" is sent to the function.

Now we have a problem. The two arguments are not the same, the second is a pointer to a
variable. We must somehow alert the function to the fact that it is supposed to receive an integer
variable and a pointer to an integer variable. This turns out to be very simple. Notice that the
parameter definitions in the function define "nuts" as an integer, and "fruit" as a pointer to an
integer. The call in the main program therefore is now in agreement with the function heading
and the program interface will work just fine.

In the body of the function, we print the two values sent to the function, then modify them and
print the new values out. This should be perfectly clear to you by now. The surprise occurs
when we return to the main program and print out the two values again. We will find that the
value of pecans will be restored to its value before the function call because the C language
makes a copy of the item in question and takes the copy to the called function, leaving the
original intact. In the case of the variable "apples", we made a copy of a pointer to the variable
and took the copy of the pointer to the function. Since we had a pointer to the original variable,
even though the pointer was a copy, we had access to the original variable and could change it
in the function. When we returned to the main program, we found a changed value in "apples"
when we printed it out.

By using a pointer in a function call, we can have access to the data in the function and change
it in such a way that when we return to the calling program, we have a changed value of data.

It must be pointed out however, that if you modify the value of the pointer itself in the function,
you will have restored pointer when you return because the pointer you use in the function is a
copy of the original. In this example, there was no pointer in the main program because we
simply sent the address to the function, but in many programs you will use pointers in function
calls. One of the places you will find need for pointers in function calls will be when you request
data input using standard input/output routines. These will be covered in the next two chapters.
Compile and run twoway.c and observe the output.

8.12 Pointers Are Valuable

Even though you are probably somewhat intimidated at this point by the use of pointers, you
will find that after you gain experience, you will use them profusely in many ways. You will
also use pointers in every program you write other than the most trivial because they are so
useful. You should probably go over this material carefully several times until you feel comfortable with it because it is very important in the area of input/output which is next on the

8.13 Programming Exercises

1. Define a character array and use "strcpy" to copy a string into it. Print the string out by using
a loop with a pointer to print out one character at a time. Initialize the pointer to the first element
and use the double plus sign to increment the pointer. Use a separate integer variable to count
the characters to print.

2. Modify the program to print out the string backwards by pointing to the end and using a
decrementing pointer.

next lesson

C LANGUAGE TUTORIAL 9. Standard Input/Output

No comments:

Post a Comment

App creators your key for sucsses

If you can click a mouse, you can
profit from online video!
Recent news articles show how some YouTubers are earning upwards of $100,000 per year from the ads they display on and around their videos...

What if you could earn money from those ads, without actually creating the content?


All you need to know in one place

Small step for huge knowledge
This is the place you learn everything you need to move forward and become the leader in programming and creating mobile apps.

No need to be an expert anyone can start from scratch !

You just need to find the area that interests you and access, from there the way to success is up to you .

Good Luck!

 Click Here App Creators

App creators


i want to know

hello everyone

i wanted to ask from the viewers

if ther any subject thet you read and want continue reading and its not completed yet please let me know by comment or email

or if ther any subject thet you want me to add pls let me know