Skip to main content

Pointers


A pointer variable is a variable that holds the memory address of another variable. They are called pointers for the simple reason that, by storing an address, they point to a particular location in memory.
At the moment when a variable is declared, it must be stored in a concrete memory location. The programs do not decide where the variable is to be placed. That is done automatically by the compiler and the operating system at run time.
The declaration  int x=3  tells the C compiler to
  •        Reserve space in memory to hold the integer value.(location/address is decided by the compiler/os)
  •        Associate the name x with this memory location.
  •         Store the value 3 in this location.
The space allocated can be obtained with
printf(“%d”,sizeof(x));

The value stored can be obtained with
printf(“%d”,x);

The memory location or address can be obtained with
printf(“%u”,&x); // &x  is the address of the variable x.

The pointer variable is declared with *(indirection) operator.
Eg:
int *p; creates a pointer variable p which can store the address of an integer variable.
p=&x ; now p contains address of integer variable x ie; p points to variable x.

Because a pointer holds an address rather than a value, it has two parts.
The pointer itself holds the address.
The address points to a value.

printf(“%u”,p) will print the address it holds
printf(“%d”,*p) will  print the value it points to

pointers can be used to
  • Build faster and efficient code.
  •  Alternate/efficient way of accessing information stored in variables and arrays.
  •  Pass arrays and strings more conveniently from one function to another.
  •  Return more than one value from a function
  •  Dynamic memory allocation.
  •  Build complex data structures.( linked lists, trees etc..)
The following program will illustrate the pointer indirection operator and dereferencing
#include <stdio.h>
 int main()
{
 int num=5;
 int *ptr=&num;
printf(“the value and address of num is %d  %u”,num,&num);
printf(“the value and address of num using pointer ptr is %d  %u”,*ptr,ptr);
*ptr=15; // indirection, which stores value in variable num using the pointer ptr.
printf(“the value of num after indirect initialization is %d”,num);
}

Comments

Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …