Skip to content

Instantly share code, notes, and snippets.

@smarteist
Created May 28, 2025 06:58
Show Gist options
  • Save smarteist/66c74bf003da35d306f20e8ca5a995e6 to your computer and use it in GitHub Desktop.
Save smarteist/66c74bf003da35d306f20e8ca5a995e6 to your computer and use it in GitHub Desktop.
Example C Pointer
#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;
}
@smarteist
Copy link
Author

smarteist commented May 28, 2025

  • 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.

image

In programming, the terms L-value and R-value refer to different types of expressions:

  1. L-value (Left value): Refers to a variable (can be assigned).

    • An L-value refers to an expression that has a memory address and can appear on the left side of an assignment.
    • It represents a variable that can be modified.
    • Example: var in var = 10; is an L-value because it can be assigned a value.
  2. R-value (Right value): Refers to a value or expression (cannot be assigned).

    • An R-value refers to an expression that does not have a memory address and typically represents a temporary value.
    • It can appear on the right side of an assignment but cannot be assigned a value.
    • Example: 10 in var = 10; is an R-value because it is a literal value that cannot be modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment