Learn to use pointers
UNDERSTANDING POINTERS (for beginners) by Ted Jensen Version 0.0 This material is hereby placed in the public domain September 5, 1993 TABLE OF CONTENTS INTRODUCTION; CHAPTER 1:.? What is a pointer CHAPTER 2: Pointer types and Arrays CHAPTER 3: Pointers and Strings Chapter 4: More On Strings Chapter 5: Pointers and Structures Chapter 6: Some More ON Strings Epilog: ======================= ==================================================== Introduction: over a period of several years of monitoring varioustelecommunication conferences on CI have noticed that one of themost difficult problems for beginners was the understanding ofpointers. After writing dozens of short messages in attempts toclear up various fuzzy aspects of dealing with pointers, I set upa series of messages arranged in "chapters "Which I Could Drawfrom or Email to Various Individuals WHO APP eared to need help inthis area. Recently, I posted all of this material in the FidoNet CECHOconference. It received such a good acceptance, I decided toclean it up a little and submit it for inclusion in Bob Stout '
sSNIPPETS file. It is my hope that I can find the time to expand on this textin the future. To that end, I am hoping that those who read thisand find where it is lacking, or in error, or unclear, wouldnotify me of same so the next version, should there be one, I cancorrect these deficiencys. It is impossible to acknowledge all those whose messages onpointers in various nets contributed to my knowledge in thisarea. So, I will just say Thanks to All. I frequent the CECHO on FidoNet via RBBSNet and becontacted via can the echo itself or by email at: RBBSNet address 8: 916 / 1.I can also be reached viaInternet email at ted.jensen@spacebbs.comOr Ted Jensen PO Box 324 Redwood City, CA 94064 == ============================================================================================================================================================================================================= ==============
CHAPTER 1: What is a pointer One of the things beginners in C find most difficult tounderstand is the concept of pointers The purpose of thisdocument is to provide an introduction to pointers and their useto these beginners I have found that often the main reason?.. beginners have aproblem with pointers is that they have a weak or minimal feelingfor variables, (as they are used in C). Thus we start with adiscussion of C variables in general. A variable in a program is something with a name, the valueof which can vary. The way the compiler and linker handles thisis that it assigns a specific block of memory within the computerto hold the value of that variable. The size of that blockdepends 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 avariable type such as an integer need not be the same on alltypes of machines. When we declare a variable w e inform the compiler of twothings, the name of the variable and the type of the variable.For example, we declare a variable of type integer with the namek by writing: int k; On seeing the "int" part of this statement the compiler setsaside 2 bytes (on a PC) of memory to hold the value of theinteger. It also sets up a symbol table. and in that table itadds the symbol k and the address in memory where those 2 byteswere set aside. Thus, later if we WRITE: K = 2; at Run Time We Expect this value 2 Will Be Placed in Thatmemory Location Reserved for the Storage of The Value of K. in a Sense There Two "VALUES"
associated with k, onebeing the value of the integer stored there (2 in the aboveexample) and the other being the "value" of the memory locationwhere it is stored, ie the address of k. Some texts refer tothese two values with the nomenclature rvalue (right value, pronounced "are value") and lvalue (left value, pronunced "elvalue") respectively. The lvalue is the value permitted on the left side of theassignment operator '=' (ie the address where the result ofevaluation of the right side ends up) The rvalue is that whichis on the right side of the assignment statment, the '2' above.Note that rvalues can not be used on the left side of theassignment statement Thus:.. 2 = k; is illegal 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 Theaddress of the Variable J (ITS Lvalue) And Creates Code to Copythe Value 7 To That Address. in line 2, However, The J Isinterpreted As i ts rvalue (since it is on the right hand side ofthe assignment operator '='). That is, here the j refers to thevalue _stored_ at the memory location set aside for j, in thiscase 7. So, the 7 is copied to the address designated by thelvalue of k. In all of these examples, we are using 2 byte integers so allcopying of rvalues from one storage location to the other is doneby copying 2 bytes. Had we been using long integers, we would becopying 4 bytes. Now Let '
s say that we have a reason for wanting a variabledesigned to hold an lvalue (an address). The size required tohold such a value depends on the system. On older desk topcomputers with 64K of memory total, the address of any point inmemory can be contained in 2 bytes. Computers with more memorywould require more bytes to hold an address. Some computers, such as the IBM PC might require special handling to hold asegment and offset under certain circumstances. The actual sizerequired is not too important so long as we have a way ofinforming the compiler that what we want to store is an address. Such a variable is called a "pointer variable" (for reasonswhich will hopefully become clearer a little later). In C whenwe define a pointer variable we do so by preceding its Name Withan Asterisk. in c weapo give our pointer a Type Which, in thiscase, refers to the Type of Data Stored At The Address We Will Bestoring IN Our Pointer. for Example, Consider The VariableDefinition: int * Ptr; ptr is the _name_ of our variable (just as 'k' was the nameof our integer variable) The '*' informs the compiler that wewant a pointer variable, ie to set aside however many bytes isrequired to store an address in. Memory. The "Int" Says That Weintend to Use Our Pointer Variable To Store The Address of Aninteger. Such a Pointer IS said to "Point to" an INTEGER. Note, HOWEVER, THEN WHEN WHEN WROTE "INT K" INT K; "WE DID NOT Give Ka Value.if this Definiton WAS Made Outside of Any Function Many Compilerswill Initialize It To Zero. Simlarly, Ptr Has No Value, That IsWe Haven '
t stored an address in it in the above definition. Inthis case, again if the definition is outside of any function, itis intialized to a value #defined by your compiler as NULL. Itis called a NULL pointer. While in most cases NULL is # definedas zero, it need not be. that is, different compilers handlethis differently. Also note that while zero is an integer, NULLneed not be. But, back to using our new variable ptr. Suppose now that wewant to store in ptr the address of Our Integer Variable K. Todo this We Use: Ptr = & K; What The '&' Operator Does Is Retrieve The Lvalue (Address) of K, Even Though K Is on The Right Hand Side of To assignmentoperator '=', and copies that to the contents of our pointer ptr.Now, ptr is said to "point to" k. Bear with us now, there isonly one more operator we need to discuss. The "dereferencing operator" is the Asterisk and it is buyas backs: * ptr = 7; Will Copy 7 to the address pointed to by Ptr. t Hus if PTR "Points to" K, The Above Statement Willset The Value of K to 7. That IS, WHEN We Use the '*' this Waywe all ptr is Pointingat, NOT The Value of The Pointer Itself. Similarly, We Could Write: Printf ("% D / N", * PTR); To print to the screen of the integer value stored at the addresspointed to by "ptr". One way to see how All this stuff fits together would be torun the fol and the outputcareful. ---------------------------------------------------------------------------------------------------------- ------------------ # includeint J, k;
INT * PTR;
Int main (void)
{
J = 1;
K = 2;
PTR = & K;
Printf ("/ n"); Printf ("J Has The Value% D and IS Stored AT% P / N", J, & J);
Printf ("K Has The Value% D and IS Stored AT% P / N", K, & K);
Printf ("Ptr Has The Value% P and IS Stored AT% P / N", PTR, & PTR);
Printf ("The Value of the Integer Pointed to By Ptr IS% D / N",
* PTR);
Return 0;
}
---------------------------------------
To review:
A variable is defined by giving it a a type and a name (e.g.
INT K;)
A Pointer Variable is defined by giving it a type and a name
(E. INT * PTR) Where the asseterisk tells the compiler That
The variable named PTR is a Pointer Variable and the Type
TELLS the compiler what type the pointer is to point to point
INTEGER IN THIS CASE.
Once a variable is defined, we can get its address by
Preceding Its Name with the unary '&' Operator, AS IN & K.
We can "dereference" a Pointer, I.E. Refer to the value of
That Which it points to, by using the unary '*' Operator AS
in * ptr.
An "LValue" of a variable is the value of its address, i.e.
Where it is stored in memory. The "rvalue" of a variable is
The Value Stored In That Variable (at That Address).
============================================================================================================================================================================================================= ================
Chapter 2: 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 "Pointsto" 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 Defined 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 integer.
Now, Let's Say We Point Our Integer Pointer Ptr at The First THE FIRST
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 anteger (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
Defined As a Pointer to a long, IT Would Add 4 to it IT ITETAD OF
1. The Same Goes for Other Data Types Such as Floats, Doubles,
OR EVEN User Defined Data Types Such As Structure.
Similarly, Since PTR and PTR Are Both Equivalent To
PTR 1 (though the point in the program by the point in the program of 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 (I.E. 2 for an INTEGER, 4 for a long,
ETC.).
Since A Block of 10 Integers Located ContiUsly IN Memoryis, 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
Alternative Access The Via a Pointer As Follows:
INT * PTR;
PTR = & my_Array [0]; / * Point Our Pointer At the first
Integer IN Our Array * /
And the cele could print out our array each used the arch
NOTATION or by dereferencing urpinter. The folowing code
Illustrates this:
-------------------------------------------------- ----
#include
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 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 what the program sharps out the same value in Either
Case. Also Note How We dereferenced Our Pointer in line b, i.e.
We first added i to it and the dereferenced the 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 there. Each Time Try and predict the outcome and
Carefully Look at The Actual Outcome.in C, The Standard States That Wherever We might USE
& var_name [0] we can replace this with var_name, thus in ou chess
WHERE We wrote:
PTR = & my_Array [0];
WE CAN WRITE:
PTR = my_Array; To Achieve The Same Result.
This Leads Many Texts To State That The Name of An Array Is A
Pointer. While this is true, i prefer to mentally think "the
Name of the array is a _constant_ pointer ". Many Beginners
(Including myself when I Was Learning) Forget That _constant_
Qualifier. in My Opinon this Leads to some confusion. for
Example, while we can write ptr = my_Array; we cannot Write
MY_ARRAY = PTR;
THE REAON IS That The While PTR IS A Variable, My_Array IS A
Constant. That IS, THE LOCATION AT Which The First Element of
MY_ARRAY WILL BE Stored Cannot Be Changed Once My_Array [] HAS
BEEN DECLARED.
Modify The Example Program Above by Changing
PTR = & my_Array [0]; to PTR = my_Array;
And Run it agin to verify the results are identical.
Now, Let's Delve a little further inTo the Difference Between
The name "ptr" and "my_array" as buying Above. We said That
MY_ARRAY Is a constant pointer. What do we mean by THAT? Well,
To Understand The Term "Constant" in this Sense, Let's Go Back to
Our definition of the term "variable". WHEN WE Define a variable
We set aside a spot in membrate to hold the value of the
ApproPriate Type. Once That Is Done the name of the variable can
Be Interpreted in One of Two Ways. When Used On The Left Side of
The Assignment Operator, The Compiler Interprets It as The Memory
Location to which to move what which lies on the right side of
The Assignment Operator. But, When Used on The Right Side of THEASSIGNMENT OPERATOR, The Name of a variable is interpreted to
Mean The Contents Stored At That Memory Address Set Aside To Hold
The value of what variable.
With That In Mind, Let's Now Consider The SimpleSt of That NOW CONSIDER
Constants, AS IN:
INT I, K;
i = 2;
Here, While "I" is a variable and the occupies space in the
Data Portion of Memory, "2" is a constant and, as such, instead
Of setting aside memory in the data segment, it is imbedded
Directly in The Code Segment of Memory. That is, while Writing
Something like k = i; tells the compiler to create Code Which AT
Run Time Will Look at Memory Location & I to Determine The Value
To Be Moved to K, Core Created By I = 2; Simply Puts The '2' in
The code and there is no reference of the data segment.
Similarly, in The Above, Since "MY_Array" is a constant, overce
The Compiler Establishes Where The Array Itself Is To Be Stored,
IT "KNOWS" The Address of My_Array [0] and on seeing:
PTR = my_Array;
IT Simply Uses this address as a constant in the code segment and
............
Well, That's a lot of technical stuff to digest and i don't
Expect a beginner to understand all of it on first ready. with
Time and Experimentation you willow going to come back and re-read
THE FIRST 2 Chapter, But For Now, Let's Move on To The
RELATIONSHIP BETWEEN POINTERS, Character Arrays, And Strings.
============================================================================================================================================================================================================= ================ CHApter 3: 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
Finally it illustrate 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 Pascal OR (Most Versions
Of) Basic, Strings Are Treated Differently from Arrays. To Start
Off our discussion we willwreite 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. Note
That "NUL" is _not_ the Same as "null". The Nul Refers to a Zero
As defined by the escape sequence '/ 0'. That 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
Null is #defined in a header file in your ccCompiler, 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 Issient. So, C
PERMITS:
CHAR MY_STRING [40] = "TED";
When a double quotes are buy, 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 case, the same.
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
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 Of80 Characters Each. Since these Are Globally Defined, They Are
Initialized to all '/ 0's first. Then, Stra Has The First 42
CHARACTERS INTIALIZED to the STRING in quotes.
Now, Moving Into The Code, We Define Two Character Pointers
And show the string on the screen. We then "Point" The Ponter Pa
At stra. That is, by means of the assignment statement we copy
The Address of Stra [0] INTO OUR Variable Pa. We now puts ()
To show what 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 State:
While the Character Pointed to by Pa (I.E. * Pa) is not a nul
Character (I.E. 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.
Note That 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 addthe Nul Character with line C.
IT is Very Educational to Run this Program with your debugger
While Watching Stra, Strb, Pa and Pb And Single Stepping Through
The Program. It is even more educational if instead of simply
Defining strb [] AS HAS BEEN DONE ABOVE, Initialize It Also with
Something Like:
STRB [80] = "12345678901234567890123456789012345678901234567890"
WHERE The Number of Digits Used Is Greater Than The Length of
Stra and the repeat the Single Stepping Procedure While Watching
The Above Variables. Give these Things a Try!
Of Course, What The Above Program Illustrate Is A Simple Way
OF COPYING A STRING. AFTER Playing with the Above Until you Have
A good understanding of what is happening, we can pro Chenged to
Creating Our OWN Replacement for the Standard Strcpy () That Comes
With C. IT Might Look Like:
Char * my_strcpy (char * desination, char * source)
{
Char * p = destination
While (* Source! = '/ 0')
{
* p = * source ;
}
* p = '/ 0';
Return destination.
}
In this case, I have followed the practice buy
STANDARD ROUTINE OF RTURNING A POINTER TO The Destination.
Again, The Function Is Designed to Accept The Values of TWO
Character Pointers, I.E. Addresses, and Thus in the previous
Program We Could Write:
Int main (void)
{
MY_STRCPY (STRB, STRA);
PUTS (STRB);
}
I have deviated slightly from the form buy in standard C
Which Would Have The Prototype:
Char * my_strcpy (char * destination, const char * source);
Here the "const" modifier is buy to associure the user what
Function will not modify the contents pointed to by the source
Pointer. You can prot this by modifying the function Above, andits prototype, to include the "const" modifier as shown. Then,
WITHIN THE FUNCTION You Can Add A Statement Which Attempts To
Change the contents of what which is pointed to by source, such
AS:
* Source = 'x';
Which Would Normally Change The First Character of The String To
An X. The Const Modifier Should Cause Your Compiler To Catch
THIS AN ERROR. TRY IT and See.
Now, Let's Consider of the Things the Above EXAMPLES
Have Shown US. First Off, Consider The Fact That * Ptr Is To BE
Interpreted as returning the value pointed to by ptr wife
Incrementing The Pointer Value. on The Other Hand, Note That
THIS HAS TO DO with the precedence of the operatrs. Were We To
Write (* PTR) We would increment, NOT THE POINTER, But That
Which the Pointer Points to! i.e. if used on the first character
of the above example string the 't' Would Be Increment TO A
'U'. You Can Write Some Simple Example Code to Illustrate this.
Recall Again That A String is nothing more Than An Array
Of Characters. What We Have Done Above Is Deal with Copying
An Array. It happens to be an array of characters but the
Technique Could Be Applied to an Array of Integers, Doubles,
Etc. in Those Cases, HoWever, We would not be dealing with
Strings and hence the end of the array 10 NOT BE
_AUTOMAATICALLY_ Marked with a Special Value Like the NUL
Character. We Could IMplement a Version That Relied ON A
Special Value to Identify The End. for Example, We Could
Copy an array of postive integers by marking the end with a
NEGATIVE INTEGER. On The Other Hand, IT Is More Usual That
WHEN WE WRITE A FUNCTION TO COPY ARRAY OF ITHERTHAN STRINGS WE Pass The Function The Number of Items To BE
Copied as well as the address of the array, e.g. Something
Like The Following Prototype Might Indicate:
Void int_copy (int * PTRA, INT * PTRB, INT NBR);
WHERE NBR Is The Number of Integers to Be copied. you might want
To Play with this idea and create an array of integers and seeiff
You can write the function int_copy () And make it it work.
Note That this permits using functions to manipulate Very
Large arrays. for example, if we have an array of 5000 integers
That we want to manipulate with a function, we need online
That Function the address of the array (and any auxiliary)
Information Such As NBR Above, Depending On What We are doing.
The array itself does _not_ get passed, i.e. the whole array is
NOT COPIED and PUT IN THE Stack Before Calling The Function, ONLY
ITS Address is Sent.
Note That this is Different from passing, Say an integer, to
A Function. WHEN WHEN WHEN WHEN WHEN WHEN WE MAKE A COPY OF THE
Integer, I.E. Get ITS Value and Put It on the stack. within thein
Function Any Manipulation of The Value Pass CAN in No Way
Effect The Original Integer. But, with arrays and pointers we
Can Pass the address of the variable and hence manipulate the
Values of the Original Variables.
============================================================================================================================================================================================================= ================
Chapter 4: More on strings
Well, WE Have Progressed Quite Aways in A Short Time! Let'sback 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 des [], 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 Nota 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 Numeric Value of the Parameter Passed Is The
Same WHETHER WE USE A Character Pointer or An Array Name As A
Parameter. This 10 TEND TEND TEND TEND TEND TEND TEND TEND TEND TEND TEND TEND TEND TEND TEND TEND To IMPLY THAT
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. Now, Looking
At this Last Expression, Part of It .. (A I) IS A Simple
Addition Using The Operator and The Rules of C State That Such
An Expression IS Commutative. That IS (A I) Is Identical To
(i a). Thus we could write * (i a) Just As Easily AS
* (A I).
But * (i a) Could Have Come from i [A]! From all of this
Comes The Curious Truth That IF:
Char a [20];
INT I;
Writing a [3] = 'x'; is the Same as Writing3 [A] = 'X';
Try it! Set up an array of characters, integers or longs,
Etc. and assigned the 3rd or 4th Element a value sale the
Conventional Approach and The Print Out That Value To Be Sure
You Have That Working. Then Reverse The Array Notation As I Have
Done Above. A Good Compiler Will NOT BALL AND THE RESULL WILL
Be Identical ... Nothing More!
Now, Looking At Our Function Above, WHEN WE WRITE:
DEST [i] = source [i];
This Gets Interpreted by c to ie
* (DEST I) = * (Source i);
But, this Takes 2 Additions for Each Value Taken on By I.
Additions, Generally Speaking, Take More Time Than
Incrementations (Such As Those Done Using The Operator AS IN
i ). This May not be true in modern Optimizing Compilers, But
One Can Never Be Sure. Thus, The Pointer Version May Be A Bit
Faster Than The Array Version.
Another Way to Speed Up The Pointer Version Would Be To
Change:
While (* Source! = '/ 0') to Simply While (* Source)
Since The Value Withnin The Preenthesis Will Go To Zero (False) AT
The Same Time IN Either Case.
At this point you mightow to experiment a bit with Writing
Some of Your Own Programs Using Pointers .manipulating strings
IS a good place to experiment. You might want to write your own
Versions of Such Standard Functions As:
Strlen ();
STRCAT ();
Strchr ();
And Any Others you might has your system.
We will come back to strings and their manipulation through
Pointers in A Future Chapter. For now, Let's move on and discuss
Structures for a bit.
============================================================================================================================================================================================================= ================ CHApter 5: 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; / * agn * /
FLOAT RATE; / * E.G. 12.75 per hours * /
}
Let's Say We Have An 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 a list of the people in ou
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 going
Of The File Which, PResum - WE KNOW How To do.
For Review, Recall That We CAN Access Structure Members with STRUCTURE
The Dot Operator as in:
--------------- Program 5.1 ------------------
#include
#include
Struct tag {
Char lname [20]; / * last name * /
Char fname [20]; / * first name * /
Int Age; / * agn * /
FLOAT RATE; / * E.G. 12.75 per hours * /
}
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 mightow address:
Date_of_hire;
DATE_OF_LAST_RAISE;
Last_Percent_Increase;
Emergency_phone;
Medical_plan;
Social_s_nbr;
ETC .....
Now, 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 Any
Structure Passed to It. However, in The Original C (Kernighan &
Ritchie) 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 There Must Be
ENOUGH ROOM on the stack to hold it. with large structures this
Could Prove To Be a Problem. However, Passing a Pointer Uses A
Minimum Amount of Stack Space.
In Any Case, Since this Is A Discussion of Pointers, We Will
Discuss How We Go About Passing a Pointer to A Structure and Then
Using it within the function.
Consider the case described, I. we want a function That
Will Accept As a parameter a Pointer to a structure and from
. "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "
For Example We Want to Print Out The Name of The Employee IN OUR
EXAMPLE STRUCTURE.
Okay, So we know That Our Pointer is Going to Point To A
Structure Declared Using Struct Tag. We define Such a Pointerwith The Definition:
Struct Tag * ST_PTR;
And We Point It to Our Example Structure with:
ST_PTR = & MY_STRUCT;
Now, we can access a given member by de-referencing the
Pointer. But, How do we de-reference the pointer to a structure?
Well, consider the fact this we might want to use the pointer to
Set The agent of the employee. We would write:
(* ST_PTR) .age = 63;
Look at this Carefully. It Says, Replace That within
Parenthesis with this which st_ptr points to, which is the
Structure my_struct. thus, this breaks down to the same as
MY_STRUCT.AGE.
However, this is a fairly offen buy expression and the
Designers of C Have Created An Alternate Syntax with The Same
Meaning which is:
ST_PTR-> AGE = 63;
With That in Mind, Look at the Following Program:
------------ Program 5.2 --------------
#include
#include
Struct tag {/ * the structure type * /
Char lname [20]; / * last name * /
Char fname [20]; / * first name * /
Int Age; / * agn * /
FLOAT RATE; / * E.G. 12.75 per hours * /
}
Struct Tag my_struct; / * define the structure * /
Void show_name (struct tag * p); / * function prototype * /
Int main (void)
{
Struct Tag * ST_PTR; / * a Pointer to a structure * /
ST_PTR = & my_struct; / * Point the Pointer to my_struct * /
STRCPY (My_Struct.lname, "Jensen");
STRCPY (My_STRUCT.FNAME, "TED");
Printf ("/ n% s", my_struct.fname);
Printf ("% s / n", my_struct.lname);
my_struct.age = 63;
Show_name (ST_PTR); / * Pass the Pointer * /
Return 0;
}
Void show_name (struct tag * p)
{
Printf ("/ n% s", p-> fname); / * p points to a structure * / printf ("% s", p-> lname);
Printf ("% D / N", P-> AGE);
}
-------------------- End of Program 5.2 ----------------
Again, this is a lot of information to absorb at one time.
The Reader Should Compile and Run The Various Code Snippets and
USING a Debugger Monitor Things Like My_STRUCT and P While Single
Stepping through the main and folload the code down 通o
Function to see what is happens.
============================================================================================================================================================================================================= ================
Chapter 6: Some More On Strings, And Arrays of Strings
Well, Let's Go Back to Strings for A bit. In the folowing
All Assignments Are To Be Understood As Being Global, I.E. MADE
Outside of any function, incruding main.
We Pointed Out in An Earlier Chapter That We Could Write:
CHAR MY_STRING [40] = "TED";
Which Would Allocate Space for a 40 Byte Array and Put the String
In The First 4 Bytes (Three for the Characters in The quotes and
a 4th to handle the terminating '/ 0'.
Actually, IF All We Wanted to Do Was Store The Name "TED" WE
COULD WRITE:
CHAR MY_NAME [] = "TED";
And The Compiler Would Count The Characters, Leave Room for The
Nul Character and Store The Total of The Four Characters in Memory
THE LOCATION OF Hich Would Be Returned by The Array Name, in this
Case my_string.
In Some Code, INSTEAD of the Above, you might see:
Char * my_name = "TED"; Which is an alternate approach. is the the a Difference Between
THE? The answer is .. Yes. Using the array notation 4 bytes of
Storage In The Static Memory Block Are Taken Up, One for Each
Character and one for the Nul Character. But, in The Pointer
NOTATION THE SAME 4 bytes Required, _plus_ n bytes to store the
Pointer Variable My_Name (Where n depends on the system but is
USUALLY a minimum of 2 bytes and can be 4 or more).
In the array notation, my_name is a constant (not A
Variable). in the point notation my_name is a variable. as to
Which is the _better_Metter, That Depends on what you are going
TO Do Withnin the rest of the program.
Let's now Go One Step Further and Consider What Happensiff IF
Each of these Definitions Are Done within a function as opposed
To Globally Outside The Bounds of any function.
void my_function_a (char * ptr)
{
Char a [] = "abcde";
.
.
}
Void my_function_b (char * PTR)
{
Char * cp = "abcde";
.
.
}
Here We are dealing with automatic variables in baoth case.
In my_function_a the automatic variable is the character Array
a []. in my_function_b it is the pointer cp. While C Is Designed
In Such A Way That A Stack Is Not Required On Those Processors
Which don't use the the theme, my particular processor (80286) HAS A
Stack. I wrote a Simple Program Incorporating Functions Similar
To Those Above and Found That in my_function_a the 5 character
In The String Were All Stored on The Stack. on The Other Hand,
In my_function_b, the 5 Characters Were Stored in The Data Space
And The Pointer Was Stored on The Stack.
By Making A [] Static I Could Force the Compiler to Place THE
5 Characters in the data space as opposed to the stack. I Didthis Exercise to Point Out Just One More Difference Between
DEALING WITH ARRAYS AND DEALING WITH POINTERS. by The Way, Array
Initialization of Automatic Variables As I Have Done in
MY_FUNCTION_A WAS ILLEGAL in The Older K & R C and Only "CAME OF
AGE "in the net Ansi C. A Fact That May Be Important When ONE
IS considering portabilty and backwards compatability.
As long as we are discussing the rateship / different / Difference
Between Pointers and Arrays, Let's Move on To Multi-Dimensional
Arrays. consider, for example the arch:
Char Multi [5] [10];
Just What Does this mean? Well, Let's Consider IT IN T
Following Light.
Char Multi [5] [10];
^^^^^^^^^^^^^
IF We Take The First, Underlined, Part Above and Consider IT
To be a variable in its Own Right, We Have an Array of 10
Characters with the "name" Multi [5]. But this name, in itself,
Implies an array of 5 somethings. in Fact, IT means an array of
FIVE 10 Character Arrays. Hence We Have an Array of Arrays. in
Memory We might think of this as looking like:
Multi [0] = "0123456789"
Multi [1] = "abcdefghij"
Multi [2] = "abcdefghij"
Multi [3] = "9876543210"
Multi [4] = "jihgfedcba"
With individual elements being, for example:
Multi [0] [3] = '3'
Multi [1] [7] = 'h'
Multi [4] [0] = 'J'
Since Arrays Are To Be ContiGuous, Our Actual Memory Block
For the Above Should Look Like:
"0123456789abcdefghijabcdefghij9876543210jihgfedcba"
Now, The Compiler Knows How Many Column Are Present in The COLUMNS Aref
Array So IT CAN Interpret Multi 1 as The Address of The 'A' in
The 2nd Row Above. That IS, IT Adds 10, The Number of Columns, To Get this location. IF We wee dealing with integers and an
Array with The Same Dimension The Compiler Would Add
10 * Sizeof (int) Which, on My Machine, Would Be 20. Thus, The
Address of the "9" in The 4th Row Above Would Be & Multi [3] [0] OR
* (Multi 3) in Pointer Notation. To Get to the Content of The Content of To
2nd element in row 3 we add 1 to this address and dereference the
Result as in
* (* (Multi 3) 1)
WITH A Little Thought We can see That:
* (* (Multi Row) col) and
Multi [Row] [col] yield the same.
The Following Program Illustrates this using integer arrays
INSTEAD OF CHARACTER ARRAYS.
------------------- Program 6.1 ----------------------
#include
#define rows 5
#define cols 10
INT MULTI [ROWS] [COLS];
Int main (void)
{
Int row, col;
For (row = 0; row For (COL = 0; col Multi [ROW] [COL] = ROW * COL; For (row = 0; row For (COL = 0; col { Printf ("/ N% D", Multi [ROW] [COL]); Printf ("% d", * (* (multiple row) col); } Return 0; } ----------------- End of Program 6.1 --------------------- Because of the double de-referencing request in the point Pointer Version, The Name of A 2 Dimensional Array Is Said To Be a Pointer to a Pointer. with a Three Dimensional Array We Would Be DEALING WITH AN Array of Arrays of Arrays and a Pointer TO A Pointer to a Pointer. Note, However, That Here We since initially Set aside the block of memory for the array by defining it using Array Notation. Hence, WE Are Dealing with an all constant, not a Variable. That IS WE Are Talking About a fixed Pointer NOT AVARIABLE POINTER. The Dereferencing Function Used Above Permits Us to access any element in the array of arrays without the need Of Changing The Value of That Pointer (The Address of Multi [0] [0] As Given by the Symbol "Multi"). Epilog: I Have Written the Preceding Material to Provide An Introduction to Pointers for Newcomers To C. In C, The More One Understands About Pointers The Greater Flexibility One Has in The Writing of code. The Above Has Just Scratched The Surface of The Subject. in Time I Hope to Expand on this Material. Therefore, If You Have Questions, Comments, Criticism, etc. Concerning That Which Has Been Presented, I Would Greatly Appreciate Your Contacting me using one of the mail address in the mail Introduction. Ted Jensen