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

you can also declare the pointers as

How Pointer work?

Lets write an example to demonstrate the use of pointers.

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.

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

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

25 Comments

  1. 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

    • 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

    • change this to pointer declaration.

    • hi im rakesh from patna university
      just translate 1-d array into pointers like:-
      a[i] = *(a+i)
      and convert the 2d array to pointers like :-
      a[i][j]=*(*(a+i)+j)

  2. source code of pointers used in arrays

  3. 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…

  4. 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….

  5. 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;

    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.

  6. 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:

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

    -rejie

Leave a Reply