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

Pointer c-1

Uploaded by

devarshkaji29
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Pointer c-1

Uploaded by

devarshkaji29
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Pointers in C

• Pointer is a variable that stores the address of


another variable . A pointer in C is used to allocate
memory dynamically at run time . The pointer
variable might be belonging to any of the data
type such as int, float, cahr, double etc .

• Syntax :-
data_type *var_name;

• Ex :-
int*p;
char*p;
Initializing of Pointer Variable
• Pointer initialization is the process of assigning address of a variable to
pointer variable . Pointer variable contains address of variable of same data
type .

• In c address operator & is used to determine the address of the


variable .It returns the address of the variable .

• Ex :-
int a=10;
int*p; // pointer declaration
p=&a; // pointer initialization
or
int*p=&a; //declaration and initialization
together
Chain Of Pointer
• It is possible to make a pointer to point to another
pointer, thus creating a chain of pointers.

• We can access the target value indirectly pointed to


by pointer to a pointer by applying the indirection
operator twice .
Pointer expression

•The pointer variable can be used in expression .

• Syntax:-
data_type * ptr=expression;

• For ex :- p1 and p2 are properly declared and


initialize pointer then the following expressions are
valid .

y=*p1 * *p2
sum=sum+*p1;
*p2=*p2+10;
Rules of Pointer Operations
• A pointer variable can be assign the address of another vaiable .

• A pointer variable can be assign the address of another pointer .

• A pointer variable can be assign a NULL value or zero value .

• A pointer variable can be pre-fixed or post-fixed with increment


or decrement operator.

• An integer value may be added or subtracted from a pointer


variable .
Array Of Pointer
• In C, a pointer array is a homogeneous collection of indexed
pointer variables that are references to a memory location.

• It is generally used in C Programming when we want to point at


multiple memory locations of a similar data type in our C
program. We can access the data by dereferencing the pointer
pointing to it.

• Syntax :- pointer_type *array_name [array_size];

• Here,
pointer_type: Type of data the pointer is pointing to.
array_name: Name of the array of pointers.
array_size: Size of the array of pointers.
Array Of Pointer to Character

•One of the main applications of the array of pointers is to


store multiple strings as an array of pointers to characters.

• Here, each pointer in the array is a character pointer that


points to the first character of the string.

• Syntax :- char *array_name [array_size];

• After that, we can assign a string of any length to these


pointers.
Pointer Arithmetic
• A pointer in C is an address which is a numeric
value , therefore we can perform arithmetic operation as
we do on any numeric value.

• A limited set of arithmetic operation can be perform


on pointer and they are as follows :
• Increment
• Decrement
• An integer added to a pointer
• An integer subtracted from a pointer
Incrementing a pointer

• If we increment a pointer by 1 the pointer will start pointing to


the immediate next location.

• This is different from the general arithmetic because the value


of the pointer will get increased by the size of the datatype to
which the pointer is pointing .

• The rule to increment the pointer is :


new_address=current_address + i * size_of(datatype)
• where i is the number by which the pointer get increased

• We can traverse an array by using pointer .


• Example :
int a=10;
int *p;
p=&a;
p=p+1;

• Here suppose address of a is 2010 which is stored in


pointer variable p.

• We perform increment operation on pointer variable p then


new value of p becomes .
p=p+1*sizeof(int);
p=2010+1*2
p=2010+2
p=2012
Decrementing pointer in C

• When we decrement a pointer it will start pointing to


the previous location .

• The syntax of decrementing the pointer is :


new_address=current_address – i * sizeof(data
type)
Precedence of Address and Dereferencing Operators
• c = *++cp or c = *(++cp):
increments the pointer cp, and then assigns the value pointed to by cp
to c.

•c = * cp++ or c = * (cp++)
assigns the value pointed to by cp to c, and then increments the pointer
cp.

• c = ++*cp or c = ++(*cp)
increments the value pointed to by cp, and then assigns the new value
to c; cp remains unchanged

•c = (*cp) ++
fetches the value pointed to by cp, assigns it to c, and then increments
the value pointed to by cp;
• c = *--cp or c = * (--cp)
decrements the pointer cp, and then assigns the value
pointed to by cp to c.

•c = * cp-- or c = * (cp--)
assigns the value pointed to by cp to c, and then
decrements the pointer cp.

• c = -- *cp or c = -- (* cp)
decrements the value pointed to by cp, and then assigns
the new value to c; cp remains unchanged

•c = (* cp) --
fetches the value pointed to by cp, assigns it to c, and then
decrements the value pointed to by cp; cp remains unchanged.
• Write a C program to traverse an array using pointer arithmetic
Pointer Conversion
• A pointer of one type can be converted to a pointer of another type by using
an explicit cast. The cast (T *) converts its operand into a pointer to an object of
type T.

• For example, a double pointer dp can be converted into an int pointer ip by


writing
ip = (int *) dp;
•and back to a double pointer by writing
dp = (double *) ip;

• The conversion of a pointer to P into a pointer to Q and back is guaranteed to


work correctly only if Q requires less or equally strict storage alignment when
compared to P.

• For example, given that cp is a character pointer, the statements


ip = (int *) cp;
i = *ip;
•will cause an addressing exception if cp was pointing to an odd address.
Generic Pointers
• Generic pointers are use to define functions whose formal parameters can
accept pointers of any type.

•In such situations, the type void * is used as the proper type for a generic
pointer.

• Any pointer may be converted to type void * and back without loss of
information.

• An explicit cast for such conversion may be added for clarity, but is not
necessary.

•Here is a prototype of a function that uses a generic pointer in its definition:


void free(void *) ;

•and here is an example of a call to this function:


free(cp);
FUNCTIONS AND POINTERS

• A function can take a pointer to any data type as argument and can return a
pointer to any data type.

•For example:
double *maxp(double *xp, double *yp)
{
return *xp >= *yp ? xp : yp;
}

•This function definition specifies that the function maxp returns a pointer to a .
double object, and expects two arguments, both of which are pointers to double
objects.

•Thus, given that double u = 1, v = 2, *mp; the statement


mp = maxp(&u, &v);

•makes mp point to v.
• For example :
void exchange(int *ip, int *jp)
{
int t;
t = *ip;
*ip = *jp;
*jp = t;
}

• and that the values of the integer variables i and j are 1 and 2, they
become 2 and 1 respectively after the function call exchange (&i,
&j)
• Application Of Array Of Pointers :-
1. Dynamic allocation of memory.

2. Handling string

3. Function Pointers

4. Efficient sorting and searching

5. Command line argument


• Disadvantage of array of pointer :-

1. Complexity in memory management

2. Increase complexity in code

3. Performance overhead

4. Difficulty in Data Management

5. Increase Memory usage

You might also like