Pointers in C
--By
Mrs. E Sirisha,
Asst Professor,
CSE Dept.,
Gayatri Vidya Parishad College of Engineering(Autonomous),
Visakhapatnam
Variable
• A variable is a named memory location.
• Variables provide direct access to its memory
location.
• A variable has a name, an address, a type, and
a value:
• The name identifies the variable to the
programmer
• The address specifies where in main memory
the variable is located
Consider the declaration
int num=3;
This declaration tells the compiler to:
• Reserve space in memory to hold integer
value
• Associate the name I with this memory
location
• Store the value 3 at this location
Pointer variable
A pointer is a variable that contains the memory location
(address) of another variable.
Syntax:-
type * variable name;
You start by specifying the type of data stored in the
location identified by the pointer.
The asterisk tells the compiler that you are creating a
pointer variable.
Finally you give the name of the variable.
Declaring a Pointer Variable
To declare ptr as an integer pointer:
int *ptr;
To declare ptr as a character pointer:
char *ptr;
To declare ptr as a float pointer:
float *ptr;
Accessing a variable through its
pointer
• Once we declare a pointer variable we must point it to
something. We can do this by assigning to the
pointer the address of the variable you want to point as
in the following example:
ptr=#
• This places the address where num is stores into the
variable ptr. If num is stored in memory 21260 address
then the variable ptr has the value 21260.
Pointer Variable
Assume ptr is a pointer variable and x is an integer variable
x
pt
r
x = 10
10
ptr = &x
&x
Now ptr can access the value of x.
HOW!!!!
Write: *pointervariable . For
example:
printf(“%d”,*ptr);
Variables, Addresses and Pointers
main()
{
int a = 5;
int b = 6;
int *c;
// c points to variable a
c = &a;
}
Memory Value
a (1001) 5
b (1003) 6
c (1005) 1001
Example:
include< stdio.h >
{
int num, *intptr;
float x, *floptr;
char ch, *cptr;
num=123;
x=12.34;
ch=’a’;
intptr=&num;
cptr=&ch;
floptr=&x;
printf(“%d”,intptr);
printf(“%d”, floptr);
printf(“%d”, cptr);
}
Run this code
int main()
{
int x;
int *ptr;
x = 10;
ptr = &x;
*ptr = *ptr + 1;
printf(“%d”,x);
return 0;
}
Pointer Arithmetic
• We can perform arithmetic operations on
the pointers like
• Increment
• Decrement
• Addition
• Subtraction
• Comparison
Incrementing Pointer in C
• If we increment a pointer by 1, the
pointer will start pointing to the
immediate next location.
• This is somewhat different from the
general arithmetic since the value of the
pointer will get increased by the size of
the data type to which the pointer is
pointing.
Eg: int *p;//pointer to int
p=p+1; or
p++;
Decrementing Pointer in C
• Like increment, we can decrement a
pointer variable. If we decrement a
pointer, it will start pointing to the
previous location.
Eg: int *p;//pointer to int
p=p-1; or
p--;
• C Pointer Addition
• We can add a value to the pointer variable.
Eg: int *p;//pointer to int
p=p+3;
if we were using 32-bit architecture, it was
incrementing to 6 only, i.e., 2*3=6. As integer
value occupies 2-byte memory in 32-bit OS.
THANK YOU
ALL THE BEST FOR YOUR EXAM

Pointer basics.pptx

  • 1.
    Pointers in C --By Mrs.E Sirisha, Asst Professor, CSE Dept., Gayatri Vidya Parishad College of Engineering(Autonomous), Visakhapatnam
  • 2.
    Variable • A variableis a named memory location. • Variables provide direct access to its memory location. • A variable has a name, an address, a type, and a value: • The name identifies the variable to the programmer • The address specifies where in main memory the variable is located
  • 3.
    Consider the declaration intnum=3; This declaration tells the compiler to: • Reserve space in memory to hold integer value • Associate the name I with this memory location • Store the value 3 at this location
  • 4.
    Pointer variable A pointeris a variable that contains the memory location (address) of another variable. Syntax:- type * variable name; You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable.
  • 5.
    Declaring a PointerVariable To declare ptr as an integer pointer: int *ptr; To declare ptr as a character pointer: char *ptr; To declare ptr as a float pointer: float *ptr;
  • 6.
    Accessing a variablethrough its pointer • Once we declare a pointer variable we must point it to something. We can do this by assigning to the pointer the address of the variable you want to point as in the following example: ptr=&num; • This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260.
  • 7.
    Pointer Variable Assume ptris a pointer variable and x is an integer variable x pt r x = 10 10 ptr = &x &x Now ptr can access the value of x. HOW!!!! Write: *pointervariable . For example: printf(“%d”,*ptr);
  • 8.
    Variables, Addresses andPointers main() { int a = 5; int b = 6; int *c; // c points to variable a c = &a; } Memory Value a (1001) 5 b (1003) 6 c (1005) 1001
  • 9.
    Example: include< stdio.h > { intnum, *intptr; float x, *floptr; char ch, *cptr; num=123; x=12.34; ch=’a’; intptr=&num; cptr=&ch; floptr=&x; printf(“%d”,intptr); printf(“%d”, floptr); printf(“%d”, cptr); }
  • 10.
    Run this code intmain() { int x; int *ptr; x = 10; ptr = &x; *ptr = *ptr + 1; printf(“%d”,x); return 0; }
  • 11.
    Pointer Arithmetic • Wecan perform arithmetic operations on the pointers like • Increment • Decrement • Addition • Subtraction • Comparison
  • 12.
    Incrementing Pointer inC • If we increment a pointer by 1, the pointer will start pointing to the immediate next location. • This is somewhat different from the general arithmetic since the value of the pointer will get increased by the size of the data type to which the pointer is pointing. Eg: int *p;//pointer to int p=p+1; or p++;
  • 13.
    Decrementing Pointer inC • Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start pointing to the previous location. Eg: int *p;//pointer to int p=p-1; or p--;
  • 14.
    • C PointerAddition • We can add a value to the pointer variable. Eg: int *p;//pointer to int p=p+3; if we were using 32-bit architecture, it was incrementing to 6 only, i.e., 2*3=6. As integer value occupies 2-byte memory in 32-bit OS.
  • 17.
    THANK YOU ALL THEBEST FOR YOUR EXAM