Computer Science & Engineering

pointers in c

innoovatum
By innoovatum

Faculty

created at Jan. 2, 2025, 10:07 a.m.

  • Creating PointersYou learned from the previous chapter, that we can get the memory address of a variable with the reference operator &:Exampleint myAge = 43; // an int variable.

    Note Description

    Creating Pointers

    You learned from the previous chapter, that we can get the memory address of a variable with the reference operator &:

    Example

    int myAge = 43// an int variable

    printf("%d", myAge);  // Outputs the value of myAge (43)
    printf("%p", &myAge); // Outputs the memory address of myAge (0x7ffe5367e044)
    Try it Yourself »

    pointer is a variable that stores the memory address of another variable as its value.

    pointer variable points to a data type (like int) of the same type, and is created with the * operator.

    The address of the variable you are working with is assigned to the pointer:

    Example

    int myAge = 43;     // An int variable
    int* ptr = &myAge;  // A pointer variable, with the name ptr, that stores the address of myAge

    // Output the value of myAge (43)
    printf("%d\n", myAge);

    // Output the memory address of myAge (0x7ffe5367e044)
    printf("%p\n", &myAge);

    // Output the memory address of myAge with the pointer (0x7ffe5367e044)
    printf("%p\n", ptr);
    Try it Yourself »

    Example explained

    Create a pointer variable with the name ptr, that points to an int variable (myAge). Note that the type of the pointer has to match the type of the variable you're working with (int in our example).

    Use the & operator to store the memory address of the myAge variable, and assign it to the pointer.

    Now, ptr holds the value of myAge's memory address.


    Dereference

    In the example above, we used the pointer variable to get the memory address of a variable (used together with the & reference operator).

    You can also get the value of the variable the pointer points to, by using the * operator (the dereference operator):

    Example

    int myAge = 43;     // Variable declaration
    int* ptr = &myAge;  // Pointer declaration

    // Reference: Output the memory address of myAge with the pointer (0x7ffe5367e044)
    printf("%p\n", ptr);

    // Dereference: Output the value of myAge with the pointer (43)
    printf("%d\n", *ptr);
    Try it Yourself »

    Note that the * sign can be confusing here, as it does two different things in our code:

    • When used in declaration (int* ptr), it creates a pointer variable.
    • When not used in declaration, it act as a dereference operator.

    Good To Know: There are two ways to declare pointer variables in C:

    int* myNum;
    int *myNum;

    Notes on Pointers

    Pointers are one of the things that make C stand out from other programming languages, like Python and Java.

    They are important in C, because they allow us to manipulate the data in the computer's memory. This can reduce the code and improve the performance. If you are familiar with data structures like lists, trees and graphs, you should know that pointers are especially useful for implementing those. And sometimes you even have to use pointers, for example when working with files and memory management.

    But be careful; pointers must be handled with care, since it is possible to damage data stored in other memory addresses.


    Tags:   c   programming

    Top Similar Notes

    Advanced Concrete Structures
    • Subject Name : ADVANCED CONCRETE STRUCTURES | Subject Code : CV-ACSFI

    SYLLABUS CE 15031: ADVANCED CONCRETE STRUCTURES (3-1-0) CR-04 Module-I (10 Hours) Introduction to EQ Engineering: Cyclic behavior of concrete and reinforcement, significance of ductility, ductilit...

    • 1 Likes |
    • 31 Downloads |
    • 58 Views
    Crud Application Using Cakephp | Innoovatum.Com
    • Subject Name : none | Subject Code : none

    ...

    • 0 Likes |
    • 0 Downloads |
    • 33 Views
    Crud Application Using Cakephp Part 2 | Innoovatum.Com
    • Subject Name : none | Subject Code : none

                                                            &nbs...

    • 0 Likes |
    • 0 Downloads |
    • 31 Views
    Website Hosting In 5 Minutes | Innoovatum.Com | Shaukat Kotwal
    • Subject Name : none | Subject Code : none

    ...

    • 0 Likes |
    • 400 Downloads |
    • 325 Views
    Creating Audio App Player In Javascript | Innoovatum
    • Subject Name : none | Subject Code : none

    ...

    • 0 Likes |
    • 0 Downloads |
    • 32 Views