Hello
I need help understanding /interpreting (or putting into words the behaviours of pointers and arrays). Please see the included code.
I have included 4 cases/scenarios of using pointers and arrays in which I am trying to understand their behaviour. I think I understand what is happening in cases 1, 3 and 4. However I don’t know what is happening in case 2.
Here are my descriptions of what I think is happening in case 1, 3 and 4.
Case 1
char myName[] = "John Smith";
myName is an array so when you do
cout << myName << endl;
you are just selecting the ith item of the array and outputting it.
Case 3
char* myPointer2 = myName;
myPointer2 is a pointer that points to data type of type char.
If you have: int anArray[5];
anArray is actually a pointer that points to the first element of the array
Since by definition the name of any array is a pointer when you do
myPointer2, it is if you are performing the same action as in Case one that is
myPointer2 is equivalent to myName
Case 4
char* myPointer3 = myName;
When you do cout << *(myPointer3+i)
You are telling the computer to output the contents of the memory address myPointer3+i
Questions:
Why does Case 2 gives the output it gives? In other words why doesn’t it just print one letter at a time?
I also want to add that I noticed that if I do the following
Line A: cout << myName << endl;
Line B: int someNums[] = {1, 2, 3, 4, 5};
Line C: cout << someNums << endl;
Line A results in the following being output to the screen: John Smith
However Line C results in the following being output to the screen: 003FF854
003FF854 is a memory address. Why is that the output as opposed to 12345
My Code:
The Results:
I need help understanding /interpreting (or putting into words the behaviours of pointers and arrays). Please see the included code.
I have included 4 cases/scenarios of using pointers and arrays in which I am trying to understand their behaviour. I think I understand what is happening in cases 1, 3 and 4. However I don’t know what is happening in case 2.
Here are my descriptions of what I think is happening in case 1, 3 and 4.
Case 1
char myName[] = "John Smith";
myName is an array so when you do
cout << myName << endl;
you are just selecting the ith item of the array and outputting it.
Case 3
char* myPointer2 = myName;
myPointer2 is a pointer that points to data type of type char.
If you have: int anArray[5];
anArray is actually a pointer that points to the first element of the array
Since by definition the name of any array is a pointer when you do
myPointer2, it is if you are performing the same action as in Case one that is
myPointer2 is equivalent to myName
Case 4
char* myPointer3 = myName;
When you do cout << *(myPointer3+i)
You are telling the computer to output the contents of the memory address myPointer3+i
Questions:
Why does Case 2 gives the output it gives? In other words why doesn’t it just print one letter at a time?
I also want to add that I noticed that if I do the following
Line A: cout << myName << endl;
Line B: int someNums[] = {1, 2, 3, 4, 5};
Line C: cout << someNums << endl;
Line A results in the following being output to the screen: John Smith
However Line C results in the following being output to the screen: 003FF854
003FF854 is a memory address. Why is that the output as opposed to 12345
My Code:
Rich (BB code):
#include <iostream>
using namespace std;
int main()
{
char myName[] = "John Smith";
cout << "char myName[] = \"John Smith\";" << endl;
cout << "strlen(myName) = " << strlen(myName) << endl << endl;
//Case One
cout << "Case 1" << endl;
cout << "using: cout << " << "myName" << endl;
for (int i = 0; i < (int) strlen(myName); i++)
{
cout <<"i = " << i << ", " << myName << endl;
}
cout << endl << "===" << endl << endl;
//=====================================================
//Case Two
cout << "Case 2" << endl;
cout << "char* myPointer1 = myName;" << endl;
cout << "using: cout << " << "myPointer1+i" << endl;
char* myPointer1 = myName;
for (int i = 0; i < (int) strlen(myName); i++)
{
cout <<"i = " << i << ", " << myPointer1+i << endl;
}
cout << endl << "===" << endl << endl;
//=====================================================
// Case Three
cout << "Case 3" << endl;
cout << "char* myPointer2 = myName;" << endl;
cout << "using: cout << " << "myPointer2" << endl;
char* myPointer2 = myName;
cout << strlen(myName) << endl;
for (int i = 0; i < (int) strlen(myName); i++)
{
cout <<"i = " << i << ", " << myPointer2 << endl;
}
cout << endl << "===" << endl << endl;
//=====================================================
//Case Four
cout << "Case 4" << endl;
cout << "char* myPointer3 = myName;" << endl;
cout << "using: cout << " << "*(myPointer3+i)" << endl;
char* myPointer3 = myName;
cout << strlen(myName) << endl;
for (int i = 0; i < (int) strlen(myName); i++)
{
cout <<"i = " << i << ", " << *(myPointer3+i) << endl;
}
return 0;
}
Rich (BB code):
char myName[] = "John Smith";
strlen(myName) = 10
Case 1
using: cout << myName
i = 0, J
i = 1, o
i = 2, h
i = 3, n
i = 4,
i = 5, S
i = 6, m
i = 7, i
i = 8, t
i = 9, h
===
Case 2
char* myPointer1 = myName;
using: cout << myPointer1+i
i = 0, John Smith
i = 1, ohn Smith
i = 2, hn Smith
i = 3, n Smith
i = 4, Smith
i = 5, Smith
i = 6, mith
i = 7, ith
i = 8, th
i = 9, h
===
Case 3
char* myPointer2 = myName;
using: cout << myPointer2
10
i = 0, J
i = 1, o
i = 2, h
i = 3, n
i = 4,
i = 5, S
i = 6, m
i = 7, i
i = 8, t
i = 9, h
===
Case 4
char* myPointer3 = myName;
using: cout << *(myPointer3+i)
10
i = 0, J
i = 1, o
i = 2, h
i = 3, n
i = 4,
i = 5, S
i = 6, m
i = 7, i
i = 8, t
i = 9, h
Press any key to continue . . .
Last edited: