1c Pointers in C

Explain with suitable examples, how & and * operators are used with pointer

variable. (3)

 

1. & Operator (Address Operator)

The & operator is used to obtain the address of a variable in memory.

Example

#include <stdio.h>

int main()
{
int a = 10;
printf(“Value of a = %d\n”, a);
printf(“Address of a = %p”, &a);
return 0;
}

Explanation

  • a → gives the value stored in the variable
  • &a → gives the memory address of the variable

Example output (address varies):

Value of a = 10
Address of a = 6422296

 

2. * Operator (Indirection / Dereference Operator)

The * operator is used to:

  1. Declare a pointer variable
  2. Access the value stored at the address held by the pointer

Example

#include <stdio.h>

int main()
{
int a = 10;
int *p;

p = &a;   // pointer stores address of a

printf(“Address of a = %p\n”, p);
printf(“Value of a = %d”, *p);

return 0;
}

Explanation

  • int *p; → declares a pointer to an integer
  • p = &a; → stores the address of a in pointer p
  • *p → accesses the value stored at that address

Output:

Address of a = 6422296
Value of a = 10

 

Summary

Operator Name Purpose
& Address Operator Returns the memory address of a variable
* Indirection / Dereference Operator Accesses the value stored at the address

Example relationship:

a = 10
&a → address of a
p = &a
*p → value of a (10)

 

 

 

Write a C program using pointers to compute the Sum and Mean of all elements

stored in an array of n real numbers.(8)

 

This program reads n real numbers into an array, then uses a pointer to traverse the array and calculate the sum and mean.

#include <stdio.h>

int main()
{
int n, i;
float arr[100], sum = 0, mean;
float *ptr;

printf(“Enter the number of elements: “);
scanf(“%d”, &n);

printf(“Enter %d real numbers:\n”, n);
for(i = 0; i < n; i++)
{
scanf(“%f”, &arr[i]);
}

ptr = arr;   // pointer points to the first element of the array

for(i = 0; i < n; i++)
{
sum = sum + *(ptr + i);   // accessing array elements using pointer
}

mean = sum / n;

printf(“Sum = %.2f\n”, sum);
printf(“Mean = %.2f\n”, mean);

return 0;
}

Explanation

  1. An array arr stores n real numbers.
  2. A pointer ptr is assigned the address of the first element of the array (ptr = arr).
  3. The expression *(ptr + i) accesses the ith element of the array using pointer arithmetic.
  4. The program calculates:
    • Sum of all elements
    • Mean = Sum / n

Sample Output

Enter the number of elements: 4
Enter 4 real numbers:
2.5
3.5
4.0
5.0
Sum = 15.00
Mean = 3.75

 

 

Explain how a pointer is assigned with a structure in C and how the member

variable of structure is accessed using pointer?(3)

 

A pointer to a structure is a pointer variable that stores the address of a structure variable. It allows access to the structure members through the pointer.

 

1. Assigning a Pointer to a Structure

A pointer is assigned the address of a structure variable using the address operator (&).

Syntax

struct structure_name *pointer_name;
pointer_name = &structure_variable;

Example

#include <stdio.h>

struct student
{
int roll;
float marks;
};

int main()
{
struct student s1;
struct student *ptr;

ptr = &s1;   // pointer assigned the address of structure

return 0;
}

Here:

  • s1 is a structure variable.
  • ptr is a pointer to the structure.
  • ptr = &s1 stores the address of s1.

 

2. Accessing Structure Members Using Pointer

Structure members can be accessed in two ways when using pointers.

(a) Using Dereference Operator *

(*ptr).roll = 10;
(*ptr).marks = 85.5;

Here:

  • *ptr accesses the structure.
  • .roll and .marks access the members.

 

(b) Using Arrow Operator ->

The arrow operator is a shortcut for accessing structure members through pointers.

ptr->roll = 10;
ptr->marks = 85.5;

ptr->roll is equivalent to (*ptr).roll.

 

Complete Example Program

#include <stdio.h>

struct student
{
int roll;
float marks;
};

int main()
{
struct student s1;
struct student *ptr;

ptr = &s1;

ptr->roll = 101;
ptr->marks = 89.5;

printf(“Roll: %d\n”, ptr->roll);
printf(“Marks: %.2f\n”, ptr->marks);

return 0;
}

 

Output

Roll: 101
Marks: 89.50

 

Key Points

  • A structure pointer stores the address of a structure variable.
  • Members can be accessed using
    • (*ptr).member
    • ptr->member (more commonly used).