Why Array Names Act Like Pointers | Programming in C

UPI Study › Courses › Programming in C › Free lessons › Lesson 8.4 Free with a free account

Why Array Names Act Like Pointers

Programming in C · Chapter 8: Pointers, Pointer Arithmetic, and String Manipulation via PointersSection 8.4 · lesson 51 of 787 min · free with a free account

← Pointer Arithmetic in CNext lesson: Arrays of Pointers in C →

Earn 3 college credits for Programming in CRecommended for credit by NCCRS · Computer Science 240 · self-paced · one payment, lifetime access

In this lesson

How C array names decay to pointers, and where they differ.

  • Explain how an array name decays to a pointer to its first element
  • Show that a[i] means *(a + i) and that brackets work on pointers
  • Identify three ways an array differs from a pointer: sizeof, assignment, and &
  • Explain why a pointer cannot report the length of the array it points into

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,053 words). Read along, or search it.

Opening Question

What Is Scores Without Brackets

Priya writes scores of two a hundred times a day. So Sam asks her a sneaky question. When you type just scores, with no brackets, what is it? The whole row, she says. She's half right. And the missing half explains why arrays and pointers in C always seem to hide behind each other.

Array Decay

The Name Becomes the Address of Box Zero

Here's the secret. Almost everywhere you use an array's name, C quietly swaps it for the address of the array's first box. This swap is called decay: the array decays into a pointer to box zero. It happens automatically, which is why passing an array to a function in seven point three just worked. So line five stores a plain name in an int pointer, with no ampersand, and it fits. Line seven asks whether scores is the same address as ampersand scores of zero. Build and run. Star first reads box zero, fifty. And the comparison says one, meaning yes: the same address.

Brackets Explained

Brackets Are Pointer Arithmetic in Disguise

Now remember from eight point three: adding one to an int pointer moves one whole int. So scores plus two is the address of box two. And star of scores plus two, in parentheses, is the number inside it. C defines the brackets as exactly that. Scores of two means star of scores plus two. The brackets aren't a second feature, just a friendlier spelling. Add both lines and run it again. Seventy, twice. The number in the brackets says how many boxes to step from the start. That's why counting begins at zero: box zero is zero steps away.

Four Spellings

Brackets Work on Pointers Too

A pointer variable holds an address, just like a decayed array name. So the brackets work on pointers too. Here are four spellings of the same box: scores of i, star of scores plus i, p of i, and star of p plus i. Build and run. Every row shows the same number four times. Use whichever reads best. Most C programmers write p of i for a row, and star p for a single value.

Fun Fact

The Index Can Come First

A fun fact. Since a of two means star of a plus two, and adding works in either order, C also accepts the number first: two of a. It really works. Both print seventy. Please never write it that way.

Quick Check

Brackets Count From Where a Pointer Points

Quick check. p starts at t plus one. Then the program prints p of two, and star t plus two, with no parentheses. What does it print? Pause and predict. It prints 12 5. t decays to the address of box zero, so p starts at box one. p of two counts two more boxes from there: box three, twelve. Star t plus two has no parentheses. It reads box zero, three, and adds two: five. Nine would need p of two to count from the start of t. But brackets on a pointer count from wherever that pointer points.

Key Distinction

Three Ways an Array Is Not a Pointer

But decay doesn't make an array and a pointer the same thing. An array is a row of boxes. A pointer is one box that holds an address. The swap only happens when the name is used in an expression, and it has exceptions. Three matter: sizeof, assignment, and the ampersand. Take them one at a time.

Difference One

Sizeof Sees the Whole Row

First, sizeof. It sees the whole row, not an address. The pointer, on the other hand, is just one address. Build and run. sizeof scores is sixteen bytes: four ints of four bytes. sizeof p is eight, one address, on this Mac. So the box-count trick only works on the array itself, where the compiler knows its size. That's the same trap you met in seven point three. A pointer variable can also move on. After p plus plus, star p is sixty.

Difference Two

An Array Name Cannot Be Assigned

Second, you can't assign to an array name. A pointer can be pointed somewhere new, but an array's name is fixed to its row. Priya hit this one. To restore Sam's saved scores, she wrote scores equals backup. Clang refuses: array type, int of three, is not assignable. Notice it calls scores an array type, because it is. It's the row itself, not a pointer to it, so there's no arrow to redirect. Sam laughed. You can re-aim an arrow, he said, but not a row of lockers. Priya replaced the line with a loop that copies box by box.

Difference Three

Ampersand Gives the Whole Row's Address

Third, the ampersand. Taking the address of the array name gives the address of the whole row, instead of box zero. It starts at the same place, but its one step is the size of the entire row. The casts to char star only let us measure in bytes. Run it. a plus one steps over one int, four bytes. Ampersand a plus one steps over all four ints: sixteen. You'll rarely write ampersand a. But now you won't be fooled when you see it.

Side by Side

A Row of Boxes Versus an Arrow

Here they are, side by side. Same brackets and stars, different things underneath. An array is a row of boxes, and sizeof measures the whole row. A pointer is one box with an address, eight bytes here. A pointer can be re-aimed. An array name can't. So think of it this way: an array becomes a pointer when you use it in an expression, but it isn't one. Keep the row and the arrow separate in your head.

Quick Check

The Box-Count Trick on a Pointer

Quick check. Six lap counts in an array, and a pointer to it. The program tries the box-count trick on both. What does it print on this Mac? Pause and predict. It prints 6 2. And the build already hinted why. Clang warns: sizeof p will return the size of the pointer, not the array itself. On the array, sizeof sees the whole row: twenty-four bytes, divided by four, is six. On the pointer, sizeof sees one address: eight bytes, divided by four, is two. The trick counts nothing useful there. Six, six would need a pointer to remember its row's length. It doesn't, and that's why functions need the length passed in.

Key Takeaways

Becomes a Pointer, but Is Not One

To recap. Used in an expression, an array's name decays into the address of box zero. a of i means star of a plus i, so brackets work on pointers too. But sizeof sees the whole array, an array name can't be assigned, and ampersand a is the address of the whole row. An array becomes a pointer when used. It isn't one.

About Programming in C at UPI Study

Programming in C (Computer Science 240) is a self-paced online course of 78 video lessons in 11 chapters, from pseudocode and your first program to data types, decisions, loops, functions, arrays, strings, pointers, files, structures, dynamic memory and linked lists. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: C vs C++

Why take it here

Why students take Programming in C with UPI Study

How programs work close to the machineProgramming in C teaches how a program uses memory: variables and data types, control flow, functions, arrays and strings, pointers, files, structures and dynamic memory. C is the base for operating systems, embedded devices and many computer-science courses.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Programming in C carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Why Array Names Act Like Pointers

Is an array name a pointer in C?
No, but it usually acts like one. When an array's name is used in an expression, C converts it to the address of its first element, which is called decay. That is why int *p = scores; works with no ampersand, and why scores == &scores[0] is true. The array is still a row of elements, so it behaves differently with sizeof, with assignment, and with &.
Why does a[i] equal *(a + i) in C?
C defines the subscript operator that way: a[i] means *(a + i). The array name decays to the address of element 0, adding i moves i whole elements forward, and the star reads the value there. This explains why indexes start at zero, since element 0 is zero steps from the start, and why p[i] works on a pointer, counting from wherever p points.
Why is sizeof different for an array and a pointer in C?
sizeof applied to an array measures the whole array, while sizeof applied to a pointer measures only the address. For int laps[6] with 4-byte ints, sizeof laps is 24, so sizeof laps / sizeof laps[0] gives 6. For a pointer p to laps, sizeof p is 8 on a typical 64-bit system, so the same trick gives 2. A pointer does not remember its array's length.
Why can't you assign one array to another in C?
An array name is fixed to its own row of elements, so it cannot be redirected the way a pointer can. Writing scores = backup; makes clang report that the array type is not assignable. To copy an array, copy it element by element in a loop, or use memcpy from string.h. A pointer variable, by contrast, can be re-aimed with p = backup; or moved with p++.

About the Programming in C course

How many college credits is Programming in C?
Programming in C (Computer Science 240) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Programming in C cost and what is included?
$250, paid once, with lifetime access: all 78 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Programming in C take?
It is self-paced with no deadlines. The 78 lessons total about 9 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Programming in C.

78 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Programming in C
3 credits · NCCRS
Enroll →