Declaring Pointers
Part of the Logic & Flow section of Coddy's C journey — lesson 2 of 63.
Now that you understand what pointers are, let's learn how to create them. Declaring a pointer variable follows a specific syntax that tells the compiler what type of data the pointer will point to.
The basic syntax for declaring a pointer is: data_type *pointer_name;
int *ptr; // Declares a pointer to an integer
char *ch_ptr; // Declares a pointer to a character
float *f_ptr; // Declares a pointer to a floatThe asterisk (*) is what makes this a pointer declaration. It tells the compiler that this variable will store a memory address, not the actual data value. When you declare int *ptr;, you're saying "ptr is a pointer that can store the address of an integer variable."
Notice that the data type before the asterisk specifies what kind of data the pointer will point to. This is important because different data types take up different amounts of memory space, and the compiler needs to know this information for proper memory management.
Challenge
EasyWrite a C program that demonstrates proper pointer declaration syntax. Your program should:
- Declare a pointer to an integer named
int_ptr - Declare a pointer to a character named
char_ptr - Declare a pointer to a float named
float_ptr - Print the memory addresses stored in each pointer using
printfwith the%pformat specifier
Your output should display the three pointer addresses in the following format:
Integer pointer: [address]
Character pointer: [address]
Float pointer: [address]Note that the actual memory addresses will vary, but the format and labels must match exactly as shown above.
Cheat sheet
To declare a pointer variable, use the syntax: data_type *pointer_name;
int *ptr; // Declares a pointer to an integer
char *ch_ptr; // Declares a pointer to a character
float *f_ptr; // Declares a pointer to a floatThe asterisk (*) makes this a pointer declaration, telling the compiler that this variable will store a memory address. The data type before the asterisk specifies what kind of data the pointer will point to.
Use %p format specifier with printf to display pointer addresses.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Declare your pointers here
// TODO: Print the pointer addresses using the required format
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions