PART B · PRESCRIBED EXERCISE 03
Demonstrate pointers in C
Understand the algorithm. Run the code.
Explain the result with confidence.
LAB B03 · 01
Understand the task.
Aim: Demonstrate pointers in C.
Pointer access. A pointer variable holds a pointer value identifying an object. Unary & obtains an object’s address, and unary * accesses the object reached through a valid pointer.
Review the related theory in Unit 4.
LAB B03 · 02
Know what goes in and what comes out.
Two integers from -100 to 100: an original x value and a replacement value. A fixed three-element array then demonstrates pointer arithmetic.
Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.
LAB B03 · 03
Plan the steps before writing code.
- Read and validate the original and replacement values.
- Assign &x to p and verify that p identifies x.
- Read x through *p.
- Assign the replacement through *p and display the changed x.
- Retarget p to the array’s first element and access each element using *(p + i).
LAB B03 · 04
One complete program. Ready to run.
#include <stdio.h>
int main(void)
{
int x, replacement, i;
int values[3] = {5, 10, 15};
int *p;
if (scanf("%d%d", &x, &replacement) != 2 || x < -100 || x > 100 || replacement < -100 || replacement > 100) {
puts("Enter the original and replacement values from -100 to 100.");
return 1;
}
p = &x;
printf("Pointer targets x: %s\n", p == &x ? "YES" : "NO");
printf("Before: x = %d, *p = %d\n", x, *p);
*p = replacement;
printf("After: x = %d, *p = %d\n", x, *p);
p = values;
printf("Array through pointer:");
for (i = 0; i < 3; i++) printf(" %d", *(p + i));
putchar('\n');
return 0;
}
Pointer targets x: YES
Before: x = 12, *p = 12
After: x = 25, *p = 25
Array through pointer: 5 10 15
Compile on your own computer
gcc B03-pointer-demonstration.c -o programRun the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.
LAB B03 · 05
Connect each statement to its purpose.
- p = &x stores an address, whereas *p = replacement changes x itself.
- The equality test demonstrates the address relationship without making a particular printed address part of the expected output.
- When p points to values, p + i moves by int elements, not by a manually chosen number of bytes.
- The loop uses only indexes 0–2, so every dereference is within the array. Unit 4 separately demonstrates portable %p printing and the one-past endpoint.
LAB B03 · 06
Follow the sample calculation.
| Step or component | What happens |
|---|---|
| p = &x | p targets the x object holding 12. |
| *p = 25 | x becomes 25; the address in p stays the same. |
| p = values | p now targets values[0]. |
| *(p + i) | Read 5, 10 and 15 from indexes 0, 1 and 2. |
LAB B03 · 07
Check more than one input.
| Try this input or case | Expected behaviour |
|---|---|
| 0 -100 | x changes from 0 to -100. |
| 7 7 | Writing the same value leaves x equal to 7. |
| 101 2 | Rejected before pointer access. |
Time and storage
O(1) time here because the array always has three elements; O(1) extra working space.
Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.
LAB B03 · 08
Use the real C compiler.
Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.
Load the compiler when you’re ready to practise.
Your C workspace
Edit real C code and see real compiler output.
Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.
View / copy the selected source
#include <stdio.h>
int main(void)
{
int x, replacement, i;
int values[3] = {5, 10, 15};
int *p;
if (scanf("%d%d", &x, &replacement) != 2 || x < -100 || x > 100 || replacement < -100 || replacement > 100) {
puts("Enter the original and replacement values from -100 to 100.");
return 1;
}
p = &x;
printf("Pointer targets x: %s\n", p == &x ? "YES" : "NO");
printf("Before: x = %d, *p = %d\n", x, *p);
*p = replacement;
printf("After: x = %d, *p = %d\n", x, *p);
p = values;
printf("Array through pointer:");
for (i = 0; i < 3; i++) printf(" %d", *(p + i));
putchar('\n');
return 0;
}
LAB B03 · 09
Explain why the program works.
How are p and *p different?
p is a pointer value; *p designates the target object and can read or change its value.
Does p = values copy the array?
No. The array expression converts to a pointer to its first element.
Is any non-null pointer safe to dereference?
No. Correct target type, bounds, lifetime and access permissions are also required.
LAB B03 · 10
Build the next skill.
Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.