Table of contents
1.
Introduction
2.
Introduction to Pointers in C
3.
Syntax for Declaring Pointers
4.
Pointer Declaration with Initialization
5.
Types of Pointers
5.1.
Null Pointers
5.2.
Void Pointers
5.3.
Wild Pointers 
6.
Pointer and Data Types
6.1.
Integer Pointer
6.2.
Float Pointer
6.3.
Character Pointer
7.
Examples of Pointer Declarations
7.1.
Declaring an Integer Pointer
7.2.
C
7.3.
Declaring a Float Pointer
7.4.
C
7.5.
Declaring a Character Pointer
7.6.
C
8.
Frequently Asked Questions
8.1.
What is the difference between a pointer and a reference? 
8.2.
How do I declare a pointer to a pointer? 
8.3.
Why should I initialize a pointer? 
9.
Conclusion
Last Updated: Aug 3, 2024
Easy

Declare a Pointer in C

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pointers are a fundamental concept in C programming, essential for understanding how memory works and for effective programming. 

Declare a Pointer in C

In this article, we will explore how to declare pointers in C, their syntax, and their uses. By the end, you'll have a solid understanding of pointers and how to use them in your programs.

Introduction to Pointers in C

Pointers are variables that store the memory address of another variable. They are a powerful feature of C, allowing you to work directly with memory and perform complex operations efficiently. 

Understanding pointers is crucial for tasks such as dynamic memory allocation, array manipulation, and implementing data structures like linked lists.

Syntax for Declaring Pointers

Declaring a pointer in C involves specifying the type of data the pointer will point to and using the * symbol to indicate that the variable is a pointer. Here’s the basic syntax:

type *pointer_name;

 

  • type: The data type of the variable that the pointer will point to.
     
  • *: Indicates that the variable is a pointer.
     
  • pointer_name: The name of the pointer variable.
     

Pointer Declaration with Initialization

You can also declare and initialize a pointer in one line:

type *pointer_name = &variable_name;

 

  • &variable_name: The address of the variable that the pointer will point to.

Types of Pointers

Null Pointers

 A null pointer is a pointer that does not point to any memory location. It is typically initialized to NULL or 0.

int *ptr = NULL;

Void Pointers

A void pointer is a generic pointer that can point to any data type but cannot be dereferenced directly.

void *ptr;
int num = 10;
ptr = #

Wild Pointers 

A wild pointer is an uninitialized pointer that may point to any memory location. It is important to initialize pointers to avoid undefined behavior.
 

int *ptr; // Wild pointer

Pointer and Data Types

Pointers can be declared for various data types. Here are examples:

Integer Pointer

int *intPtr;
int num = 5;
intPtr = #

 

  • Explanation: intPtr is a pointer to an integer. It stores the address of the integer variable num.

Float Pointer

float *floatPtr;
float value = 3.14;
floatPtr = &value;

 

  • Explanation: floatPtr is a pointer to a float. It stores the address of the float variable value.

Character Pointer

char *charPtr;
char letter = 'A';
charPtr = &letter;

 

  • Explanation: charPtr is a pointer to a character. It stores the address of the character variable letter.

Examples of Pointer Declarations

Declaring an Integer Pointer

  • C

C

int num = 10;

int *ptr = # // Pointer to integer

printf("Value of num: %d\n", *ptr);
You can also try this code with Online C Compiler
Run Code

 

Output: 

10

 

Explanation: Here, ptr is an integer pointer initialized to the address of num. *ptr dereferences the pointer to access the value of num.

Declaring a Float Pointer

  • C

C

float value = 5.67;

float *fptr = &value; // Pointer to float

printf("Value of value: %.2f\n", *fptr);
You can also try this code with Online C Compiler
Run Code


Output: 

5.67


Explanation: fptr is a float pointer initialized to the address of value. *fptr dereferences the pointer to access the value of value.

Declaring a Character Pointer

  • C

C

char letter = 'Z';

char *cptr = &letter; // Pointer to char

printf("Value of letter: %c\n", *cptr);
You can also try this code with Online C Compiler
Run Code


Output: 

Z

 

Explanation: cptr is a character pointer initialized to the address of letter. *cptr dereferences the pointer to access the value of letter.

Frequently Asked Questions

What is the difference between a pointer and a reference? 

A pointer holds the address of a variable and can be changed to point to different variables, while a reference is an alias for another variable and cannot be changed once initialized.

How do I declare a pointer to a pointer? 

A pointer to a pointer is declared with two asterisks. For example:

int **ptr;

Why should I initialize a pointer? 

Initializing a pointer avoids undefined behavior by ensuring the pointer points to a valid memory location.

Conclusion

Declaring and using pointers in C is crucial for efficient programming and understanding memory management. By grasping the basics of pointer declaration, initialization, and usage, you'll be better equipped to handle more complex programming tasks. Remember to follow best practices to avoid common pitfalls and make the most out of pointers in your C programs.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass