About
About
Influence Of Anabolic Steroids Dianabol, Steranabol And Deca-Durabolin On Metabolism Of Some Nitrogen Compounds In RabbitsWhat Is a "Reference" in Programming?
A reference is simply a pointer that tells the computer where some data lives in memory.
Think of it as a street address for a house.
The reference itself is small (just a few bytes), but by following it you can read or change whatever data sits at that location.
---
1. Why Use References?
Problem How a Reference Helps
Large objects – copying them would waste time and memory Copy only the reference; the object stays in one place
Sharing data – many parts of a program need to see or change the same thing All references point to the same object, so changes are visible everywhere
Dynamic size – you don’t know how big an object will be until run‑time Allocate it once, then use its reference wherever needed
---
2. The Mechanics
Allocation
```c
int ptr = malloc(sizeof(int) 100); // allocate 100 ints
```
`malloc` returns a memory address (the pointer). That’s the reference.
Dereferencing
```c
ptr0 = 42; // write to first element
int x = ptr50; // read from 51st element
```
Freeing
```c
free(ptr); // release the memory block
```
After `free`, the pointer still holds the old address, but that memory is no longer valid. It’s common practice to set it to `NULL` afterwards:
```c
ptr = NULL;
```
Passing by Reference
If you want a function to modify an array or even the pointer itself (e.g., reallocating), you pass the pointer:
```c
void doubleArray(int arr, size_t len)
for(size_t i=0;i="2;
```
---
4. Summary Cheat‑Sheet
Concept Syntax (C) Purpose
Declaration `int x;` Allocate a variable of type int
Definition `int x = 5;` Provide an initial value
Variable name `x`, `myVar1` Identifier for the variable
Address operator `&x` Get memory address (pointer)
Dereference `p` Access value at pointer p
Pointer type `int p;` Holds address of an int
Array name as pointer `arr` is equivalent to `&arr0` Use array in pointer context
---
Example: Using a Variable
#include
int main(void)
int x = 10; // declaration + initialization
printf("x = %d
", x); // print the value of x
// Get address and print it
printf("&x = %p
", (void)&x);
// Use a pointer to access x
int ptr = &x;
printf("ptr = %d
", ptr); // same as printing x
return 0;
Output
x = 10
&x = 0x7fffc5b3a1ec
*ptr = 10
(Addresses will differ each time.)
---
Summary
`int a;` declares an integer variable `a`. It has no initial value until you assign one.
The program can use that variable to store numbers, perform calculations, and pass it around.
This is the foundation for understanding how data is stored in memory in C. In later lessons, we’ll explore initialization (`int a = 5;`), constants (`const int a = 5;`), and more complex types like arrays and structs.