Movatterモバイル変換


[0]ホーム

URL:


OverIQ.com

  1. Home
  2. C Programming Tutorial
  3. Pointer Basics in C

Pointer Basics in C

Last updated on July 27, 2020


The real power of C lies in pointers. The pointers are slightly difficult to grasp at first. After going through the basics of pointers, you will get a better idea about what they are and how to use them.

What is a Pointer?#

A pointer is a variable used to store a memory address. Let's first learn how memory is organized inside a computer.

Memory in a computer is made up of bytes (A byte consists of8 bits) arranged in a sequential manner. Each byte has a number associated with it just like index or subscript in an array, which is called the address of the byte. The address of byte starts from0 to one less than the size of memory. For example, say in a 64MB of RAM, there are64 * 2^20 = 67108864 bytes. Therefore the address of these bytes will start from0 to67108863.

Let's see what happens when you declare a variable.

intmarks;

As we know anint occupies4 bytes of data (assuming we are using a 32-bit compiler) , so compiler reserves4 consecutive bytes from memory to store an integer value. The address of the first byte of the4 allocated bytes is known as the address of the variablemarks. Let's say that address of4 consecutive bytes are5004,5005,5006 and5007 then the address of the variable marks will be5004.

Address Operator (&)#

To find the address of a variable, C provides an operator called address operator (&). To find out the address of the variable marks we need to place& operator in front of it, like this:

&marks

The following program demonstrates how to use the address operator (&).

 1 2 3 4 5 6 7 8 91011121314
// Program to demonstrate address(&) operator#include<stdio.h>intmain(){inti=12;printf("Address of i = %u\n",&i);printf("Value of i = %d ",i);// signal to operating system program ran finereturn0;}

Expected Output:

12
Address of i = 2293340Value of i = 12

Note: Address ofi may vary every time you run the program.

How it works:

To find the address of the variable, precede the variable name by& operator. Another important thing to notice about the program is the use of%u conversion specification. Recall that%u conversion specification is used to print unsigned decimal numbers and since the memory addresses can't be negative, you must always use%u instead of%d.

Address of operator (&) can't be used with constants or expression, it can only be used with a variable.

12345
&var;// ok&12;// error because we are using & operator with a constant&(x+y)// error because we are using & operator with an expression</pre>

We have been using address operator(&) in the functionscanf() without knowing why? The address of a variable is provided toscanf(), so that it knows where to write data.

Declaring Pointer Variables#

As already said a pointer is a variable that stores a memory address. Just like any other variables you need to first declare a pointer variable before you can use it. Here is how you can declare a pointer variable.

Syntax:data_type *pointer_name;

data_type is the type of the pointer (also known as the base type of the pointer).
pointer_name is the name of the variable, which can be any valid C identifier. Let's take some examples:

12
int*ip;float*fp;

int *ip means thatip is a pointer variable capable of pointing to variables of typeint. In other words, a pointer variableip can store the address of variables of typeint only. Similarly, the pointer variablefp can only store the address of a variable of typefloat. The type of variable (also known as base type)ip is a pointer toint and type offp is a pointer tofloat. A pointer variable of type pointer to int can be symbolically represented as(int *). Similarly, a pointer variable of type pointer to float can be represented as(float *).

Just like other variables, a pointer is a variable so, the compiler will reserve some space in memory. All pointer variable irrespective of their base type will occupy the same space in memory. Normally4 bytes or2 bytes (On a 16-bit Compiler) are used to store a pointer variable (this may vary from system to system).

Assigning Address to Pointer Variable#

After declaring a pointer variable the next step is to assign some valid memory address to it. You should never use a pointer variable without assigning some valid memory address to it, because just after declaration it contains garbage value and it may be pointing to anywhere in the memory. The use of an unassigned pointer may give an unpredictable result. It may even cause the program to crash.

12345
int*ip,i=10;float*fp,f=12.2;ip=&i;fp=&f;

Hereip is declared as a pointer toint, so it can only point to the memory address of anint variable. Similarly,fp can only point to the address of afloat variable. In the last two statements, we have assigned the address ofi andf toip andfp respectively. Now,ip points to the variablei andfp points to variablef. It is important to note that even if you assign an address of afloat variable to anint pointer, the compiler will not show you any error but you may not get the desired result. So as a general rule always you should always assign the address of a variable to its corresponding pointer variable of the same type.

We can initialize the pointer variable at the time of declaration, but in this case, the variable must be declared and initialized before the pointer variable.

inti=10,*iptr=&i;

You can assign the value of one pointer variable to another pointer variable If their base type is same. For example:

12345
intmarks=100,*p1,*p2;p1=&marks;p2=p1;

After the assignment,p1 andp2 points to the same variablemarks.

As already said when a pointer variable is declared it containsgarbage value and it may be point anywhere in the memory. You can assign a symbolic constant calledNULL (defined instdio.h) to any pointer variable. The assignment ofNULL guarantees that the pointer doesn't point to any valid memory location.

123
inti=100,*iptr;iptr=NULL;

Dereferencing Pointer Variable#

Dereferencing a pointer variable simply means accessing data at the address stored in the pointer variable. Up until now, we have been using the name of the variable to access data inside it, but we can also access variable data indirectly using pointers. To make it happen, we will use a new operator called the indirection operator (*). By placing indirection operator (*) before a pointer variable we can access data of the variable whose address is stored in the pointer variable.

inti=100,*ip=&i;

Hereip stores address of variablei, if we place* beforeip then we can access data stored in the variablei. It means the following two statements does the same thing.

12
printf("%d\n",*ip);// prints 100printf("%d\n",i);// prints 100

Indirection operator (*) can be read as value at the address. For example,*ip can be read as value at addressip.

Note: It is advised that you must never apply indirection operator to an uninitialized pointer variable, doing so may cause unexpected behaviour or the program may even crash.

12
int*ip;printf("%d",*ip);// WRONG

Now we know by dereferencing a pointer variable, we can access the value at the address stored in the pointer variable. Let's dig a little deeper to view how the compiler actually retrieves data.

1234567
charch='a';inti=10;doubled=100.21;char*cp=&ch;int*ip=&i;double*ip=&d;

Let's say pointercp contains the address1000. When we write*cp the compiler knows that it has to retrieve information from the starting address1000. Now the question arises how much data to retrieve from starting address1000 ?1 bytes,2 bytes; What do you think ? To know how much information to retrieve from starting address1000, the compiler looks at the base type of pointer and will retrieve the information depending upon the base type of pointer. For example, if the base type is pointer tochar then1 byte of information from the starting address will be retrieved and if the base type pointer toint then4 bytes of information from the starting address will be retrieved. It is important to note that if you are on a system where the size ofint is2 bytes then2 bytes of information from the starting address will be retrieved.

So in our case, only1 byte of data from starting address will be retrieved. i.e the data stored at address2000 will be retrieved only.

Similarly, ifip points to the address2000. On writing*ip compiler will retrieve4 bytes of data starting from address 2000.

In the following image, shaded portion shows the number of bytes retrieved.

Before moving ahead, Interpret the meaning of the following expression:

*(&i) , wherei is a variable of typeint.

We know from the precedence table that parentheses() has the highest precedence, so&i is evaluated first. Since&i is the address of variablei, so dereferencing it with* operator will give us the value of the variablei. So we can conclude that writing*(&i) is same as writingi.

The following example demonstrates everything we have learned about pointers so far.

 1 2 3 4 5 6 7 8 910111213141516171819202122232425
#include<stdio.h>intmain(){inti=12,*ip=&i;doubled=2.31,*dp=&d;printf("Value of ip = address of i = %d\n",ip);printf("Value of fp = address of d = %d\n\n",d);printf("Address of ip = %d\n",&ip);printf("Address of dp = %d\n\n",&dp);printf("Value at address stored in ip = value of i = %d\n",*ip);printf("Value at address stored in dp = value of d = %f\n\n",*dp);// memory occupied by pointer variables// is same regardless of its base typeprintf("Size of pointer ip = %d\n",sizeof(ip));printf("Size of pointer dp = %d\n\n",sizeof(dp));// signal to operating system program ran finereturn0;}

Expected Output:

 1 2 3 4 5 6 7 8 9101112
Valueofip=addressofi=2686788Valueoffp=addressofd=1202590843Addressofip=2686784Addressofdp=2686772Valueataddressstoredinip=valueofi=12Valueataddressstoredindp=valueofd=2.310000Sizeofpointerip=4Sizeofpointerdp=4

Note: Memory address may vary every time you run the program.

There is nothing new in the above program that deserves any explanation. Before we proceed to next chapter, always remember that the size of the pointer variables is same regardless of its base type but the size of the memory address that will be accessed while dereferencing depends on upon the base type of the pointer variable.


Load Comments


C Programming Tutorial

Recent Posts

x

[8]ページ先頭

©2009-2025 Movatter.jp