pointers (1)
pointers (1)
1. Understanding Pointers
Example:
int x = 10;
In this example:
2. Pointer Declaration
A pointer is declared using the * operator before the variable name. The data type of
the pointer must match the type of the variable it points to.
Syntax:
data_type *pointer_name;
Example:
Example:
int a = 5;
4. Dereferencing a Pointer
Dereferencing a pointer means accessing the value stored at the memory location
the pointer is pointing to. This is done using the * operator (not to be confused with
pointer declaration).
Example:
int a = 5;
*p gives the value at the memory address held by p, which in this case is 5.
5. NULL Pointer
A NULL pointer is a special pointer that does not point to any valid memory location.
It is used to indicate that the pointer is not currently referencing anything.
Example:
int *p = NULL;
6. Pointer Arithmetic
Example:
int arr[3] = {10, 20, 30};
int *p = arr;
p++;
p++;
When p++ is executed, it doesn't just increment the pointer by 1; it moves the
pointer to the next integer's memory location (typically 4 bytes later if it's an
int).
An array name in C is a constant pointer to the first element of the array. The
elements of the array can be accessed using pointers.
Example:
A pointer to a pointer is a pointer that holds the address of another pointer. This can
be useful in various cases like dynamically allocating memory for a 2D array or
passing pointers to functions.
Example:
int a = 10;
#include <stdio.h>
int main() {
int a = 10;
return 0;
Example:
#include <stdio.h>
return a + b;
int main() {
return 0;
In C, dynamic memory allocation is done using the malloc(), calloc(), realloc(), and
free() functions. These functions are used to allocate and deallocate memory at
runtime, and they return a pointer to the allocated memory.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
return 0;
2. Dangling Pointer: A pointer that points to a memory location that has been
deallocated or no longer valid.
*p = 10;
Pointers and Arrays: Array names are pointers to their first element. arr[i] is
equivalent to *(arr + i).
Pointer to Pointer: A pointer can store the address of another pointer (int
**pp).
Question:
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("%d", *p);
return 0;
Answer:
Output: 10
Explanation:
2. Pointer Arithmetic
Question:
#include <stdio.h>
int main() {
int *p = arr;
return 0;
}
Answer:
Output: 20
Explanation:
Question:
#include <stdio.h>
int main() {
return 0;
Answer:
Output: 4
Explanation:
*(ptr + 3) gives the value at the 4th position (index 3), which is 4.
4. Null Pointer
Question:
Answer:
A null pointer is a pointer that does not point to any valid memory location. In C, it is
typically represented as NULL. A pointer initialized to NULL means it is not assigned
any valid address.
Example:
Question:
#include <stdio.h>
int main() {
int a = 5;
int *p = &a;
printf("%d", **q);
return 0;
Answer:
Output: 5
Explanation:
**q dereferences q twice, first to access p and then to access the value a
holds, which is 5.
Question:
#include <stdio.h>
int main() {
int x = 5;
int *p = &x;
p++;
printf("%d", *p);
return 0;
Answer:
The first print statement will output 5, but the second one results in undefined
behavior because after p++, it points to a memory location beyond x. Accessing this
location is unpredictable.
Question:
#include <stdio.h>
*p = 10;
int main() {
int x = 5;
changeValue(&x);
printf("%d", x);
return 0;
Answer:
Output: 10
Explanation:
8. Pointer Size
Question:
#include <stdio.h>
int main() {
int *p;
printf("%lu", sizeof(p));
return 0;
Answer:
Output: 8
Explanation:
On a 64-bit machine, the size of any pointer is 8 bytes because the addresses
are 64-bit long.
9. Pointer to a Function
Question:
Which of the following declares a pointer to a function that returns an int and takes
an int as an argument?
Answer:
int (*ptr)(int);
Explanation:
This declares ptr as a pointer to a function that takes an int as a parameter
and returns an int.
Question:
What is the correct way to dynamically allocate memory for an array of 5 integers in
C?
Answer:
Explanation:
malloc allocates memory for 5 integers (5 * sizeof(int)), and the cast (int *)
converts the void pointer returned by malloc to an integer pointer.
Question:
Answer:
A dangling pointer is a pointer that points to a memory location that has been freed
or deallocated. Accessing memory through a dangling pointer can lead to undefined
behavior.
Example:
*p = 100;
Question:
int main() {
char *p = str;
return 0;
Answer:
Output: e
Explanation: