SlideShare a Scribd company logo
4
Most read
8
Most read
14
Most read
Pointers in C




 Omar Mukhtar
Outline


    Review of concepts in previous lectures

    Introduction to pointers

    Pointers as function arguments

    Pointers and arrays

    Pointer arithmetic

    Pointer-to-pointer
Review and Background


    Basic data types
       −     Place-holders for numeric data
               
                   Integer numbers (int, short, long)
               
                   Real numbers (float, double)
               
                   Characters / symbols codes (char)

    Arrays
       −     A contiguous list of a particular data type

    Functions
       −     Give “name” to a particular piece of code
       −     Modularization & reuse of code
Pointers


    The most useful and tricky concept in C
    language
       −   Other high level languages abstract-out this
           concept

    The most powerful construct too
       −   Makes C very fast
       −   Direct interaction with hardware
       −   Solves very complicated programming
           problems
What are pointers?


    Just another kind of “placeholder” to hold
    “address” of memory location
        −   Address is also a number
        −   Itself resides on some memory location

       Memory Address      Value
            0x8004           ...
            0x8008                        variable A
                            129
            0x800C           ...
            0x8010         0x8008        address of A
            0x8014           ...
What are pointers?

    Declare variables a, b
        −   int a, b;

    Declare a pointer
        −   int* pa;

    Set value of a
        −   a = 10;

    Point pa to address of a
        −   pa = &a;

    Set value of a using pa
        −   *pa = 12; pa = &b;
Pointer Operators


    “address-of” operator: &
       −   Gets address of a variable

    De-referencing operator: *
       −   Accesses the memory location this pointer
           holds address of
Pointers and Functions


    A function can be passed arguments using
    basic data types
        −    int prod(int a, int b) { return
             a*b; }

    How to return multiple values from function?
        −    void prod_and_sum(int a, int b,
             int*p, int* s)
             { *p = a*b; *s = a+b; }
In & Out Arguments of Function


    A function may like to pass values and get the
    result in the same variables. e.g. a Swap
    function.

    void swap(int* a, int* b) { int c;
     c = *b; *b = *a; *a = c; }

    int a = 5; b = 6;

    swap(&a, &b);

    // a hold 6 and b hold 5 now.
Pointers and Arrays


    Since arrays are a contiguous set of variables
    in memory, we can access them with pointers

    int arr[5];

    int *p = &arr[0];

    *(p+0) = 1;      // arr[0]

    *(p+1) = 2;      // arr[1]

    *(p+2) = 4;      // arr[2]

    *(p+3) = 8;      // arr[3]

    ...
Pointer Arithmetic

    Arithmetic operators work as usual on ordinary
    data types.
        −   int a = 1; a++; // a == 2

    It gets a bit complicated when arithmetic
    operators are used on pointers

    int* p = 0x8004; p++;

    What does p hold now? 0x8005???

    Compiler knows that p is a pointer to integer
    type data, so an increment to it should point to
    next integer in memory. Hence 0x8008.
Pointer Arithmetic

    So an arithmetic operator increases or
    decreases its contents by the size of data type
    it points to

    int* pi = 0x8004; double* pd =
    0x9004; char* pc = 0xa004;

    pi++; // pi == 0x8008

    pd++; // pd == 0x900c

    pc++; // pc == 0xa005

    Only '+' and '-' operator are allowed. '*' and '/'
    are meaningless.
Pointer-to-Pointer

    Pointer variable is just a place-holder of an
    address value, and itself is a variable.
        −   Hence a pointer can hold address of other
            pointer variable. In that case it is called a
            “double pointer”.

    int*p; int **pp; pp = &p;

    e.g a function may like to return a pointer
    value

    void pp_example(int** p) { *p =
    0x8004; }

    int *p; pp_example(&p);
Pointer Pitfalls

    Since pointer holds address of memory
    location, it must never be used without proper
    initialization.

    An uninitialized pointer may hold address of
    some memory location that is protected by
    Operating System. In such case, de-
    referencing a pointer may crash the program.

    An initialized pointer does not know the
    memory location, it is pointing to is, holds a
    valid value or some garbage.

    A pointer cannot track boundaries of an array.

More Related Content

PPT
Pointers C programming
PPTX
Pointer in c
PPTX
Functions in c language
PPTX
Arrays in c
PPTX
Freedom of speech
PDF
Operators in c programming
PPTX
PPTX
Digital Marketing for Beginners.pptx
Pointers C programming
Pointer in c
Functions in c language
Arrays in c
Freedom of speech
Operators in c programming
Digital Marketing for Beginners.pptx

What's hot (20)

PDF
Pointers
PPTX
Pointer in c program
PPTX
Presentation on pointer.
PPT
RECURSION IN C
PPTX
Pointers in c language
PDF
Pointers in C
PPTX
Pointer in C++
PPTX
Pointer to function 1
PPTX
Pointers in c++
PPTX
Pointer in C
PPTX
POINTERS IN C
PPTX
Computer Science:Pointers in C
PPTX
Pointers in C Programming
PPTX
Pointers in c++
PPTX
Array Of Pointers
PPTX
C programming - String
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Functions in C
PPTX
Pointers in c - Mohammad Salman
PPTX
Pointer arithmetic in c
Pointers
Pointer in c program
Presentation on pointer.
RECURSION IN C
Pointers in c language
Pointers in C
Pointer in C++
Pointer to function 1
Pointers in c++
Pointer in C
POINTERS IN C
Computer Science:Pointers in C
Pointers in C Programming
Pointers in c++
Array Of Pointers
C programming - String
Array Introduction One-dimensional array Multidimensional array
Functions in C
Pointers in c - Mohammad Salman
Pointer arithmetic in c
Ad

Similar to C Pointers (20)

PPTX
pointers.pptx
PDF
PSPC--UNIT-5.pdf
PPT
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPTX
Pointers in C
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPTX
PPS-POINTERS.pptx
PPTX
Pointers in C++ object oriented programming
PPTX
Introduction to pointers in c plus plus .
PPTX
pointers.pptx
PDF
PPTX
Pointers and single &multi dimentionalarrays.pptx
PPT
Pointers
PPT
ch08.ppt
PDF
Pointers-Computer programming
PPTX
COM1407: Working with Pointers
PPT
pointer, structure ,union and intro to file handling
PPT
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
Advanced pointers
pointers.pptx
PSPC--UNIT-5.pdf
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Pointers in C
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPS-POINTERS.pptx
Pointers in C++ object oriented programming
Introduction to pointers in c plus plus .
pointers.pptx
Pointers and single &multi dimentionalarrays.pptx
Pointers
ch08.ppt
Pointers-Computer programming
COM1407: Working with Pointers
pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Advanced pointers
Ad

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Spectroscopy.pptx food analysis technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
20250228 LYD VKU AI Blended-Learning.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Big Data Technologies - Introduction.pptx
Encapsulation theory and applications.pdf
Approach and Philosophy of On baking technology
Programs and apps: productivity, graphics, security and other tools
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectroscopy.pptx food analysis technology
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Empathic Computing: Creating Shared Understanding
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

C Pointers

  • 1. Pointers in C Omar Mukhtar
  • 2. Outline  Review of concepts in previous lectures  Introduction to pointers  Pointers as function arguments  Pointers and arrays  Pointer arithmetic  Pointer-to-pointer
  • 3. Review and Background  Basic data types − Place-holders for numeric data  Integer numbers (int, short, long)  Real numbers (float, double)  Characters / symbols codes (char)  Arrays − A contiguous list of a particular data type  Functions − Give “name” to a particular piece of code − Modularization & reuse of code
  • 4. Pointers  The most useful and tricky concept in C language − Other high level languages abstract-out this concept  The most powerful construct too − Makes C very fast − Direct interaction with hardware − Solves very complicated programming problems
  • 5. What are pointers?  Just another kind of “placeholder” to hold “address” of memory location − Address is also a number − Itself resides on some memory location Memory Address Value 0x8004 ... 0x8008 variable A 129 0x800C ... 0x8010 0x8008 address of A 0x8014 ...
  • 6. What are pointers?  Declare variables a, b − int a, b;  Declare a pointer − int* pa;  Set value of a − a = 10;  Point pa to address of a − pa = &a;  Set value of a using pa − *pa = 12; pa = &b;
  • 7. Pointer Operators  “address-of” operator: & − Gets address of a variable  De-referencing operator: * − Accesses the memory location this pointer holds address of
  • 8. Pointers and Functions  A function can be passed arguments using basic data types − int prod(int a, int b) { return a*b; }  How to return multiple values from function? − void prod_and_sum(int a, int b, int*p, int* s) { *p = a*b; *s = a+b; }
  • 9. In & Out Arguments of Function  A function may like to pass values and get the result in the same variables. e.g. a Swap function.  void swap(int* a, int* b) { int c; c = *b; *b = *a; *a = c; }  int a = 5; b = 6;  swap(&a, &b);  // a hold 6 and b hold 5 now.
  • 10. Pointers and Arrays  Since arrays are a contiguous set of variables in memory, we can access them with pointers  int arr[5];  int *p = &arr[0];  *(p+0) = 1; // arr[0]  *(p+1) = 2; // arr[1]  *(p+2) = 4; // arr[2]  *(p+3) = 8; // arr[3]  ...
  • 11. Pointer Arithmetic  Arithmetic operators work as usual on ordinary data types. − int a = 1; a++; // a == 2  It gets a bit complicated when arithmetic operators are used on pointers  int* p = 0x8004; p++;  What does p hold now? 0x8005???  Compiler knows that p is a pointer to integer type data, so an increment to it should point to next integer in memory. Hence 0x8008.
  • 12. Pointer Arithmetic  So an arithmetic operator increases or decreases its contents by the size of data type it points to  int* pi = 0x8004; double* pd = 0x9004; char* pc = 0xa004;  pi++; // pi == 0x8008  pd++; // pd == 0x900c  pc++; // pc == 0xa005  Only '+' and '-' operator are allowed. '*' and '/' are meaningless.
  • 13. Pointer-to-Pointer  Pointer variable is just a place-holder of an address value, and itself is a variable. − Hence a pointer can hold address of other pointer variable. In that case it is called a “double pointer”.  int*p; int **pp; pp = &p;  e.g a function may like to return a pointer value  void pp_example(int** p) { *p = 0x8004; }  int *p; pp_example(&p);
  • 14. Pointer Pitfalls  Since pointer holds address of memory location, it must never be used without proper initialization.  An uninitialized pointer may hold address of some memory location that is protected by Operating System. In such case, de- referencing a pointer may crash the program.  An initialized pointer does not know the memory location, it is pointing to is, holds a valid value or some garbage.  A pointer cannot track boundaries of an array.