What is the relationship between a pointer and an array (or string)?
A free video tutorial from Huw Collingbourne
Director of Technology, SapphireSteel Software
4.4 instructor rating • 16 courses • 74,003 students
The address of the array is the same as the address of the first item in the array. Why is that important?
Learn more from the full courseAdvanced C Programming: Pointers
Master pointers, addresses and memory allocation in C
03:50:10 of on-demand video • Updated August 2019
- Pointers and addresses
- Indirection and multiple indirection
- Generic pointers and casts
- Memory allocation and reallocation
- Pointer arithmetic
- Singly and doubly linked lists
- Queues and stacks
- Deep and shallow copying
- Common pointer errors
English [Auto] Arrays in C are actually just sequential data items stored at some location in memory the address of the array is the same as the address of the first item in the array because it is where the array begins. The name of the array that is the name you get to the array variable is also the address of the array. To explain this let's look at how strings are used in C unlike many other languages. C does not have a dedicated string data type in C A string is just an array of characters for convenience C treats the null character Slash's zero as the string Terminator. When you declare a string variable like this C automatically ODs a null char at the end. Now let's see if we can find where this string is stored in memory. Now here's my program and here you can see that I've just created a string hello world which I've assigned to the string array variable. S.T. all one. So let's run this first. It displays the string that is the array of characters of the variable s.t. all one. Then the character at index 0 that is at the first index of the array which is h of hello world. Then I show the address of the array which is this number representing a memory location. Now I get the address the first character remember that I said the address of the array is the same as the address of the first item in the array. Well we can see that's true because the address of the character H which is shown when I use the ampersand address of operator is the same as the address of the string shown when I use the address of operator with the string variable s.t. one. But now look at this last value here instead of using the ampersand to get an address I just display the variable itself. Or one as an integer using percent D in my format string. And this shows the same number which is the address of the array. Now to understand how this works. Let me repeat what I said at the beginning of this lesson because it's really important that you understand this the address of an array is the same as the address of the first item in the array because it's where the array begins and the name of the array that is the name you give to the array variable is also the address of the array. In other words why we may tend to think of arrays as fixed length lists and in many other programming languages that may be all you need to know about arrays. In fact the way your computer and the C language creates and stores an array is a bit different. So remember that an array is really the same as an address in memory that defines the beginning of sequential data items. And when you deal with arrays including strings you're dealing with addresses. And usually when you're dealing with addresses you will be using pointers that's what I'll explain in the next lesson.