Created
May 28, 2025 06:58
-
-
Save smarteist/66c74bf003da35d306f20e8ca5a995e6 to your computer and use it in GitHub Desktop.
Example C Pointer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
// 1. Declaring a Pointer | |
int *ptr; // Pointer to an integer | |
// 2. Assigning Address to a Pointer | |
int var = 10; | |
ptr = &var; // ptr now holds the address of var | |
// 3. Dereferencing a Pointer | |
int value = *ptr; // value is now 10 (the value of var) | |
// 4. Modifying Value via Pointer | |
*ptr = 20; // var is now 20 | |
// Example Output | |
printf("Value of var: %d\n", var); // Output: 20 | |
printf("Address of var: %p\n", (void*)&var); // Output: Address of var | |
printf("Value via pointer: %d\n", *ptr); // Output: 20 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
int var; declares an integer variable.
var = 10; assigns the value 10 to var.
int *ptr; declares a pointer to an integer.
ptr = &var; assigns the address of var to ptr.
In programming, the terms L-value and R-value refer to different types of expressions:
L-value (Left value): Refers to a variable (can be assigned).
var
invar = 10;
is an L-value because it can be assigned a value.R-value (Right value): Refers to a value or expression (cannot be assigned).
10
invar = 10;
is an R-value because it is a literal value that cannot be modified.