🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Row Pointers for 2D Arrays
Chapter 8 · Pointers · Walk a matrix through a table of row addresses, and build rows of different lengths
Topics covered in this free C tutorial
This lecture of our free C programming course joins two ideas you already know: the two-dimensional array and the array of pointers. The result is row pointers for 2D arrays, a table in which every element holds the address of the first element of one row. With it you can index a matrix as row[i][j], exchange whole rows by swapping two addresses, and even build rows of different lengths. These are practical skills for embedded C programming, where frames, buffers and lookup pages rarely share one fixed size.
What You Will Learn
- Why
grid[i]can be stored in a pointer, and what type it has - How to build row pointers for 2D arrays and print a matrix through them
- How
row[i][j],*(row[i] + j)and*(*(row + i) + j)are the same access - How this differs from the way a real two-dimensional array finds its elements
- How to build a jagged array in C with rows of different lengths
- The compiler errors you will see when the types do not match
Prerequisites
Before you start
Make sure you have completed the lecture on the array of pointers in C, and the earlier lectures on two-dimensional arrays and pointer arithmetic. You should know that a 2D array is stored row after row in one continuous block of memory.
What Are Row Pointers for 2D Arrays?
Take a matrix with three rows and four columns:
int grid[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
C treats grid as an array of three elements, and each element is itself an array of four ints. So grid[1] is a whole row. Whenever an array name appears in an expression, it decays into a pointer to its first element, so grid[1] becomes &grid[1][0], which has type int *. That is exactly the type our pointer table can store.
int *row[3];
row[0] = grid[0];
row[1] = grid[1];
row[2] = grid[2];
Now row[i] holds the address of the first element of row i, and the matrix can be reached through the table.
row[3] aims at the three rows of grid[3][4]
Row Pointers for 2D Arrays: Memory Layout
The matrix itself is not moved or copied. It stays as one continuous block of twelve integers. The pointer table is a second, separate object that records where each row begins. If an int is 4 bytes, the three stored addresses are 16 bytes apart because each row holds four ints.
| Row pointer | Offset of the row from the start of grid | First value in that row |
|---|---|---|
row[0] | 0 bytes | 1 |
row[1] | 16 bytes | 5 |
row[2] | 32 bytes | 9 |
How row[i][j] Reaches an Element
The subscript operator is only a shorthand for pointer arithmetic. Follow the access row[1][2] one move at a time:
| Expression | Type | Meaning |
|---|---|---|
row | int ** | address of the first slot of the pointer table |
row + 1 | int ** | address of slot 1 (moves by one pointer size) |
*(row + 1) | int * | the stored address, same as row[1] |
*(row + 1) + 2 | int * | address of the element two places along that row (moves by one int) |
*(*(row + 1) + 2) | int | the value, same as row[1][2] |
So row[i][j], *(row[i] + j) and *(*(row + i) + j) are three spellings of one operation. The sentence to remember: first pick the row address from the table, then step along that row.
Example 1: Printing a Matrix Through Row Pointers
#include <stdio.h>
int main(void)
{
int grid[3][4] = {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
int *row[3];
for (int i = 0; i < 3; i++) {
row[i] = grid[i]; /* grid[i] decays to &grid[i][0] */
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%3d", row[i][j]);
}
printf("\n");
}
printf("row[1][2] = %d\n", row[1][2]);
printf("*(*(row + 1) + 2) = %d\n", *(*(row + 1) + 2));
printf("*(row[1] + 2) = %d\n", *(row[1] + 2));
printf("row[1] - row[0] = %td ints\n", row[1] - row[0]);
return 0;
}
gcc -std=c11 -Wall -Wextra -pedantic -o rowptr_grid ep_rowptr_grid.c
./rowptr_grid
Expected output:
1 2 3 4
5 6 7 8
9 10 11 12
row[1][2] = 7
*(*(row + 1) + 2) = 7
*(row[1] + 2) = 7
row[1] - row[0] = 4 ints
The nested loop never mentions grid; everything goes through row. The three printed expressions all give 7, which proves they are equivalent. The last line shows that row[1] - row[0] is 4: subtracting two int * values counts elements, not bytes, and one row contains four ints.
Real 2D Array vs Row Pointers for 2D Arrays
The syntax x[i][j] looks identical for both, but the machine does different work.
| Feature | Real int grid[3][4] | Row pointer table |
|---|---|---|
| Storage | one continuous block | separate pointer table plus the row data |
Finding [i][j] | one multiplication-and-add: i * 4 + j elements | one extra memory load to fetch the row address, then add j |
| Row length | every row must be the same | each row may differ |
| Swapping two rows | copy 4 ints for each row | swap two pointers |
Fit for DMA or one memcpy | yes, it is contiguous | only if the rows happen to be contiguous |
| Type after decay | int (*)[4] | int ** |
Example 2: Row Pointers for 2D Arrays With Different Lengths
A true matrix forces every row to the same width. A jagged array in C does not. Here three data frames have 3, 2 and 5 samples. We keep one pointer per frame and a parallel array of lengths, because the pointers alone cannot tell us where a row ends.
Jagged rows: one pointer per frame, a length per frame
#include <stdio.h>
#include <stddef.h>
int main(void)
{
int frame_a[] = {10, 20, 30};
int frame_b[] = {7, 14};
int frame_c[] = {5, 15, 25, 35, 45};
int *frame[3] = {frame_a, frame_b, frame_c};
size_t len[3] = {3, 2, 5};
for (size_t i = 0; i < 3; i++) {
int sum = 0;
printf("frame %zu:", i);
for (size_t j = 0; j < len[i]; j++) {
printf(" %d", frame[i][j]);
sum += frame[i][j];
}
printf(" (sum = %d)\n", sum);
}
printf("rectangular int[3][5] : %zu bytes\n", sizeof(int[3][5]));
printf("ragged data only : %zu bytes\n",
sizeof(frame_a) + sizeof(frame_b) + sizeof(frame_c));
printf("ragged pointer table : %zu bytes\n", sizeof(frame));
return 0;
}
gcc -std=c11 -Wall -Wextra -pedantic -o rowptr_ragged ep_rowptr_ragged.c
./rowptr_ragged
frame 0: 10 20 30 (sum = 60)
frame 1: 7 14 (sum = 21)
frame 2: 5 15 25 35 45 (sum = 125)
rectangular int[3][5] : 60 bytes
ragged data only : 40 bytes
ragged pointer table : 24 bytes
Notice the memory report. A rectangular int[3][5] needs 60 bytes, while the jagged version needs 40 bytes of data plus a 24-byte pointer table on this 64-bit machine, which is 64 bytes in total. That is slightly more, not less. On a 32-bit microcontroller the pointer table shrinks to 12 bytes and the total becomes 52 bytes, which now beats 60. The lesson: row pointers for 2D arrays save memory only when the rows differ enough to outweigh the table, and the answer depends on pointer size.
Typical Uses in C Programming for Embedded Systems
- Variable-length frames: serial or CAN messages whose payload sizes differ, indexed by message number.
- Per-channel sample windows: each channel keeps a different number of samples.
- Menu pages and lookup tables: each page is a row with its own number of entries.
- Cheap row exchange: sorting or pivoting the rows of a matrix by swapping addresses rather than copying every element.
Common Mistakes With Row Pointers
The most frequent error is trying to initialize the pointer table directly from the 2D array name. Compile this on purpose to see what happens:
#include <stdio.h>
int main(void)
{
int grid[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int *row[3] = grid; /* wrong: grid decays to int (*)[4] */
int **pp = grid; /* wrong: int ** is not int (*)[4] */
printf("%d %d\n", row[0][0], pp[0][0]);
return 0;
}
progs/bad_rowptr.c: In function 'main':
progs/bad_rowptr.c:7:19: error: invalid initializer
7 | int *row[3] = grid; /* wrong: grid decays to int (*)[4] */
| ^~~~
progs/bad_rowptr.c:8:19: warning: initialization of 'int **' from incompatible pointer type 'int (*)[4]' [-Wincompatible-pointer-types]
8 | int **pp = grid; /* wrong: int ** is not int (*)[4] */
| ^~~~
After decay, grid has the type int (*)[4], a pointer to a row of four ints. That is neither an array of int * nor an int **. The first line is a hard error. The second is a warning in GCC 13 and is promoted to an error in newer compilers, because the two types hide different memory layouts and using one as the other reads garbage.
| Mistake | Effect | Fix |
|---|---|---|
Passing grid to a parameter declared int ** | wrong type; the function misreads the data | declare int (*m)[4], or build a row pointer table first |
| Forgetting each row’s length | loops run past the end of short rows | keep a parallel length array or a sentinel value |
| Leaving a slot unassigned | dereferencing an uninitialized pointer | fill every slot, or start with = {0} and check for NULL |
| Assuming rows are contiguous | a single memcpy of the whole structure misses data | copy row by row, or use a real 2D array |
| Confusing subtraction results | pointer difference is in elements, not bytes | cast to char * when you need bytes |
Embedded warning. If a peripheral such as a DMA controller will fill the whole matrix, use a real contiguous 2D array. A table of row pointers does not guarantee the rows sit next to each other.
Best Practices, Performance and Security
- Choose a real 2D array when every row has the same length and you want one contiguous block.
- Choose row pointers for 2D arrays when rows differ in length, when you need to reorder rows cheaply, or when rows live in separate buffers.
- Each access through a pointer table adds one load. Inside a hot loop, copy
row[i]into a localint *once and index that. - Always store row lengths next to the pointers and check every index against them. An out-of-range index on a jagged table is a classic source of buffer overruns.
- Use
const int *rows for tables placed in flash so that accidental writes are caught at compile time.
Summary and Key Takeaways
Key takeaways
grid[i]decays to anint *, so it can be stored in an array of pointers.row[i][j]means: load the row address, then stepjelements along it.row[i][j]equals*(row[i] + j)equals*(*(row + i) + j).- A real 2D array and a pointer table share the same syntax but not the same type or layout.
- Jagged rows need a length array; the pointers alone cannot mark where a row ends.
- Memory saved by a jagged array depends on the size of a pointer.
Conclusion
Row pointers for 2D arrays give you flexibility that a rigid matrix cannot, at the price of one extra memory load and some careful bookkeeping. You now know why the compiler rejects int *row[3] = grid;, how three equivalent expressions reach the same element, and how to build a jagged table whose rows differ in length. The next lecture in this free embedded C course introduces the void pointer, the one pointer type that can hold the address of anything.
Frequently Asked Questions
What are row pointers for 2D arrays?
They are an array of pointers where each element holds the address of the first element of one row of a two-dimensional array. You can then access the matrix as row[i][j] through the table.
How do I assign a 2D array row to a pointer?
Use the row name: p[i] = grid[i];. The name grid[i] decays to a pointer to the first element of that row, whose type is int *.
Why can I not write int *row[3] = grid;?
The name grid decays to int (*)[4], a single pointer to a row of four ints, and that cannot initialize three separate int * elements. Assign the rows one by one in a loop.
What is a jagged array in C?
It is a table of pointers where each pointer aims at a row of a different length. C has no special type for it; you build it from an array of pointers and keep the lengths yourself.
Is a pointer table faster than a real 2D array?
Usually not for reading. The pointer table needs an extra memory load per access, and its rows may not be contiguous. It wins when you reorder rows often or need rows of different sizes.
Can I pass a 2D array to a function expecting int **?
No. A real 2D array decays to int (*)[N], which is a different type from int **. Either declare the parameter as int (*m)[4] or build a row pointer table and pass that.
Are the rows of a pointer table guaranteed to be next to each other?
No. Each pointer may aim anywhere. Only a genuine 2D array guarantees contiguous storage, which matters when you use DMA or memcpy on the whole matrix.
Next up: the void pointer in C
Row pointers let you treat a matrix as a list of rows. Next, this free C tutorial introduces the generic pointer, which can hold the address of any object.

2 Comments