A couple questions

This is a discussion on A couple questions within the C++ Programming forums, part of the General Programming Boards category; what arithmetic can you do with pointers? addition, subtraction, multiplication? If you assign a 0 to a pointer would it ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    A couple questions

    what arithmetic can you do with pointers? addition, subtraction, multiplication?

    If you assign a 0 to a pointer would it make the pointer point to nothing?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what arithmetic can you do with pointers?
    You can add or subtract by an integer, or subtract by a pointer. All of these are only strictly defined for pointers into an array or simulated array.

    >If you assign a 0 to a pointer would it make the pointer point to nothing?
    Close enough, yes.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    can a pointer variable store the address of another variable?

    does an array's name store the address of the array's first element?

    Is an array's name a pointer, constant, and a variable?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >can a pointer variable store the address of another variable?
    Yes:
    Code:
    int a; 
    int *x=&a;
    x now points to the memory address of a.

    >does an array's name store the address of the array's first element?
    I always answer this wrong, so I'm not going to embarass myself.

    >Is an array's name a pointer, constant, and a variable?
    See above.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The array's name is a pointer to the first (0th) element. The subscript simply adds that value to the base address (the first element of the array). For some fun, try the following (and you'll see what I mean):
    Code:
    int array[4] = { 1, 2, 4, 8 };
    std::cout << array[3] << std::endl;
    std::cout << 3[array] << std::endl;
    *edit*
    I hope no one saw that.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can a pointer variable store the address of another variable?
    That's really the definition of a pointer, isn't it?

    >does an array's name store the address of the array's first element?
    No, but in most cases the name of an array is converted to a pointer to the first element.

    >Is an array's name a pointer, constant, and a variable?
    No, somewhat, and yes. Contradictory, no? Try this on for size.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of Questions About Classes
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2009, 03:50 PM
  2. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 02:04 AM
  3. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 08:55 AM
  4. A couple of questions that someone might know the answer to...
    By Finchie_88 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-15-2005, 09:26 AM
  5. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 06:02 PM

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