Influence Of Anabolic Steroids Dianabol, Steranabol And Deca-Durabolin On Metabolism Of Some Nitrogen Compounds In Rabbits
What 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
int ptr = malloc(sizeof(int) 100); // allocate 100 ints
```
`malloc` returns a memory address (the pointer). That’s the reference.
- Dereferencing
ptr0 = 42; // write to first element
int x = ptr50; // read from 51st element
```
- Freeing
free(ptr); // release the memory block
```
After `free`, the pointer still holds the old address, gitea.potatox.net but that memory is no longer valid. It’s common practice to set it to `NULL` afterwards:
```c
ptr = NULL;
```
- Passing by Reference
```c
void doubleArray(int arr, size_t len)
for(size_t i=0;i
```
---
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.