Introduction To C Language

The C language is widely used because it provides the programmer with the power of low-level languages and the flexibility and simplicity of a high-level language.

In C there is a feature that enables programmers to access memory addresses directly.

Objectives

  • About pointers
  • Declares and manipulate pointers
  • Use pointers to manipulate character arrays
  • Article Summary

About Pointers

A pointer can be defined as a variable containing the address of another variable.

ptr =&x;

Where the new variable “ptr” is called a pointer because it points to the location where x is stored in memory. Therefore, “ptr” refers to x's address and not its value.

Declaring Pointers

Pointer variables must be declared before use in a program. When a pointer variable is declared, the variable name must be preceded by an asterisk (*).The pointer must also be declared to be of some data type. This data type, however, refers to the data type of the variables to which the pointer points.

int *ptr;

Indicates that “ptr” is a pointer to an integer variable.

Examples

  1. float *ptr_to_float;

    The pointer variables ptr_to_float is pointing to a variable of what data type?

  2. Is the following declaration valid?
    1. *ptr_to_something; 
  3. State whether True or False
    1. int *ptr_to_int; 
    An integer is being declared.

  4. Is the declaration
    1. Int some_int,*ptr_to_int; 
    Valid?

Answers

  1. Float

  2. No. When a pointer variable is being declared, the type of variable to which it is pointing to (int, float, or char) should also be indicated.

  3. False. A pointer to an integer is being declared and not an integer.

  4. Yes. It is okay to club declaration of a certain type along with pointers to the same type.

Manipulating Pointers

Where in the preceding we saw that “ptr” was pointing to x, in other words “ptr” contained the address of the location in which x is stored. The data item represented by x can be accessed using the unary operator *, that is also called the indirection operator that can be used only on pointers.

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. main()  
  4. {  
  5.    clrscr();  
  6.    int x,y,z,*ptr;  
  7.    x=34;  
  8.    y=67;  
  9.    z=20;  
  10.    printf("x=%d,y=%d,z=%d\n",x,y,z); //output x=34,y=67,z=20  
  11.    ptr=&x; //ptr now points to x  
  12.    y=*ptr; //y gets the value of variable to which ptr points i.e x  
  13.    printf("x=%d,y=%d,z=%d",x,y,z); //output x=34,y=34,z=20  
  14.    getch();  
  15.    return 0;  

Manipulating Pointers

The following are some questions and answers:

  1. The address operator "&" obtains the address of a variable whereas the indirection operator "*" obtains the value of the variable to which the pointer is pointing to.

  2. With the following declarations:

    int x, y, *ptr;

    Which of the following are valid assignments?

    1. x=y;
    2. y=*ptr;
    3. x=ptr;
    4. x=&ptr;
    5. ptr=&x;
    6. x=&y;

  3. Consider the following sequence of statements and complete the partially-filled table:
    1. Int x,y,*ptr1, *ptr2;  
    2. X=65;  
    3. Y=89;  
    4. Ptr1=&x; /*ptr1 points to x*/  
    5. Ptr2=&y; /*ptr2 points to y */  
    6. X=*ptr1; /*statement A */  
    7. Ptr1=ptr2; /* Statement B */  
    8. X=*ptr1; /*statement C */ 
    After statement &x x &y y ptr1 ptr2

    A
    B
    C

  4. What is the output of the following sequence of statements:
    1. int x, y, temp, *ptr1, * ptr2; /* declare*/  
    2. x=23;  
    3. y=37;  
    4. ptr1=&x; /*ptr1 points to x*/  
    5. ptr2=&y; /* ptr2 points to y */  
    6. temp=*ptr1;  
    7. *ptr1=*ptr2;  
    8. *ptr2=temp;  
    9. printf(“x is %d while y is %d, x, y); 
    Answers:

    & , *
    1. Meaningful
    2. Meaningful
    3. Not meaningful, x is of type int whereas ptr is a pointer to an int
    4. Not meaningful, x is of type int whereas &ptr is the address of a pointer to an int
    5. Meaningful
    6. Not meaningful, x is of type int whereas &y is an address (and thus a pointer)

      After statement

      table

      The preceding statement observes that &x and &y are fixed values.

      When statement A is executed, ptr1 is pointing to x. Thus, x will be set to itself.

      The output will be:

      x is 37 whereas y is 23

      It might seem to be a long-winded method of swapping two variables.
      1. #include<stdio.h>  
      2. #include<conio.h>  
      3. main()  
      4. {  
      5.    clrscr();  
      6.    int x,y,*ptr1,*ptr2;  
      7.    x=65;  
      8.    y=89;  
      9.    ptr1=&x;  
      10.    ptr2=&y;  
      11.    printf("x is %d and y is %d", x, y);  
      12.    x=*ptr1;  
      13.    ptr1=ptr2;  
      14.    x=*ptr1;  
      15.    printf("x is %d and y is %d",x,y);  
      16.    getch();  
      17.    return 0;  


      swapping

Pointer Arithmetic

Arithmetic operators of incrementing and decrementing can be done on pointers. In fact, pointer arithmetic is one of the reasons why it is essential to declare a pointer as pointing to a certain datatype so that when the pointer is incremented or decremented, it moves by the appropriate number of bytes.

ptr++;

Does not necessarily mean that ptr now points to the next memory location. What memory location it will point to will depend upon the datatype to which the pointer points.

Example

Example

String-Handling Functions Using Pointers

See the following examples:

Examples below

Summary

  • A pointer is a variable that contains the address of some other variable in memory.
  • A pointer may point to a variable of much data type.
  • A pointer can point to any position of memory.
  • A pointer variable is declared as:

    datatype *<pointer variable name>

  • A pointer variable is initialized as:

    <pointer variable name>= &<variable name to which the pointer will point to>

  • The & returns the address of the variable.
  • The * before a pointer name gives the value of the variable to which it is pointing.
  • Pointers can also be subjected to arithmetic.
  • Incrementing a pointer by 1 makes it point to a memory location given by the formula:
  • New address = Old address + Scaling Factor
  • One-dimensional character arrays can be declared by declaring a pointer and initializing it.
  • The name of a character array is actually a pointer to the first element of the array.
  • Two-dimensional character arrays can also be declared using pointers.


Similar Articles