text: Pointers and arrays
  Pointers and arrays are intricately linked in the C language. In previous lessons, you learned how to declare an array of variables:
  int anArray[5]; // declare array of 5 integers
  anArray is actually a pointer that points to the first element of the array! Because the array variable is a pointer, you can dereference it, which returns array element 0:
  int anArray[5] = { 9, 7, 5, 3, 1 };
  // dereferencing an array returns the first element (element 0)
  cout << *anArray; // prints 9!
  char szName[] = "Jason"; // C-style string (also an array)
  cout << *szName; // prints 'J'
  Pointer arithmetic.
  pnPtr + 1
  pnPtr - 1.
  pnPtr+1
  pnPtr+3
  When calculating the result of a pointer arithmetic expression, the compiler always multiplies the integer operand by the size of the object being pointed to. This is called scaling.
  The following program:
  int nValue = 7;
  int *pnPtr = &nValue;
  cout << pnPtr << endl;
  cout << pnPtr+1 << endl;
  cout << pnPtr+2 << endl;
  cout << pnPtr+3 << endl;
  Outputs::
  short nValue = 7;
  short *pnPtr = &nValue;
  cout << pnPtr << endl;
  cout << pnPtr+1 << endl;
  cout << pnPtr+2 << endl;
  cout << pnPtr+3 << endl;
  0012FF7C
  0012FF7E
  0012FF80
  0012FF82
  Because a short is 2 bytes, each address differs by 2.
  It is rare to see the + and – operator used in such a manner with pointers. However, it is more common to see the ++ or — operator being used to increment or decrement a pointer to point to the next or previous element in an array.
  Pointer arithmetic and arrays
  If anArray is a pointer that points to the first element (element 0) of the array, and adding 1 to a pointer already returns the next object, then anArray+1 must point to the second element (element 1) of the array! We can verify experimentally that this is true:
  int anArray[5] = { 9, 7, 5, 3, 1 };
  cout << *(anArray+1) << endl; // prints 7
  The parentheses are necessary to ensure the operator precedence is correct — operator * has higher precedence than operator +.
  Note that *(anArray+1) has the same effect as anArray[1]. It turns out that the array indexing operator ([]) actually does an implicit pointer addition and dereference! It just looks prettier.
  We can use a pointer and pointer arithmetic to loop through an array. Although not commonly done this way (using indices is generally easier to read and less error prone), the following example goes to show it is possible:;
  This program uses a pointer to step through each of the elements in an array. Each element is dereferenced by the switch expression, and if the element is a vowel, nVowels is incremented. The for loop then uses the ++ operator to advance the pointer to the next character in the array. The for loop terminates when all characters have been examined.
  The above program produces the result:
  Mollie has 3 vowels
  Hi Alex!
  I’ve been learning C from your tutorial – and I think it’s really great, so thanks.
  However I just wanted to point (speaking of pointers ;-)) out, that the guy behind these two articles
  would have to disagree with you on whether arrays and pointers are the same. Personally, I don’t care if he is right, since the code presented here still works intentionally.
  Best regards
  Jacob
  The author of those articles is right on some points and wrong on others. However, I find his logic and examples to be quite misleading. Rather than refute his arguments, instead, let me see if I can shed some light on the situation as I understand it.
  Arrays and pointers are not strictly identical. Although arrays are implemented using pointers, arrays also contain slightly more information. When you create an array, the compiler has to keep track of how many elements the array has. Otherwise, the compiler would not know how many elements to delete when it came time to delete[] the array.
  With a non-dynamically allocated array, it’s possible to see the actual size of the array:
  int anArray[5]; cout < < sizeof(anArray);
  This program prints out 20 (assuming 4-byte integers). This is the size of the array.
  On the other hand:
  int *ptr = anArray; cout < < sizeof(ptr);
  prints 4. This is the size of the pointer.
  When you access an array through a pointer, you're accessing it in a slightly limited manner, because you lose access to the size and type information of the original array. This can be a pain (because it means you have to store the size of the array in a separate variable) but that's about the extent of the practical consequences.
  Note that for dynamically allocated arrays:
  int *pnArray = new int[5]
  the new operator returns a pointer to the array, and that's all you ever have to work with. You never get access to the actual array object.
  Functionally, in terms of how you access an array, there is no real difference between accessing the elements of an actual array and accessing the elements of an array through a pointer.
  So technically, it is correct that arrays and pointers aren't identical. Pointers give you access to a limited subset of the array information. Practically speaking, it's generally not even worth worrying.
  const int nArraySize = 14;
  char szName[nArraySize] = "Lazy BrownFox";
  But When I change the code to the below, the result becomes 4, which I do not understand. It is as if the program is counting the last whitespace character as a vowel. Can you explain this to me?
  const int nArraySize = 15;
  char szName[nArraySize] = "Lazy Brown Fox";.
  didn’t read the first post…
  this can be deleted
  [...] 2007 Prev/Next Posts « A PHP script to unzip files with file overwriting | Home | 6.8 — Pointers, arrays, and pointer arithmetic » Tuesday, July 10th, 2007 at 6:52 [...]
  This is going over my head:
  for (char *pnPtr = szName; pnPtr <= szName + nArraySize; pnPtr++)
  Could you explain it, please?
  I’m sorry but would it be possible to explain? For example the ‘pn:
  for (char *pnPtr = szName;
  This is creating a new pointer named pnPtr and setting it to point to the first character in the name array. After this, pnPtr and szName will be holding the same address.
  pnPtr <= szName + nArraySize”.
  pnPtr++?
  For what it’s worth (maybe more for others who read this, not necessarily Cory, who probably stopped caring about a response to this message months ago :)) I think the answer is: yes. Although, it’ll really be looking at the character at that address, as Alex said, not the address itself. But yes, you’re thinking along the right lines there.
  It’s okay – I’ve come back to it with a clear head and I get it now. (And I realize that nArraySize-2 works.) :)
  Thanks for tutorials, BTW.
  I am thankfull for above Information.I am doing C++ programming…and am doing a Project In Multiple Arrays, using Functions.
  Hope To Get More Information in this web.
  Thank you very much
  Savenaca yauyau
  how would you sort an char array using pointers like you did above?
  I do not see where I sorted an array in this lesson. Perhaps this comment was posted on the wrong lesson?
  “It turns out the the array” should read, “It turns out that the array”.
  [ Indeed! Thank you. -Alex ]
  You’re making a one-off error here, I think:
  The for loop should run for
  pnPtr <= szName + nArraySize-1;
  or
  pnPtr <= szName + nArraySize-2;
  since the junk data after szString[nArraySize - 1] might end up being a vowel (right?)
  [ You are correct. I changed the example so it loops the correct number of times. It still goes through the loop once when pnPtr is pointing to the terminating NULL, but this doesn't hurt anything. -Alex ]
  I just wonder why did write in the last example (in for loop):
  char *pnPtr = szName
  in place of:
  char *pnPtr = *szName
  since you need the pointer of the first element in the array not the element itself!
  im wrong… now got it! ‘arr’…
  const int nArraySize = 7;
  char szName[nArraySize] = "Mollie";
  // This produces an array of characters like so.
  // szName[0] = 'M' , szName[1] = 'o'
  // szName[2] = 'l' , szName[3] = 'l'
  // szName[4] = 'i' , szName[5] = 'e'
  // szName[6] = ''.. :)
  Actually, there is one place difference could matter
  //file1.cpp
  int myarray[10];
  //file2.cpp
  extern int* myarray;
  void foo()
  {
  myarray[0] = 0;
  }
  a main function in file2.cpp calling “foo” will crash
  #include
  int main()
  {
  using namespace std;
  char szName[7] = "Mollie";
  cout << *(&szName); // Returns Mollie
  cout << &szName; // Returns Address of first element of szName
  cin.clear();
  cin.ignore(255, 'n');
  cin.get();
  } :
  char *pnPntr = &szName;
  I want pnPntr to point to the first Address of szName.
  Yet this works,
  char *pnPntr = szName;
  Depending on what is on the left of szName, szName means completely different things!
  cout << szName ;
  I get mollie..
  char *pnPntr=szName;
  put a pointer declaration and suddenly szname is and address!
  cout << *pnPntr;:
  int *pnPntr = (int*.
  Wow. I finally understand pointers! Yay!
  That is a pretty hand way of stepping through an array.
  I really appreciate your guide! THANKS
  This contains misinformation: “anArray is actually a pointer that points to the first element of the array”.
  An array is not a pointer, ever. An array is an array, a pointer is a pointer. This isn’t up for discussion, read the language standard. An array has an implicit array-to-pointer conversion, but that doesn’t make it a pointer anymore than it makes a function a pointer because a function-to-pointer conversion exists.
  The type of
  a
  in
  int a[10]
  is
  int[10]
  There are no pointers there.:
  #include <iostream>
  int main()
  {
  using namespace std;
  //string myString = "Christopher";
  char myStringArray[] = "Christopher";
  int arraySize = sizeof(myStringArray)/sizeof(myStringArray;
  }
  Will output the following:.
  …rather than a hex address for pnPtr.
  While setting up a random integer array and carrying out the same process (sans vowel counting)…
  #include <iostream>
  int main()
  {
  using namespace std;
  int myStringArray[] = {1, 2, 3, 4, 5, 10, 20};
  int arraySize = sizeof(myStringArray)/sizeof(myStringArray[0]);
  for (int *pnPtr = myStringArray; pnPtr < myStringArray + arraySize; pnPtr += 1)
  cout << "pnPtr: " << pnPtr << "\t*pnPtr: " << *pnPtr << endl;
  }
  ?
  wow.! this site is great,
  it somehow helps me to learn more.
  can i ask?
  how i can i determine the numbers even and odd inputs using the pointer arithmetic?
  I have a question regarding a simple program which indexes using pointer arithmetic in order to reverse the case of a string. The code is this:
  #include <iostream>
  #include <cctype>
  using namespace std;
  int main()
  {
  char str[] = "This is a String";
  char *p = str;
  cout << "Original string: " << str << 'n';
  while(*p)
  {
  if(isupper(*p)) (*p) = tolower(*p);
  if(islower(*p)) (*p) = toupper(*p);
  p++;
  }
  cout << "Inverted-case string: " << str << 'n';
  return 0;
  }…
  I keep seeing stuff like:
  [ CODE ] somerandomstufftypedhere [ /CODE ]
  How is that possible?
  Nvm, I see it has been fixed now. Maybe it was just my crappy pc.
  BTW: Thx for your tutorials!
  I have a question about the use of the test-condition in the loop of this example and was hoping you could clear it up for me Alex.;
  If I understand this correctly, in pnPtr < szName + nArraySize;, szName is the name of the array which points to the address of element 0 in the array. nArraySize evaluates to 7, the actual size of the array which is equivalent to the pointer addition of pPtr (for the sake of making it similar to your earlier pointer arithmetic example) + 7.
  szName points to element[0] or the "M", pPtr + 1 to "o", 2 to "l", 3 to the second "l", 4 to "i", 5 to "e", 6 to the null terminator, and because the less-than sign was used instead of <=, the final element must be 1 integer higher than the size of the array so the loop ends. In this case though since you're using addresses, would it be that pPtr + 7 be the address of the next char in memory as opposed to some integer index number?
  Oh, In my haste writing my comment I forgot to mention that the main question was if I correctly understand how the test condition of the loop was working. That last bit at the end was something I just thought to ask at the last second.
  You could’ve used a simple if statement, and no this code doesn’t work, and it will add to nVowels if there is a lowercase u.
  Hi,!
  Copyright © 2013 Learn C++ - All Rights ReservedPowered by WordPress & the Atahualpa Theme by BytesForAll. Discuss on our WP Forum