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
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
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
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 Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.