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

Chp_3 - Pointers

The document provides an overview of pointers in C programming, explaining their definition, declaration, and usage. It covers pointer arithmetic, pointers to arrays, and pointers to pointers, along with examples to illustrate these concepts. Additionally, it highlights potential issues with pointers, such as uninitialized pointers and the risks of erroneous pointer assignments.

Uploaded by

elyesaposlu31
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)
16 views

Chp_3 - Pointers

The document provides an overview of pointers in C programming, explaining their definition, declaration, and usage. It covers pointer arithmetic, pointers to arrays, and pointers to pointers, along with examples to illustrate these concepts. Additionally, it highlights potential issues with pointers, such as uninitialized pointers and the risks of erroneous pointer assignments.

Uploaded by

elyesaposlu31
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/ 25

PROGRAMMING II

CHAPTER 3.
POINTERS

CENGİZ RİVA
Pointers

• A pointer is a variable that contains the address of a variable.


• This address is a location of another variable in memory.
• The value of a pointer “points” to a variable which may be
accessed indirectly with the special pointer operators * and &.
• The * operator accesses the contents of a variable whose
address is the value of a pointer.
• The * can be remembered as “at address”.
• The & operator returns the address of a variable and can be
remembered as “the address of”.
Pointers
• Let’s copy a variable content var1 to another variable var2
using pointer variable p to .
#include <stdio.h>
int main ()
{
int var1,var2; /* actual variable declaration */
int *p; /* pointer variable declaration */
var1 = 10;
p = &var1; /* store address of var in pointer variable*/
var2=*p;

printf("Address of var1 variable:\t%lu\n", &var1 );


/* address stored in pointer variable */

printf("Address stored in p variable:\t%lu\n", p );


/* access the value using the pointer */
printf("Value of var1: %i\n", var1 );
printf("Value stored at p %i\n", *p );
printf("Value of var2: %i\n", var2 );
return 0;
}
Pointers
• When we execute we observe the pointer p shows the address
of variable var1.
• Content of the pointer p is therefore shows the value of var1.
• When we copy the content of pointer p to var2, then var2 gets
the value of var1 as shown.
Pointer Declaration
• Like any variable or constant, you must declare a pointer
before you can use it to store any variable address. The general
form of a pointer variable declaration is:
type *var-name;

• Here, type is the pointer's base type;


• It must be a valid C data type and var-name is the name of the
pointer variable.
• The asterisk * you used to declare a pointer is the same asterisk
that you use for multiplication.
• However, in this statement the asterisk is being used to
designate a variable as a pointer.
Pointer Declaration
• Following are the valid pointer declaration:

int *ip; /* pointer to an integer */


double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */

• The actual data type of the value of all pointers, whether integer,
float, character, or otherwise, is the same, a long hexadecimal
number that represents a memory address.
• The only difference between pointers of different data types is
the data type of the variable or constant that the pointer points
to.
Pointer Declaration
• Following is an invalid pointer declaration.
#include <stdio.h>
int main ()
{
float x,y;
int *p;
x=1.5;
p=&x;
y=*p;
printf("value of y= %f\n",y);
}

• The compiler will give error:


NULL Pointer Assignment
• It is always a good practice to assign a NULL value to a pointer
variable in case you do not have exact address to be assigned.
• This is done at the time of variable declaration. A pointer that
is assigned NULL is called a null pointer.
• The NULL pointer is a constant with a value of zero defined in
several standard libraries. Consider the following program:
#include <stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}
Pointer Assignment
• As with any variable, a pointer may be used in the right side of
assignment to assign its value to another pointer or to a
nonpointer as shown below:
#include <stdio.h>
int main ()
{
int x;
unsigned y,p2;
int *p1;
x=100;
p1=&x;
p2=p1;
y=p2;
printf("address of x= %u\n",y);
printf("value of x= %i\n",*p1);
}
Pointer Arithmetic

• C pointer is an address, which is a numeric value.


• Therefore, you can perform arithmetic operations on a pointer
just as you can a numeric value.
• There are four arithmetic operators that can be used on
pointers: ++, --, +, and – .
• To understand pointer arithmetic, let us consider that ptr is an
integer pointer which points to the address 1000.
• Assuming 32-bit integers, let us perform the following
arithmetic operation on the pointer:

ptr++;
Pointer Arithmetic
ptr++;

• Now, after the above operation, the ptr will point to the location
1004 because each time ptr is incremented, it will point to the
next integer location which is 4 bytes next to the current location.
#include <stdio.h>
void main ()
{
int *ptr,x;
x=10;
ptr=&x;
printf("The value of ptr is : %lu\n", ptr );
ptr++;
printf("The value of ptr after ++ : %lu\n", ptr);
}
Pointer Arithmetic
• If ptr points to a character whose address is 1000, then above
operation will point to the location 1001 because next
character will be available at 1001.
#include <stdio.h>
void main ()
{
char *ptr,x;x='A';
ptr=&x;
printf("The value of ptr is : %u\n", ptr );
ptr++;
printf("The value of ptr after ++ : %u\n", ptr);
}
Pointer Arithmetic

• Actually, all type of pointers will increase or decrease by the


length of the data type that they point to.
• For integers, the length is usually 2 bytes, for floats it will
usually be 8.
• Normally, you will not need to know the actual values of the
pointers because they are not necessary in order to use them
unless you are concerned about the actual memory addresses
for some reason.
• You are not limited to increment and decrement pointers,
addition or subtraction operations can also be performed bu
not multiplication or division.
int *p1;
p1 = p1 + 9;
Pointers to Arrays

• In C, there is a strong relationship between pointers and arrays,


strong enough that pointers and arrays should be discussed
simultaneously.
• Any operation that can be achieved by array subscripting can
also be done with pointers.
• The pointer version will in general be faster but, at least to the
uninitiated, somewhat harder to understand.
• The declaration of an array a with 10 elements:
int a[10];
Pointers to Arrays
• If p is a pointer to an integer, declared as
int *p;

• We can set the address of the first element in array a to


pointer p:
p = &a[0];

• or in a more simple and common way as:


p = a;
Pointers to Arrays
• Now the assignment:
x = *p;

• will copy the contents of a[0] into x.


• If p points to a particular element of an array, then by
definition p+1 points to the next element, p+i points i elements
after p, and p-i points i elements before.
• *(p+1) refers to the contents of a[1].
Pointer to integer Array
Example
• Let’s a write simple program which we access to integer type
array elements using pointer.
#include <stdio.h>
void main()
{
int str[10];
int *p;
int i,x;
for(i=0;i<10;++i) str[i]=10*i;
p=str;
printf("0. elementh is = %i\n",*p);
printf("3. elementh is = %i\n",*(p+2));
for(i=0;i<10;++i) printf("%i. element of str =
%i\n",i,*(p+i));
}
Pointer to Character Array
Example
• Let’s a write simple program which we access to character type
array elements using pointer and pointer incrementing.
#include <stdio.h>
void main()
{
char str[]={'C','e','n','g','i','z'};
char *p;
int i,x;
p=str;
printf("0. elementh is = %c\n",*p);
printf("3. elementh is = %c\n",*(p+2));
for(i=0;i<6;++i)
{
printf("%i. element of str = %c\n",i,*p);
p++;
}
}
Pointers to Arrays

• Most professional C programmers prefer to use pointers


instead of arrays because the pointers are faster and easier to
use.
• However do not think that array indexing is wrong or
unnecessary because it does have its place.
• If the array is going to be accessed in strictly ascending or
descending order, pointers are easy and faster.
• If the array is going to accessed randomly, array indexing is
much more better to code and read the program.
• One disadvantage of using pointer is errors.
• If programmers do not correctly address it, it might cause
severe problems.
Arrays of Pointers
• Pointers may be arrayed just like any other data type.
• There may be a situation when we want to maintain an array,
which can store pointers to an int or char or any other data
type available.
• Following is the declaration of an array of pointers to an
integer:

int *p[3];

• Each element in ptr, now holds a pointer to an int value.


Following example makes use of three integers, which will be
stored in an array of pointers as follows:
Arrays of Pointers
• Each element in ptr, now holds a pointer to an int value.
• Following example makes use of three integers, which will be
stored in an array of pointers as follows:
#include <stdio.h>
void main ()
{
int var[] = {10, 100, 200};
int i, *p[3];
for ( i = 0; i < 3; i++) p[i] = &var[i];
for ( i = 0; i < 3; i++) printf("Value of var[%d] =
%d\n", i, *p[i] );
}
Pointers to Pointers

• A pointer to a pointer is a form of multiple indirection, or a


chain of pointers.
• Normally, a pointer contains the address of a variable.
• When we define a pointer to a pointer, the first pointer
contains the address of the second pointer, which points to the
location that contains the actual value as shown below.
Pointers to Pointers

• An example of how to use a pointer to a pointer is given below


as a short program:

#include <stdio.h>
void main()
{
int x,*p,**q;
x=10;
p=&x;
q=&p;
printf("value of x = %i\n",**q);
}
Problems with Pointers

• Pointers provide tremendous power and are necessary for


several systems programs, but when a pointer accidentally
contains a wrong value, it can be the most difficult bug to track
down.
• An erroneous pointer can cause reading or writing to some
unknown piece of memory.
• If you read from it, the worst that can happen is that you read
a garbage.
• However, if you write to it, you will be writing over other
pieces of your code or data.
• This will not give a compiling error but execution of the
program may lead to severe problems.
Problems with Pointers
• The classic example of a pointer error is the uninitialized
pointer as shown below:
#include <stdio.h>
void main()
{
int x,*p;
x=10;
*p=x;
printf("value of x = %i\n",*p);
}

• This will put the value of x to an unknown address of p.


p=x;

• This will assign value of x to the address of p.


• Address of x must be given to p. It must be:
p=&x;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy