C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Pointer Declaration
  • Initialization of Pointers
  • Examples
  • Example 1: Declaring and Initializing a Pointer
  • Example 2: Combining Declaration and Initialization

Was this helpful?

  1. Pointer

Defining Pointers

In C programming, defining pointers involves declaring variables capable of storing memory addresses. This section focuses on the syntax and principles of defining pointers, accompanied by examples to enhance clarity.

Pointer Declaration

  • To declare a pointer, specify the data type it points to, followed by an asterisk (*). For instance:

    int *integerPointer; // Declares a pointer to an integer

This line establishes a pointer named integerPointer capable of storing the memory address of an integer variable.

Initialization of Pointers

  • Pointers should be initialized before use. Initialization is achieved by assigning the address of an existing variable using the address-of operator (&):

    int number = 42;
    int *pointerToNumber = &number; // Initializes the pointer with the address of 'number'

Here, pointerToNumber holds the memory address of the integer variable number.

Examples

Example 1: Declaring and Initializing a Pointer

// Declaration
int *examplePointer;

// Initialization
int number = 77;
examplePointer = &number;

Example 2: Combining Declaration and Initialization

// Declaration and Initialization
double *temperaturePointer = NULL; // Initializing to NULL for safety

These examples illustrate the process of declaring pointers and initializing them with the addresses of existing variables. Such understanding is fundamental for efficient memory management and advanced programming techniques in C.

If you have specific questions or wish to explore additional topics, feel free to ask!

PreviousPointerNextAccessing Pointers

Was this helpful?