0% found this document useful (0 votes)
4 views

12

The document explains the relationship between pointers and arrays in C, detailing how arrays are stored in contiguous memory and how pointers can be used to access array elements. It also covers pointers to structures, including how to access structure members using pointer syntax. Additionally, it highlights the importance of managing array bounds and operator precedence when working with pointers and structures.

Uploaded by

bhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

12

The document explains the relationship between pointers and arrays in C, detailing how arrays are stored in contiguous memory and how pointers can be used to access array elements. It also covers pointers to structures, including how to access structure members using pointer syntax. Additionally, it highlights the importance of managing array bounds and operator precedence when working with pointers and structures.

Uploaded by

bhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Pointers and Arrays

1
Pointers and Arrays

 When an array is declared,


 The compiler allocates sufficient amount of
storage to contain all the elements of the array in
contiguous memory locations
 The base address is the location of the first
element (index 0) of the array
 The compiler also defines the array name as a
constant pointer to the first element

2
Example
 Consider the declaration:
int x[5] = {1, 2, 3, 4, 5};
 Suppose that each integer requires 4 bytes
 Compiler allocates a contiguous storage of size 5x4 =
20 bytes
 Suppose the starting address of that storage is 2500

Element Value Address


x[0] 1 2500
x[1] 2 2504
x[2] 3 2508
x[3] 4 2512
x[4] 5 2516
3
Contd.
 The array name x is the starting address of the
array
 Both x and &x[0] have the value 2500
 x is a constant pointer, so cannot be changed
 X = 3400, x++, x += 2 are all illegal

 If int *p is declared, then


p = x; and p = &x[0]; are equivalent
 We can access successive values of x by using
p++ or p-- to move from one element to another

4
 Relationship between p and x:
p = &x[0] = 2500
p+1 = &x[1] = 2504 In general, *(p+i) gives
p+2 = &x[2] = 2508 the value of x[i]
p+3 = &x[3] = 2512
p+4 = &x[4] = 2516
 C knows the type of each element in array x, so
knows how many bytes to move the pointer to
get to the next element

5
Example: function to find
average
int main()
{
int x[100], k, n;
float avg (int array[], int size)
scanf (“%d”, &n); {
int *p, i , sum = 0;
for (k=0; k<n; k++)
scanf (“%d”, &x[k]); p = array;

printf (“\nAverage is %f”, for (i=0; i<size; i++)


avg (x, n)); sum = sum + *(p+i);
return 0;
} return ((float) sum / size);
}

6
The pointer p can be subscripted
also just like an array!
int main()
{
int x[100], k, n;
float avg (int array[], int size)
scanf (“%d”, &n); {
int *p, i , sum = 0;
for (k=0; k<n; k++)
scanf (“%d”, &x[k]); p = array;

printf (“\nAverage is %f”, for (i=0; i<size; i++)


avg (x, n)); sum = sum + p[i];
return 0;
} return ((float) sum / size);
}

7
Important to remember
 Pitfall: An array in C does not know its own length, &
bounds not checked!
 Consequence: While traversing the elements of an array (either
using [ ] or pointer arithmetic), we can accidentally access off the
end of an array (access more elements than what is there in the
array)
 Consequence: We must pass the array and its size to a function
which is going to traverse it, or there should be some way of
knowing the end based on the values (Ex., a –ve value ending a
string of +ve values)
 Accessing arrays out of bound can cause strange
problems
 Veryhard to debug
 Always be careful when traversing arrays in programs
8
Pointers to
Structures

9
Pointers to Structures
 Pointer variables can be defined to store the
address of structure variables
 Example:
struct student {
int roll;
char dept_code[25];
float cgpa;
};
struct student *p;

10
 Just like other pointers, p does not point to
anything by itself after declaration
 Need to assign the address of a structure to p
 Can use & operator on a struct student type variable
 Example:

struct student x, *p;


scanf(“%d%s%f”, &x.roll, x.dept_code, &x.cgpa);
p = &x;

11
 Once p points to a structure variable, the
members can be accessed in one of two ways:
 (*p).roll, (*p).dept_code, (*p).cgpa
 Note the ( ) around *p
p –> roll, p –> dept_code, p –> cgpa
 The symbol –> is called the arrow operator
 Example:
 printf(“Roll = %d, Dept.= %s, CGPA = %f\n”, (*p).roll,
(*p).dept_code, (*p).cgpa);
 printf(“Roll = %d, Dept.= %s, CGPA = %f\n”, p->roll,
p->dept_code, p->cgpa);

12
Pointers and Array of Structures
 Recall that the name of an array is the address
of its 0-th element
 Also true for the names of arrays of structure
variables
 Consider the declaration:

struct student class[100], *ptr ;

13
Pointers and Array of Structures
 Recall that the name of an array is the address
of its 0-th element
 Also true for the names of arrays of structure
variables
 Consider the declaration:

struct student class[100], *ptr ;

14
 The name class represents the address of the 0-th
element of the structure array
 ptr is a pointer to data objects of the type struct
student
 The assignment
ptr = class;
will assign the address of class[0] to ptr
 Now ptr->roll is the same as class[0].roll. Same for
other members
 When the pointer ptr is incremented by one (ptr++) :
 The value of ptr is actually increased by
sizeof(struct student)
 It is made to point to the next record
 Note that sizeof operator can be applied on any
data type
15
struct student { Output
char name[20]; 3
int roll; Ajit 1001
} Abhishek 1005
int main() Riya 1007
Ajit 1001
{ Ajit 1001
struct student class[50], *p; Ajit 1001
int i, n; Ajit 1001
scanf(“%d”, &n); Abhishek 1005
for (i=0; i<n; i++) Abhishek 1005
Abhishek 1005
scanf(“%s%d”, class[i].name, &class[i].roll); Abhishek 1005
p = class; Riya 1007
for (i=0; i<n; i++) { Riya 1007
printf(“%s %d\n”, class[i].name, class[i].roll); Riya 1007
printf(“%s %d\n”, *(p+i).name, *(p+i).roll); Riya 1007
printf(“%s %d\n”, (p+i)->name, (p+i)->roll);
printf(“%s %d\n”, p[i].name, p[i].roll);
}
16
}
A Warning
 When using structure pointers, be careful of operator
precedence
 Member operator “.” has higher precedence than “*”
 ptr –> roll and (*ptr).roll mean the same
thing
 *ptr.roll will lead to error

 The operator “–>” enjoys the highest priority


among operators
 ++ptr –> roll will increment ptr->roll, not ptr
 (++ptr) –> roll will access (ptr + 1)->roll (for
example, if you want to print the roll no. of all elements of
the class array)
 When not sure, use ( and ) to force what you you want
17
Practice Problems
 Look at all problems you have done earlier on
arrays (including arrays of structures). Now
rewrite all of them using equivalent pointer
notations
 Example: If you had declared an array
int A[50]
Now do
int A[50], *p;
p = A;
and then write the rest of the program using the
pointer p (without using [ ] notation)

18

You might also like