Pointers & Structures
Expert Answer & Key Takeaways
Deep dive into advanced C concepts: Memory addresses, Pointers (declaration, initialization, arithmetic), and User-Defined Data Types (Structures and Unions).
Pointers & Structures in C
Pointers and Structures are advanced features that give C its incredible power and flexibility, allowing for direct memory manipulation and the creation of complex data types.
1. Pointers
A Pointer is a special variable that stores the memory address of another variable, rather than storing a standard data value like an integer or character.
1.1 Why use Pointers?
- Dynamic Memory Allocation: Used with
malloc()andfree()to allocate memory during runtime. - Call by Reference: Passing pointers to functions allows the function to modify the original variable.
- Array Manipulation: Pointers make iterating through arrays much faster and more efficient.
1.2 Declaring and Initializing a Pointer
To declare a pointer, use the asterisk
* symbol. To get the address of a variable, use the 'address-of' & operator.int age = 25; // A normal integer variable
int *ptr = &age; // 'ptr' is a pointer that stores the address of 'age'1.3 Dereferencing a Pointer
To access or change the actual value stored at the memory address the pointer is holding, we use the
* operator again. This is called Dereferencing.printf("Address of age: %p", ptr); // Prints something like 0x7ffe...
printf("Value of age: %d", *ptr); // Prints 25
*ptr = 30; // Changes the original 'age' variable to 301.4 Pointer Types
- Null Pointer: A pointer that points to nothing (
int *p = NULL;). Used to prevent accidental memory corruption. - Wild Pointer: An uninitialized pointer. It points to a random memory location and is highly dangerous.
- Dangling Pointer: A pointer that points to a memory location that has already been deleted or freed.
- Void Pointer: A generic pointer that can point to any data type (
void *ptr;).
2. Structures (struct)
Arrays can only hold data of the same type. A Structure is a user-defined data type that groups together variables of different data types under a single name.
2.1 Defining and Declaring a Structure
// 1. Defining the structure template
struct Student {
int rollNo;
char name[50];
float marks;
};
int main() {
// 2. Creating a structure variable
struct Student s1;
// 3. Accessing members using the dot (.) operator
s1.rollNo = 101;
s1.marks = 89.5;
return 0;
}2.2 Array of Structures
Just like an array of integers, you can create an array of structures to store records for multiple entities (e.g., a database of 100 students).
struct Student class10[100];2.3 Pointers to Structures
If you have a pointer pointing to a structure, you cannot use the dot
. operator to access its members. You MUST use the Arrow Operator (->).struct Student s1;
struct Student *ptr = &s1;
ptr->rollNo = 101; // Using arrow operator3. Unions
A Union is similar to a structure (it groups different data types), but with one major difference: Memory Sharing.
- In a
struct, every member gets its own separate memory space. Total size = sum of all members. - In a
union, all members share the exact same memory location. Total size = size of the largest member. - Because they share memory, altering one member of a union will overwrite the data of the other members. You can only use one member at a time.
union Data {
int i;
float f;
char str[20];
}; // Size of this union is 20 bytes (size of the largest member, the char array)4. typedef
The
typedef keyword is used to give a new, shorter nickname to an existing data type.typedef unsigned long int ULI;
ULI distance = 500000; // Much easier to write than 'unsigned long int'Course4All Editorial Board
Verified ExpertSubject Matter Experts
Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.