Lecture File
pointers.pdf
C Core
Pointer semantics, array decay, input handling, and string safety.
File/lecture name: pointers.pdf
Main theme: C Pointers, Arrays, and Strings: Memory addresses, input handling, and string manipulation.
Prereqs assumed by slides: Basic C syntax (variables, loops, functions), understanding of stack memory, and familiarity with printf.
What this lecture enables you to do:
&, and dereferencing using *.scanf): Reading integers, handling non-integer input errors, and detecting EOF.sizeof operator, iteration, and initialization.const correctness, and string library functions (strlen, strcat).| Name | Definition | Why it matters | Common confusion / misconception | Tiny example |
|---|---|---|---|---|
| Pointer | A data type that stores a memory address rather than a value. | Allows direct manipulation of memory and passing data by reference. | Confusing the pointer (address) with the value it points to. | int *ptr; |
| Dereference | Using * to access the value stored at the memory address held by a pointer. |
Essential for reading or modifying data via a pointer. | Thinking *ptr is a multiplication operator. |
*ptr = 10; |
| Array Decay | When an array is passed to a function, it decays into a pointer to its first element. | Explains why sizeof on a function parameter returns pointer size, not array size. |
Believing int arr[] and int *arr are identical in all contexts. |
void f(int arr[]) |
scanf Return |
Returns the number of items successfully read. | Critical for error handling; allows detecting non-integer input. | Assuming it always returns 1 or ignores failures. | if (scanf("%d", &x) == 1) |
const |
A type modifier indicating data should not be modified. | Prevents accidental writes to read-only memory (like string literals). | Confusing const int * (ptr to const) with int * const (const ptr). |
const char *s = "text"; |
| Null-Terminated String | A C string is an array of characters ending with \0 (ASCII 0). |
Required for functions like strlen to know where the string ends. |
Thinking the length of the literal is the string length. | "abc" is 3 chars, "abc\0" is 4 bytes. |
| EOF | End-of-File indicator, returned by getchar or checked via feof. |
Signals the end of input stream (e.g., Ctrl+D). | Checking feof before reading vs. after reading. |
while (!feof(stdin)) |
1. How to declare and assign a pointer safely
int *ptr;ptr = 1000;) unless you know the address is valid.ptr = &x;int x = 10;
int *ptr = &x; // Safe
2. How to handle scanf input errors robustly
scanf immediately after the call.int ret = scanf("%d", &x);
if (ret != 1) {
char c;
scanf("%c", &c); // Consume bad input
}
3. How to iterate over an array safely
sizeof(arr) / sizeof(arr[0]) (only works for local arrays).i from 0 to len - 1.unsigned int len = sizeof(arr) / sizeof(arr[0]);
for (unsigned int i = 0; i < len; ++i) { ... }
4. How to pass arrays to functions
int arr[] is treated as int *arr.void f(int arr[], unsigned int size)).sizeof(arr) inside the function to get the array length.void printArray(int arr[], unsigned int size) {
for (unsigned int i = 0; i < size; ++i) {
printf("%d\n", arr[i]);
}
}
5. How to declare string literals correctly
char myS[] = "text"; for stack-allocated, modifiable strings.const char *s = "text"; for pointers to string literals (read-only).s[0] = 'X' crashes).char myS[] = "On Stack"; // Modifiable
const char *s = "Text"; // Read-only
| Pattern Name | When to use it | Correct minimal example | Common bug + how to avoid it |
|---|---|---|---|
| Pointer to Const | When you want to read data but not modify it. | const int *px = &x; |
Bug: *px = 5; (Compiler error). Fix: Use const. |
| Const Pointer | When the pointer itself should not change address. | int * const py = &y; |
Bug: py = &x; (Compiler error). Fix: Use const on pointer. |
| Input Loop | When reading until EOF or valid input. | while (scanf(...) == 1) { ... } |
Bug: Infinite loop on bad input. Fix: Consume bad char with scanf("%c", &c). |
| Array Decay | When passing arrays to functions. | void f(int arr[]) |
Bug: sizeof(arr) returns pointer size. Fix: Pass size explicitly. |
| String Length | When calculating string length. | strlen(p) |
Bug: sizeof(p) returns pointer size. Fix: Use strlen or sizeof(arr)/sizeof(char) for stack arrays. |
s[0] = 'X'; where s points to a literal.char myS[] = "text"; instead of char *s = "text";.sizeof(arr) returns 4 (or 8) inside a function instead of 40.arr is a pointer, not an array.scanf.scanf fails to read, leaves bad input in stream, loop repeats.&arr and arr print the same address.arr evaluates to address of first element.&arr is address of the array object, arr is address of first element.strlen returns incorrect length for a stack array.sizeof (bytes) with strlen (chars).sizeof(arr)/sizeof(char) for stack arrays, strlen for pointers.scanf returns 0 but program continues.const int *px allows changing px but not *px.const placement.const before * protects the value; const after * protects the pointer.printf("%p\n", ptr) prints a random address.&variable or NULL.arr and p (where p = arr) behave differently in sizeof.arr is an array, p is a pointer.sizeof(arr) only for local arrays.scanf leaves newline character in buffer.scanf("%d") and scanf("%c").scanf("%c", &c) to consume the newline.strcat overflows destination buffer.dest has enough space for src + null terminator.arr and &arr are different in printf.arr decays to pointer, &arr is address of array object.&arr only if you need the address of the array object itself.const variable left uninitialized.const variables must be initialized.const variables.scanf fails to read integer but loop continues.sizeof on array parameter returns pointer size.printf("%d\n", *ptr); if int x = 10; int *ptr = &x;?
A: 10 (Dereferencing ptr accesses the value of x).scanf("%d", &x) return 0 if the user types "abc"?
A: It returns 0 because no integer was successfully read.char *s = "text"; and char s[] = "text";?
A: s points to a read-only literal (crash if modified); s[] is a modifiable stack array.sizeof(arr) inside a function where arr is a parameter?
A: It returns the size of a pointer (e.g., 4 or 8 bytes), not the array size.scanf?
A: int (number of items successfully read).const char *s mean?
A: Pointer to a constant character (cannot modify the data pointed to).&arr vs arr for an array int arr[5]?
A: arr is the address of the first element; &arr is the address of the array object (different type).scanf?
A: Consume the bad input character(s) after a failed read.\0).sizeof to find the length of a string passed to a function?
A: No, because of array decay; you must pass the size explicitly.feof(stdin) return?
A: True (non-zero) only after an attempted read fails due to EOF.type *name;&variable*pointersizeof(arr) / sizeof(arr[0])sizeof(arr) returns pointer size (use passed size).const char *s = "text"; (Read-only)char s[] = "text"; (Modifiable)scanf Return: Number of items read.EOF: End of file indicator.strlen: Returns length of string (null-terminated).strcat: Concatenates strings (dest must be large enough).const Placement: const int *p (ptr to const) vs int * const p (const ptr).&arr vs arr: arr decays to &arr[0].*.\0 character ending a C string.& returning memory address.stdin (keyboard).malloc, calloc, free are not covered in these slides.fopen, fread, fwrite (only stdin/stdout covered).scanf Error Handling: Slide 16 ("scanf returns the number of successfully read items").sizeof on Arrays: Slide 30 ("The sizeof operator evaluates to the size (in bytes) of its operand").const Usage: Slide 55 ("Pointers to string literals should always be pointers to const char").strlen Implementation: Slide 61 ("Here is one way one could implement strlen").strcmp, strchr, strcat descriptions).Manually curated from summaries/pointers.txt. Use this page as a study aid and cross-check official slides for grading-critical details.