Home arrow C programming arrow A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 1

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 1 PDF Print E-mail
Written by Hemanshu   
Saturday, 29 December 2007
Article Index
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 1
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9

The actual bit pattern used for a null pointer may or may not evaluate to zero since it depends on the specific system on which the code is developed. To make the source code compatible between various compilers on various systems, a macro is used to represent a null pointer. That macro goes under the name NULL. Thus, setting the value of a pointer using the NULL macro, as with an assignment statement such as ptr = NULL, guarantees that the pointer has become a null pointer. Similarly, just as one can test for an integer value of zero, as in if(k == 0), we can test for a null pointer using if (ptr == NULL).
But, back to using our new variable ptr. Suppose now that we want to store in ptr the address of our integer variable k. To do this we use the unary & operator and write:
      ptr = &k;
What the & operator does is retrieve the lvalue (address) of k, even though k is on the right hand side of the assignment operator ' , and copies that to the contents of our pointer ptr. Now, ptr is said to "point to" k. Bear with us now, there is only one more operator we need to discuss.


The "dereferencing operator" is the asterisk and it is used as follows:


      *ptr = 7;


will copy 7 to the address pointed to by ptr. Thus if ptr "points to" (contains the address of) k, the above statement will set the value of k to 7. That is, when we use the ' this way we are referring to the value of that which ptr is pointing to, not the value of the pointer itself.


Similarly, we could write:


  printf("%d\n",*ptr);


to print to the screen the integer value stored at the address pointed to by ptr;.
One way to see how all this stuff fits together would be to run the following program and then review the code and the output carefully.


------------ Program 1.1 ---------------------------------
/* Program 1.1 from PTRTUT10.TXT               6/10/97 */
#include <stdio.h>
int 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, (void *)&j);
     printf("k has the value %d and is stored at %p\n", k, (void *)&k);
     printf("ptr has the value %p and is stored at %p\n", ptr, (void
*)&ptr);
     printf("The value of the integer pointed to by ptr is %d\n", *ptr);
     return 0;
}
Note: We have yet to discuss those aspects of C which require the use of the (void *) expression used here. For now, include it in your test code. We' explain the reason behind this expression later.
To review:
       A variable is declared by giving it a type and a name (e.g. int k;)
   •
       A pointer variable is declared by giving it a type and a name (e.g. int *ptr) where
   •
       the asterisk 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 (integer in this
       case).
       Once a variable is declared, 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).


References for Chapter 1:
   1. "The C Programming Language" 2nd Edition
       B. Kernighan and D. Ritchie
       Prentice Hall
       ISBN 0-13-110362-8


Last Updated ( Saturday, 29 December 2007 )
 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits