Arrays and Pointers What is the relationship

Discussion in 'Programmer's Corner' started by integral, Apr 20, 2011.

  1. integral

    Thread Starter New Member

    Aug 19, 2010
    22
    0
    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:
    Code ( (Unknown Language)):
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. int main()
    5. {
    6.     char myName[] = "John Smith";
    7.     cout << "char myName[] = \"John Smith\";" << endl;
    8.     cout << "strlen(myName)  = " << strlen(myName) << endl << endl;
    9.    
    10.     //Case One
    11.     cout << "Case 1" << endl;
    12.     cout << "using: cout << " << "myName[i]" << endl;
    13.     for (int i = 0; i < (int) strlen(myName); i++)
    14.     {
    15.         cout <<"i = " << i << ", " << myName[i] << endl;
    16.     }
    17.     cout << endl << "===" << endl << endl;
    18.  
    19.     //=====================================================
    20.  
    21.     //Case Two
    22.     cout << "Case 2" << endl;
    23.     cout << "char* myPointer1 = myName;" << endl;
    24.     cout << "using: cout << " << "myPointer1+i" << endl;
    25.     char* myPointer1 = myName;
    26.     for (int i = 0; i < (int) strlen(myName); i++)
    27.     {
    28.         cout <<"i = " << i << ", " << myPointer1+i << endl;
    29.     }
    30.     cout << endl << "===" << endl << endl;
    31.  
    32.     //=====================================================
    33.  
    34.     // Case Three
    35.     cout << "Case 3" << endl;
    36.     cout << "char* myPointer2 = myName;" << endl;
    37.     cout << "using: cout << " << "myPointer2[i]" << endl;
    38.     char* myPointer2 = myName;
    39.     cout << strlen(myName) << endl;
    40.     for (int i = 0; i < (int) strlen(myName); i++)
    41.     {
    42.         cout <<"i = " << i << ", " << myPointer2[i] << endl;
    43.     }
    44.     cout << endl << "===" << endl << endl;
    45.  
    46.     //=====================================================
    47.  
    48.     //Case Four
    49.     cout << "Case 4" << endl;
    50.     cout << "char* myPointer3 = myName;" << endl;
    51.     cout << "using: cout << " << "*(myPointer3+i)" << endl;
    52.  
    53.     char* myPointer3 = myName;
    54.     cout << strlen(myName) << endl;
    55.     for (int i = 0; i < (int) strlen(myName); i++)
    56.     {
    57.         cout <<"i = " << i << ", " << *(myPointer3+i) << endl;
    58.     }
    59.  
    60.  
    61.     return 0;
    62. }[/i][/i][/i][/i]
    The Results:
    Code ( (Unknown Language)):
    1.  
    2.  
    3. char myName[] = "John Smith";
    4. strlen(myName)  = 10
    5.  
    6. Case 1
    7. using: cout << myName[i]
    8. i = 0, J
    9. i = 1, o
    10. i = 2, h
    11. i = 3, n
    12. i = 4,
    13. i = 5, S
    14. i = 6, m
    15. i = 7, i
    16. i = 8, t
    17. i = 9, h
    18.  
    19. ===
    20.  
    21. Case 2
    22. char* myPointer1 = myName;
    23. using: cout << myPointer1+i
    24. i = 0, John Smith
    25. i = 1, ohn Smith
    26. i = 2, hn Smith
    27. i = 3, n Smith
    28. i = 4,  Smith
    29. i = 5, Smith
    30. i = 6, mith
    31. i = 7, ith
    32. i = 8, th
    33. i = 9, h
    34.  
    35. ===
    36.  
    37. Case 3
    38. char* myPointer2 = myName;
    39. using: cout << myPointer2[i]
    40. 10
    41. i = 0, J
    42. i = 1, o
    43. i = 2, h
    44. i = 3, n
    45. i = 4,
    46. i = 5, S
    47. i = 6, m
    48. i = 7, i
    49. i = 8, t
    50. i = 9, h
    51.  
    52. ===
    53.  
    54. Case 4
    55. char* myPointer3 = myName;
    56. using: cout << *(myPointer3+i)
    57. 10
    58. i = 0, J
    59. i = 1, o
    60. i = 2, h
    61. i = 3, n
    62. i = 4,
    63. i = 5, S
    64. i = 6, m
    65. i = 7, i
    66. i = 8, t
    67. i = 9, h
    68. Press any key to continue . . .
    69. [/i][/i]
     
    Last edited: Apr 21, 2011
  2. guitarguy12387

    Active Member

    Apr 10, 2008
    359
    12
    The difference in case 2 is that you're not de-referencing the pointer. So it just prints the pointer, rather than the value being pointed to.

    Actually, it increments the POINTER every iteration of the loop (similar to case 4), but it is not being de-referenced before it is output.
     
  3. integral

    Thread Starter New Member

    Aug 19, 2010
    22
    0
    Thanks for your reply.

    I understand and agree with the part in blue.

    What do you mean by it just prints the pointer? My understanding of pointers is that when you print a pointer you are printing the address that the pointer is holding.

    When does the computer know to stop printing?
     
  4. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186


    Yes you are correct.



    What does that means I have no idea........
     
  5. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    std::cout is overloaded for various types. the 'cout << myPointer' is using the version of cout which takes a pointer to a char, it keeps printing til it hits the null at the end of your char array. someone can probably explain it better than me mind...
     
    integral likes this.
Loading...