Sunday, 21 January 2018

Basic Pointers


Basic Pointers


Basic Pointers
Pointer Definition: Pointer is a variable which contains the address of another variable.Reasons for using pointer:
1.A pointer enables us to access a variable that is
defined outside the function.
2.Pointers are more efficient in handling the data tables.
3.Pointers reduce the length and complexity of a program.
4.They increase the execution speed
5.The use of pointer array to character strings results
in saving of data storage space in memory.

Representation of a variable:Consider the following statement
       int *p;
       int quantity=179;
       p=&quantity;
     Here p is a pointer variable of type integer,which holds the address of the variable quantity.

Declaring and initializing pointers:In c,every variable must be declared for its type since
pointer variable contain address that belong to a seperate data type, they must be declared as pointers before we use them.The declaration of a pointer variable takes the
following form data type *pt-name;
   This tells the compiler three things about the variable pt-name
1.The asterick(*) tells that the variable pt-name is a  pointer variable.
2.pt-name needs a memory location.
3.pt-name points to a variable of type data type.

for example,
   int *p;
   declares the variable p as apointer variable that points to an integer data type.Remember that the type int refers to the data type of the variable being pointed to by p and not the type of the value of the pointer.
   Program to print the address of a variable along with its value.

main()
{
   char a;
   int x;
   float p,q;
   a='A';
   x=125;
   p=10.25,q=18.76;
   printf("%c is stored at addr %u \n",a,&a);
   printf("%d is stored at addr %u \n",x,&x);
   printf("%f is stored at addr %u \n",p,&p);
   printf("%f is stored at addr %u \n",q,&q);
}
Output:
A is stored at addr 4436
125 is stored at addr 4434
10.250000 is stored at addr 4442
18.760000 is stored at addr 4438.

Accessing variables using pointers

main( )
{
   int x,y;
   int *ptr;
   x=10;
   ptr=&x;
   y=*ptr;
   printf("value of x is %d\n\n",x);
   printf("%d is stored at addr %u\n",x,&x);
   printf("%d is stored at addr %u\n",*&x,&x);
   printf("%d is stored at addr %u\n",*ptr,ptr);
   printf("%d is stored at addr %u\n",y,&*ptr);
   printf("%d is stored at addr %u\n",ptr,&ptr);
   printf("%d is stored at addr %u\n",y,&y);
   *ptr=25;
   printf("\n Now x=%d\n",x);
}

Ouput:
value of x is 10
10 is stored at addr 4104
10 is stored at addr 4104
10 is stored at addr 4104
10 is stored at addr 4104
4104 is stored at addr 4106
10 is stored at addr 408.     Now x=25.

Related Posts:

  • Putting Strings TogetherPutting Strings TogetherWe cannot assign one string to another directly We cannot join two strings together by the simple arithmetic addition.       string3 = string1+string2;       str… Read More
  • Array Of Pointers To StringsArray Of Pointers To StringsAs we know , a pointer variable always contains an address. Therefore , if we construct an array of pointers it would contain a number of addresses.Let us see how the names in the earlier example c… Read More
  • String Handling Functions In CString Handling Functions In CWith every C compiler a large set of useful string handling library functions are provided. The following figure illustrates the more commonly used functions along with theirpurpose.1.strlen: Fin… Read More
  • Programs In String Handling FunctionsPrograms In String Handling Functions1.Is the folowing program correct or not?   yes/nomain(){  char *str1 = "United";  char * str2 = "Front";  char  *str3;  str3 = strcat(str1, str2); … Read More
  • Arithmetic Operations On CharactersArithmetic Operations On Characters'C' allows us to manipulate characters in the same way we do with numbers.Whenever a character constant or character variable is used in an expression , it is automatically converted to inte… Read More

0 comments:

Post a Comment