Quicky Question

This is a discussion on Quicky Question within the C++ Programming forums, part of the General Programming Boards category; When you create an array, such as int m[10] , m is actually a pointer, yes yes? And because pointer ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Quicky Question

    When you create an array, such as int m[10], m is actually a pointer, yes yes?

    And because pointer arithemetic allows you to add pointers of the same data type, you can add to *m with *(m+1) to skip over one "slot", which is equal to the sizeof(int), right?

    The whole m[1] thing is just a cleaner and more understandable way of doing it, yes?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Quote Originally Posted by Krak
    When you create an array, such as int m[10], m is actually a pointer, yes yes?
    m is an array of 11 ints. However, the name m can be used as a pointer to the first element of the array.

    And because pointer arithemetic allows you to add pointers of the same data type, you can add to *m with *(m+1) to skip over one "slot", which is equal to the sizeof(int), right?
    You aren't allowed to add pointers. I assume you meant adding values to pointers. Yes, *(m+1) accesses location [m + sizeof(int)].

    The whole m[1] thing is just a cleaner and more understandable way of doing it, yes?
    Depends what you're doing and your personal preference. Many people find a pointers/iterators approach easy to use - look at the STL for example.

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    >When you create an array, such as int m[10], m is actually a pointer, yes yes?<

    Well, no actually, however m acts as a const reference to the first value in the array.

    >And because pointer arithemetic allows you to add pointers of the same data type, you can add to *m with *(m+1) to skip over one "slot", which is equal to the sizeof(int), right?<

    yes, you can use pointer arithmatic to walk through an array. However you cannot change the value of m.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You aren't allowed to add pointers
    umm. Well you can add pointers, you cannot change the value of m because it acts as a const pointer. Adding pointers however is very possible:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main() {
    
    	int myArray[4] = {3, 5, 7, 9};
    	int* myPointer;
    	
    	myPointer = myArray;
    
    	for(int i = 0; i < 4; i++, myPointer++) {
    		cout << *myPointer << endl;
    	}
    
    	cin.get();
    	return 0;
    }
    -Andy
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    That's not adding pointers. That's adding integral values to pointers - a completely different thing.

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    My bad - misunderstood what you meant.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    > m is an array of 11 ints.

    No, it's 10 ints.

    > however m acts as a const reference to the first value in the array
    Actually, it acts as an immutable pointer to the (mutable, unless said to be const) first element of the array.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Oops - been using VB arrays

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21