Pointers in C
Introduction to Pointers
• Holds the address of another variable.
• Advantages: efficient arrays, multiple return
values, dynamic memory, linked structures,
faster execution.
• Disadvantage: program may crash if memory
unavailable.
• Operators: & (address), * (dereference).
Declaring a Pointer
Syntax: data_type *ptr_name;
Examples:
int *p;
float *temp;
int x=10; int *ptr=&x;
printf("%d", *ptr);
Program: Basic Pointer Usage
#include<stdio.h>
void main() {
int x=99;
int *ptr=&x;
printf("Value = %dn", *ptr);
printf("Address = %pn", ptr);
}
Output:
Value = 99
Address = 0x7ffd...
Program: Add 2 Floats
#include<stdio.h>
void main() {
float n1,n2,sum=0.0;
float *p1=&n1,*p2=&n2,*ps=&sum;
scanf("%f,%f",p1,p2);
*ps=*p1+*p2;
printf("%f+%f=%f",*p1,*p2,*ps);
}
Output: 3.2+2.3=5.5
Program: Positive/Negative
#include<stdio.h>
void main() {
int num,*p=&num;
scanf("%d",p);
if(*p>0) printf("Positive");
else if(*p<0) printf("Negative");
else printf("Equal");
}
Null Pointers
Special value points nowhere.
int *ptr=NULL;
if(ptr==NULL){ /* safe */ }
Generic (Void) Pointers
Can point to any type.
void *gp;
int x=10; char c='A';
gp=&x; printf("%d",*(int*)gp);
gp=&c; printf("%c",*(char*)gp);
Passing Arguments to Functions
• Pass addresses to functions.
• Function dereferences pointers to modify
caller’s vars.
Example: swap, sum, largest number.
Program: Sum of Two Integers
#include<stdio.h>
void sum(int *a,int *b,int *t){
*t=*a+*b;
}
int main(){
int n1,n2,t;
scanf("%d,%d",&n1,&n2);
sum(&n1,&n2,&t);
printf("Total=%d",t);
}
Applications of Pointers
• Arrays & Strings
• Function Pointers
• Structures
• File Handling
• Efficient Parameter Passing
• Dynamic Data Structures
• Callback Functions

Pointers in C programming second semester

  • 1.
  • 2.
    Introduction to Pointers •Holds the address of another variable. • Advantages: efficient arrays, multiple return values, dynamic memory, linked structures, faster execution. • Disadvantage: program may crash if memory unavailable. • Operators: & (address), * (dereference).
  • 3.
    Declaring a Pointer Syntax:data_type *ptr_name; Examples: int *p; float *temp; int x=10; int *ptr=&x; printf("%d", *ptr);
  • 4.
    Program: Basic PointerUsage #include<stdio.h> void main() { int x=99; int *ptr=&x; printf("Value = %dn", *ptr); printf("Address = %pn", ptr); } Output: Value = 99 Address = 0x7ffd...
  • 5.
    Program: Add 2Floats #include<stdio.h> void main() { float n1,n2,sum=0.0; float *p1=&n1,*p2=&n2,*ps=&sum; scanf("%f,%f",p1,p2); *ps=*p1+*p2; printf("%f+%f=%f",*p1,*p2,*ps); } Output: 3.2+2.3=5.5
  • 6.
    Program: Positive/Negative #include<stdio.h> void main(){ int num,*p=&num; scanf("%d",p); if(*p>0) printf("Positive"); else if(*p<0) printf("Negative"); else printf("Equal"); }
  • 7.
    Null Pointers Special valuepoints nowhere. int *ptr=NULL; if(ptr==NULL){ /* safe */ }
  • 8.
    Generic (Void) Pointers Canpoint to any type. void *gp; int x=10; char c='A'; gp=&x; printf("%d",*(int*)gp); gp=&c; printf("%c",*(char*)gp);
  • 9.
    Passing Arguments toFunctions • Pass addresses to functions. • Function dereferences pointers to modify caller’s vars. Example: swap, sum, largest number.
  • 10.
    Program: Sum ofTwo Integers #include<stdio.h> void sum(int *a,int *b,int *t){ *t=*a+*b; } int main(){ int n1,n2,t; scanf("%d,%d",&n1,&n2); sum(&n1,&n2,&t); printf("Total=%d",t); }
  • 11.
    Applications of Pointers •Arrays & Strings • Function Pointers • Structures • File Handling • Efficient Parameter Passing • Dynamic Data Structures • Callback Functions