C and C++ Programming Resources

Custom Search

Pointers

Each memory location that we use to store the data hase an address in computre memory (RAM). Computer Hardware i.e. CPU uses this addess to reference to a particular data item. A pointer is a variable that stores the address of another variable.

A pointer is a variable that represents the location of a data item, such as a variable or an array element. Pointers are used frequently in C, as they have a number of useful applications. For example, pointers can be used to pass information back and forth between a function and its reference point. Pointers provide a way to return multiple data items from a function via function arguments to
be specified as arguments to a given function.

Pointers are also closely associated with arrays and therefore provide an alternate way to access individual array elements.

Within the computer?s memory, every stored data item occupies one or more adjacent memory cells. The number of memory cells required to store a data item depends on the type of data item. For example, a single character will be stored in 1 byte of memory integer usually requires two adjacent bytes, a floating point number may require four adjacent bytes.

Declaring a Pointer

A pointer is declared just like we declare a variable. There is only one difference in declaration is that we add an asterisk * infront of it to indicate that it’s a variable which is a pointer. For example

int* i;
long* y;

you can also declare the pointers as

int *i;
long *y;

How Pointer work?

Lets write an example to demonstrate the use of pointers.

int num = 10;
int *pnum;
pnum = #

Now pnum will be having the address of the num variable.

Initializing pointers

It is often a good practise to intialize the variable while declaring. It is very easy to initialize a pointer to the address of a variable that has already
been defined.

int num=10;
int* pnum= #

You can also initialize the pointer to the default null value.

int* pnum = NULL;

Here is a simple example that demonstrates the different aspects of the pointer operations.

#include <stdio.h>
int main(void){
	int num=10;
	int* pnum=NULL;
	pnum = &num;
	*pnum += 20;
	printf("\nNumber = %d", num);
	printf("\nPointer Number = %d", *pnum);
	return 0;
}

Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4]

Tags: , , , ,

There are 25 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.

  • Megha Vasani says:

    I ahve declare 5 to 6 pointer arrays and after completing all the operation with them i want to free all the ocupied memory so i am using delete method but it shows error in executing!
    Give me the solution for this error

    • sanjay says:

      Use p = new T[n] and delete[] p:

      Fred* p = new Fred[100];

      delete[] p;

      Any time you allocate an array of objects via new (usually with the [n] in the new expression), you must use [] in the delete statement. This syntax is necessary because there is no syntactic difference between a pointer to a thing and a pointer to an array of things

      ——– Original Message ——–
      I ahve declare 5 to 6 pointer arrays and after completing all the operation with them i want to free all the ocupied memory so i am using delete method but it shows error in executing!
      Give me the solution for this error

  • ulaganaathan says:

    Explain the above mentioned topics

  • tsegay says:

    Please would you giving me the book which deal on
    “pointer array, pointer to array, pointer to function”

  • nob says:

    What is the unique idea of making histogram with the help of c program?

  • B.BAJIBABAU says:

    SIR,
    I AM STUDENT OF IIIT HYDERABAD. I WANT TELL TO YOU THAT THERE IS SOME SYANTAX ERRORS IS THERE PLEASE YOU CAN MINIMIZES THEM.
    THANK YOU.

  • karthickbabu says:

    Hi i want to know know structure variable passed to a function and that function how that structure variable used

    plz tell me

  • ravi says:

    I need to know about pointers to string

    • topherpoh says:
            #include <stdio.h>
      
            void sort(int x[], int n);
            int main(void)
            {
      	int i;
      	int num[] = {4,5,1,6,8,9,3,2,7,0};
      	int n = sizeof(num)/sizeof(int);
      	sort(num, n);
      	for(i = 0; i < n; i  )
      		printf("%d", num[i]);
      	putchar('
      ');
      	return 0;
            }
      
            void sort(int array[], int n)
            {
      	int this, next, temp;
      
      	for(this = 0; this < n; this  )
      	   for(next = this   1; next < n; next  )
      	      if(array[this] > array[next])
      	      {
      		temp = array[this];
      		array[this] = array[next];
      		array[next] = temp;
      	      }
            }
      

      change this to pointer declaration.

  • Humayun says:

    I want to know how functions work and how they are used ?
    Please mail me. humayun0156 at yahoo dot com

  • Upendra says:

    Hi, give some idea about pointer to a function.

  • aman says:

    source code of pointers used in arrays

    /*programme to sort number of strings by sorting their locations not their values */
    void main()
    {
    char a[10][10],*p[10],temp[30];
    printf("enter the strings to sort:
    ");
    for(i=0;i<7;i++)
    gets(a[i]);
    for(i=0;i<7;i++)
    p[i]=&a[i][0];
    for(i=0;i<7;i++)
    for(j=0;j<(7-i);j++)
    {  if(strcmp(p[j],p[j+1])>0)
    {  strcpy(temp,p[j]);
       strcpy(p[j],p[j+1]);
       strcpy(p[j+1],temp);
    } }
    printf("the sorted strings are:
    ");
    for(i=0;i<7;i++)
    puts(p[i]);
    getch();
    }
  • Ii want 2 know how can i calculate the prelim midterm ,pre final and final..this the example of formula he give my prof prelim…quiz#1 90×20%,quiz#2 80×10%..attendance 80×20%…then the total of pre lim ,midterm ,pre final and final is calculate …then the final grade is show 2 my c++ program…i hope any one give me example of my program…thx a lot…

  • erika says:

    Can all of you give me a sample program or source code about pointers used in arrays….plssssss….plsssss…….

  • Priya says:

    Given an expression consisting of operands a, b, c, d…. and operators and *. The expression is fully parenthesized. Write a program to evaluate the expression, given the values of a,b,c,d…
    Define a function, char *get_first_subexprn(char *e) which returns the leftmost innermost subexpression ( of the form (operand1 operator operand2) )in e where e is a fully parenthesized expression .
    Define a function int evaluate(char * e) which evaluates and returns the value of the expression e. Assume e is of the form (operand1 operator operand2). The operands may be constants or variables.
    Your algorithm should repeatedly find the first innermost parenthesized expression and replace it by the value of the sub expression until you get the value of the expression.

    This is a question i want to solve….
    please help….

  • AjusAjuddy says:

    I have searched for any topic on sort but was unable to find. however i got a question,
    how does one call function to a sort(selection) for instance if you have such a code as;

    #include<stdio.h>
    void select(int number [], int count)
    {
       int i, j;
       int min, temp;
       for(i=0; i<count-1; i++){
    		min=i;
       		for(j=i+1; j<count; j++){
    		   if(number[j]<number[min])
    			   min=j;
    		   temp=number[i];
    		   number[i]=number [min];
    		   number [min]=temp;
      		 }
    	}
    }
    
    main()
    {
       int x=0;
       int num; {'1','5','4','3','6','8','9','0','2','7';}
       //num=number;
    
       for(x=1; x<9; x++)
    	   printf("number %d", num);
    	}
    

    now the big question is, i seem to be having a problem with calling the function and also with the printf. can sumbody can you help with pointing out what is expected of me. thank u.

  • Hemant says:

    Hay Sir
    This is very nice way to Undersoot us

    Thank u sir

  • Hi This is Santhosh,

    Can any body explain How pointer accessing method is more faster than array indexing?

    Regards,
    Santhosh

  • tererai says:

    i am having a problem in coming up with the recursive algorithm of the binomial theorem.Could you pliz heelp me.

  • rejie says:

    Hello. I just want to know how to initiate this type of program using pointer. And here is the sample output:

    ————————————–
    Enter Words:Adamson -
    -
    Letter ‘a’ appears at: 2 -
    Letter ‘e’ appears at: 0 -
    Letter ‘i’ appears at: 0 –
    Letter ‘o’ appears at: 1 –
    Letter ‘u’ appears at: 0 -
    -
    ————————————–

    Here is my initial program using String and Gets. Its near similar but isn’t a program that i ask:

    #include
    #include
    #include
    
    main()
    {
    
    char s1[20], *p;
    printf("Enter word:");
    gets(s1);
    p=s1;
    printf("
    
     vowels in the input word:
    ");
    while(*p){
    if(*p=='a'||*p=='e'||*p=='i'||*p=='o'|*p=='u')
    printf("%c",%p);
    p++;
    }
    getch();
    }
    

    Please i need your help because i was reviewing for our prelim exam. Thanks and more power.

    -rejie

  • sir plz tell me a program implemaintation of stack using pointer

  • kamikaze says:

    i like to create a function like strlen.it will count the number of characters in a string…using pointers i would like to know how.tnc

  • sharan says:

    i am watting for pro


Leave a Reply

You must be logged in to post a comment.