-
Notifications
You must be signed in to change notification settings - Fork 0
Pointers and Arrays
Derek Snider edited this page May 19, 2026
·
1 revision
C-style pointers work as expected:
int x = 42;
int *p = &x;
cout << *p << endl; // 42
*p = 100;
cout << x << endl; // 100char *s = "hello";
int *ip;
void *vp;
char **argv; // pointer to pointerstruct Point { int x; int y; };
Point p = { 10, 20 };
Point *ptr = &p;
cout << ptr->x << endl; // 10
// chained access
// a->b->c, a->b.c, a.b.c all workint nums[5] = { 10, 20, 30, 40, 50 };
cout << nums[0] << endl; // 10
cout << nums[4] << endl; // 50
char buf[256];
sprintf(buf, "hello %s", "world");
puts(buf);char name[32] = "Alice";
char greeting[] = "Hello"; // size inferred from literalint grid[3][3];
grid[0][0] = 1;
grid[1][1] = 5;Works on arrays, pointers, strings, and containers:
int arr[3] = { 10, 20, 30 };
cout << arr[1] << endl; // 20
char *s = "hello";
cout << s[0] << endl; // h
// also works with map
map<string, int> ages;
ages["Alice"] = 30;
cout << ages["Alice"] << endl;int main(int argc, char **argv) {
for (int i = 0; i < argc; i++)
puts(argv[i]);
return 0;
}argv[i] reads the i-th char * directly via raw-pointer subscripting.
- Structs & Classes — struct pointers, member access
- Data Types — all types
- Functions — function pointers
- ← Back to Home