11 - Pointers-Arrays-Structures
11 - Pointers-Arrays-Structures
1
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
Pointers & Arrays
2
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
Pointers and Arrays
3
Example
• Suppose that the base address of x is 2500, and each integer requires 4 bytes.
4
Example (contd)
Both x and &x[0] have the value 2500.
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.
5
Arrays and pointers
6
Arrays
Consequences:
• ar is a pointer
• ar[0] is the same as *ar
• ar[2] is the same as *(ar+2)
• We can use pointer arithmetic to access arrays more
conveniently.
8
String Constant
•A string constant is treated as a pointer
•Its value is the base address of the string
char *p = “abc”;
p a b c \0
9
Arrays In Functions
An array parameter can be declared as an array or a pointer; an array argument can be
passed as a pointer
} }
10
Arrays and pointers
When an array is declared the compiler allocates a sufficient amount of contiguous space in
memory. The base address of the array is the address of a[0].
Suppose the system assigns 300 as the base address of a. a[0], a[1], ...,a[19] are allocated
300, 304, ..., 376.
11
Arrays and pointers
#define N 20
int a[2N], i, *p, sum;
p = a; is equivalent to p = *a[0];
p is assigned 300.
Pointer arithmetic provides an alternative to array indexing.
p=a+1; is equivalent to p=&a[1]; (p is assigned 304)
a is a constant pointer.
13
Differences : Array & Pointers
p
S a b c d e \0
a b c d e \0
14
Pointer arithmetic and element size
double * p, *q ;
The expression p+1 yields the correct machine address for the next variable of that type.
• p+i
• ++p
• p+=i
• p-q /* No of array elements between p and q */
15
Pointer Arithmetic
• x = *p++ x = *p ; p = p + 1;
• x = (*p)++ x = *p ; *p = *p + 1;
16
Pointer Arithmetic
We can use pointer arithmetic to “walk” through memory:
° C automatically adjusts the pointer by the right amount each time (i.e., 1 byte for a char,
4 bytes for an int, etc.)
17
Arrays of Structures
We can define an array of structure records as
struct stud class[100];
18
Pointers and Structures
Once ptr points to a structure variable, the members can be accessed as:
ptr –> roll;
ptr –> dept_code;
ptr –> cgpa;
19
A Warning
When using structure pointers, we should take care of operator precedence.
• Member operator “.” has higher precedence than “*”.
ptr –> roll and (*ptr).roll mean the same thing.
*ptr.roll will lead to error.
20
Use of pointers to structures
#include <stdio.h>
struct complex {
float real;
void add (x, y, t)
float imag;
}; struct complex *x, *y, *t;
{
main( ) t->re = x->real + y->real;
{ t->im = x->imag + y->imag;
struct complex a, b, c;
scanf ( “%f %f”, &a.real, &a.imag ); }
scanf ( “%f %f”, &b.real, &b.imag );
add( &a, &b, &c ) ;
printf ( “\n %f %f”, c,real, c.imag );
}
21
Practice Problems
1. Write a function to search for an element in an array of integers that returns 1 if the element
is found, 0 otherwise. If found, it also returns the index in the array where found
2. Write a function that returns the number of lowercase letters, uppercase letters, and digit
characters in a string
3. Define a structure POINT to store the coordinates (integer) of a point in 2-d plane. Write a
function that returns the two farthest (largest distance) points in an array of POINT structures
4. Write a function that takes two arrays of integers A and B and returns the size of the union
set and the size of the intersection set of A and B
5. Write a function that returns the lengths of the largest palindromes formed by any substring
(sequence of consecutive characters) of the string. It should also return the index in the
string from which the palindrome starts.
For all of the above, add suitable main() functions to call the functions. Also, decide on what
parameters you will need; for better practice, for all problems other than problems 1, assume that
the return type of the function is void.
22