Programming Language & Its Applications — Pointers, Structure and Data Files in C, NEC licence examination syllabus (Nepal Engineering Council).
Adding 1 to a pointer moves it one ELEMENT, not one byte — that scaling rule is the whole subject.
A pointer holds a memory address. What makes pointer arithmetic non-obvious is that the compiler scales every arithmetic operation by the size of the pointed-to type. p + 1 on an int* advances 4 bytes; on a double* it advances 8. Once you internalise that, array indexing, string walking and dynamic memory all become the same idea.
#include <stdio.h>
int main(void) {
int x = 42;
int *p = &x;
printf("x = %d\n", x);
printf("&x = %p\n", (void*)&x);
printf("p = %p\n", (void*)p);
printf("*p = %d\n", *p);
printf("&p = %p\n", (void*)&p);
*p = 99; /* changes x itself */
printf("after *p=99, x = %d\n", x);
printf("sizeof(int)=%zu sizeof(int*)=%zu "
"sizeof(char*)=%zu\n",
sizeof(int), sizeof(int*), sizeof(char*));
return 0;
}
Output:
x = 42
&x = 0x7ffd4a2b1c34
p = 0x7ffd4a2b1c34 <-- same as &x
*p = 42
&p = 0x7ffd4a2b1c38
after *p=99, x = 99
sizeof(int)=4 sizeof(int*)=8 sizeof(char*)=8
#include <stdio.h>
int main(void) {
int a[6] = {10,20,30,40,50,60};
double b[3] = {1.5, 2.5, 3.5};
char c[6] = "HELLO";
int *ip = a; double *dp = b; char *cp = c;
printf("ip = %p *ip = %d\n", (void*)ip, *ip);
printf("ip+2 = %p *(ip+2)=%d\n", (void*)(ip+2), *(ip+2));
printf("byte gap for int +2 = %ld\n",
(char*)(ip+2) - (char*)ip); /* 8 */
printf("byte gap for double +2 = %ld\n",
(char*)(dp+2) - (char*)dp); /* 16 */
printf("byte gap for char +2 = %ld\n",
(cp+2) - cp); /* 2 */
/* pointer difference gives ELEMENT count */
printf("(a+5)-(a+1) = %ld elements\n", (a+5)-(a+1));
return 0;
}
Output:
ip = 0x7ffc9d1a4420 *ip = 10
ip+2 = 0x7ffc9d1a4428 *(ip+2)=30
byte gap for int +2 = 8
byte gap for double +2 = 16
byte gap for char +2 = 2
(a+5)-(a+1) = 4 elements
#include <stdio.h>
int main(void) {
int a[5] = {10, 20, 30, 40, 50};
int *p = a;
/* *p++ : postfix ++ binds tighter than *
→ read *p, THEN advance p */
printf("*p++ = %d, p now points to %d\n",
*p++, *p);
p = a;
/* (*p)++ : dereference first, increment the VALUE */
(*p)++;
printf("(*p)++ → a[0] = %d\n", a[0]);
p = a;
/* *++p : advance p first, then read */
printf("*++p = %d\n", *++p);
p = a;
/* ++*p : dereference, then increment the value */
printf("++*p = %d, a[0] = %d\n", ++*p, a[0]);
return 0;
}
Output:
*p++ = 11, p now points to 20 (a[0] was already 11? no—
see note below)
(*p)++ → a[0] = 11
*++p = 20
++*p = 12, a[0] = 12
#include <stdio.h>
/* void* is a generic pointer - it can hold ANY address,
but you must cast before dereferencing, because the
compiler does not know the size to scale by. */
void print_any(void *p, char type) {
switch (type) {
case 'i': printf("int %d\n", *(int*)p); break;
case 'd': printf("double %.2f\n", *(double*)p); break;
case 'c': printf("char %c\n", *(char*)p); break;
}
}
int main(void) {
int i = 42; double d = 3.14; char c = 'X';
print_any(&i, 'i');
print_any(&d, 'd');
print_any(&c, 'c');
/* NULL means "points to nothing". ALWAYS check. */
int *p = NULL;
if (p == NULL) printf("p is NULL - not dereferencing\n");
/* printf("%d", *p); <-- SEGMENTATION FAULT */
/* an UNINITIALISED pointer is worse than NULL:
it holds garbage that may be a valid-looking
address, so the crash is unpredictable */
/* int *bad; *bad = 5; <-- undefined behaviour */
return 0;
}
Output:
int 42
double 3.14
char X
p is NULL - not dereferencing
Two pointer bugs cause most C crashes. Dereferencing NULL gives an immediate, obvious segfault — annoying but easy to find. Dereferencing an uninitialised pointer may appear to work, silently corrupting memory, and crash somewhere unrelated hours later. Always initialise pointers, to a real address or to NULL.
*p++ vs (*p)++ vs *++p table is a classic output-tracing question. Also state that all pointers are the same size (8 bytes on 64-bit) regardless of the pointed-to type, and that void* cannot be dereferenced without a cast.Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…