Pointers in C

Last Updated :

Pointers are a fundamental concept in the C programming language, which allow direct access to memory locations. A pointer is a variable that holds a memory address, which points to the location of a value in memory. Pointers are used to pass information between functions, dynamically allocate memory, and access data structures.

In C, pointers are declared using the asterisk (*) symbol. Here’s an example of how to declare a pointer:

int *ptr;

This declares a pointer variable named “ptr” that can store the memory address of an integer value.

Here are some basic operations that can be performed on pointers in C:

  1. Address of operator (&) –
    The address-of operator (&) is used to get the memory address of a variable. Here’s an example:

    int num = 42;
    int *ptr = #
    

    This sets the value of “ptr” to the memory address of “num”.

  2. Dereferencing operator (*) –
    The dereferencing operator (*) is used to access the value stored at a memory address. Here’s an example:

    int num = 42;
    int *ptr = #
    printf("%d\n", *ptr);
    

    This prints the value of “num” (42) by dereferencing the pointer “ptr”.

  3. Pointer arithmetic –
    Pointer arithmetic allows you to perform arithmetic operations on pointers, such as adding or subtracting an integer value to the pointer. Here’s an example:

    int arr[3] = {1, 2, 3};
    int *ptr = arr;
    printf("%d\n", *ptr);     // prints 1
    ptr++;
    printf("%d\n", *ptr);     // prints 2
    

    This sets the value of “ptr” to the memory address of the first element of the array “arr”. Then, the pointer is incremented by one, which points to the next element of the array.

  4. Dynamic memory allocation –
    Dynamic memory allocation allows you to allocate memory at runtime, rather than at compile time. This is done using the “malloc” function, which returns a pointer to the allocated memory. Here’s an example:

    int *ptr = (int*)malloc(10 * sizeof(int));
    

    This allocates 10 integers worth of memory and sets the value of “ptr” to the memory address of the first integer.

Note that when using pointers, it’s important to be careful and avoid common pitfalls like dereferencing uninitialized or null pointers, and accessing memory beyond its allocated bounds. These can lead to memory errors and crashes in your program.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 15 Feb, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read