This document provides an overview of pointers and strings in C including:
- Memory access and pointers allow programs to read and write to specific memory locations referenced by addresses.
- Pointers are variables that store memory addresses and allow direct access to the data at those addresses. They must be declared with a specific data type.
- Operators like & and * are used to get the address of a variable and dereference a pointer to access the value at a memory location.
- The document discusses pointer declaration, initialization, passing arguments by value vs. reference, and null pointers.
2. TOPICS: POINTER
• MEMORY ACCESS AND POINTERS
• POINTER BASICS
• DECLARING
• INITIALIZING
• DEREFERENCING A POINTER
• PARAMETER PASSING MECHANISM
• OPERATIONS ON POINTER
3. MEMORY
ACCESS
Memory access refers to
the ability to read from or
write to specific locations
in the computer's memory.
The memory in a computer
is organized into a
sequence of bytes, each
with a unique address.
4. MEMORY ACCESS
• Every variable is a memory
location and every memory
location has its address defined
which can be accessed through
ampersand (&) operator, which
denotes an address in memory
5.
6. Address of a:
0x7ffd7218d764
Address of b:
0x7ffd7218d760
Address of c:
0x7ffd7218d75f
#include <stdio.h>
int main()
{
int a = 10;
double b = 20.5;
char c = 'A';
printf("Address of a: %pn", &a);
printf("Address of b: %pn", &b);
printf("Address of c: %pn", &c);
return 0;
}
7. Variables
• Variables in C are named storage locations that hold values of a
specific data type.
• When you declare a variable, the compiler allocates memory to
store its value, and you can access that memory using the
variable's name.
int age = 25;
// Declaring an integer variable 'age' and assigning it a value
8. Pointer
• Pointers are variables that store memory addresses. They allow
direct access to the memory location they point to.
• Pointers are declared with a specific data type, indicating the
type of data stored at the memory location.
• Example, int a=10;
• int *ptr; // Declaration of an integer pointer
10
10
a
Location
Value
Name of the variable
80F Address of the location
9. Pointer
• Value 10 can be accessed by using the variable name or the
address 80F
• Memory address are simply numbers can be assigned to some
other variable.
• The variable that holds the memory address is called pointer
variable
10
10
a
80F
10
80F
ptr
82C
Pointer name
Address of the
pointer
Address
of a
10. Pointer
• Value 10 can be accessed by using the variable name or the
address 80F
• Memory address are simply numbers can be assigned to some
other variable.
• The variable that holds the memory address is called pointer
variable
10
10
a
80F
10
80F
ptr
82C
Pointer name
Address of the
pointer
Address
of a
11. Address of operator
• The address-of operator (&) is used to get the memory address
of a variable.
int age = 25;
int *ptr = &age; // Assigning the address of 'age' to the pointer
'ptr'
12. Dereferencing operator
• The dereference operator (*) is used to access the value stored
at a memory address pointed to by a pointer.
int age = 25;
int *ptr = &age;
printf("Value at the memory address pointed by ptr: %dn", *ptr);
13. • Sample Program 1
• Pointer declaration and initialization
• Sample Program 2
• Pointer Basics
14. Void pointer
• Pointer that is not associated with any data type.
• It can point to objects of any type but doesn't provide
information about the type of data it points to.
• Void pointer sample Program
• Syntax:
• Void *pointer name;
15. Null pointer
• A null pointer in C is a pointer that does not point to any
memory location.
• It is a special constant value (often represented as 0 or NULL)
that is assigned to a pointer variable to indicate that it does not
currently point to a valid memory address.
• Null pointer sample program
• Syntax:
• Int *p=NULL;
17. Pass by value
• The value of the argument is passed to the function.
• Changes made to the parameter inside the function do not
affect the original value.
• Sample Pgm
18. #include <stdio.h>
void incrementByValue(int x) {
x++;
printf("Inside function: x = %dn", x);
}
int main() {
int num = 5;
printf("Before function call: num = %dn", num);
incrementByValue(num);
printf("After function call: num = %dn", num);
return 0;
}
19. Pass by reference
• The address of the argument is passed to the function using a
pointer.
• Changes made to the parameter inside the function affect the
original value.
• Sample Pgm
20. #include <stdio.h>
void incrementByReference(int *x) {
(*x)++;
printf("Inside function: *x = %dn", *x);
}
int main() {
int num = 5;
printf("Before function call: num = %dn", num);
incrementByReference(&num);
printf("After function call: num = %dn", num);
return 0;
}