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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Programming in C.
Lesson complete
Earn the 3 college credits for Programming in C
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Arrays of Pointers in C →← Pointer Arithmetic in CNext lesson: Arrays of Pointers in C →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: C vs C++
Why take it here
Why students take Programming in C with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
Why does a[i] equal *(a + i) in C?
Why is sizeof different for an array and a pointer in C?
Why can't you assign one array to another in C?
About the Programming in C course
How many college credits is Programming in C?
How much does Programming in C cost and what is included?
Which universities accept UPI Study credits?
How long does Programming in C take?
How are the exams done?
How do my credits reach my college?
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.
3 credits · NCCRSEnroll →