Pointer type declaration and assignment

We have seen that &qty returns the address of qty and this address can be stored in a variable as shown below:

ptr = &qty;

In C, every variable must be declared for its data type before it is used. Even this holds good for the pointers too. We know that ptr is not an ordinary variable like any integer variable. We declare the data type of the pointer variable as that of the type of the data that will be stored at the address to which it is pointing to. Since ptr is a variable, which contains the address of an integer variable qty, it can be declared as:

int *ptr;

where ptr is called a pointer variable. In C, we define a pointer variable by preceding its name with an asterisk(*). The “*” informs the compiler that we want a pointer variable, i.e. to set aside the bytes that are required to store the address in memory. The int says that we intend to use our pointer variable to store the address of an integer.

Consider the following memory map,

Pointer Assignment

Let us look into an example program

/* Program below demonstrates the relationships we have discussed so far */
# include <stdio.h>
main( )
{
int qty = 5;
int *ptr; /* declares ptr as a pointer variable that points to an integer variable */
ptr = &qty; /* assigning qty’s address to ptr -> Pointer Assignment */

printf ("Address of qty = %u \n", &qty);
printf ("Address of qty = %u \n", ptr);
printf ("Address of ptr = %u \n", &ptr);
printf ("Value of ptr = %d \n", ptr);
printf ("Value of qty = %d \n", qty);
printf ("Value of qty = %d \n", *(&qty));
printf ("Value of qty = %d", *ptr);
}

OUTPUT

Address of qty = 65524
Address of ptr = 65522
Value of ptr = 65524
Value of qty = 5
Value of qty = 5
Value of qty = 5

Let’s try another one as well,

/* Program that tries to reference the value of a pointer even though the pointer is uninitialized */
# include <stdio.h>
main()
{
int *p; /* a pointer to an integer */
*p = 10;
printf(“the value is %d”, *p);
printf(“the value is %u”,p);
}

This gives you an error. The pointer p is uninitialized and points to a random location in memory when you declare it. It could be pointing into the system stack, or the global variables, or into the program’s code space, or into the operating system. When you say *p=10; the program will simply try to write a 10 to whatever random location p points to. The program may explode immediately. It may subtly corrupt data in another part of your program and you may never realize it. Almost always, an uninitialized pointer or a bad pointer address causes the fault.

This can make it difficult to track down the error. Make sure you initialize all pointers to a valid address before dereferencing them.

Within a variable declaration, a pointer variable can be initialized by assigning it the address of another variable. Remember the variable whose address is assigned to the pointer variable must be declared earlier in the program. In the example given below, let us assign the pointer p with an address and also a value 10 through the *p.

Let us say,

int x; /* x is initialized to a value 10*/
p = &x; /* Pointer declaration & Assignment */
*p=10;
Let us write the complete program as shown below:
# include <stdio.h>
main( )
{
int *p; /* a pointer to an integer */
int x;
p = &x;
*p=10;
printf("The value of x is %d",*p);
printf("\nThe address in which the x is stored is %d",p);
}  

OUTPUT

The value of x is 10
The address in which the x is stored is 52004

This statement puts the value of 20 at the memory location whose address is the value of px. As we know that the value of px is the address of x and so the old value of x is replaced by 20. This is equivalent to assigning 20 to x. Thus we can change the value of a variable indirectly using a pointer and the indirection operator.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
?>