Thread: Beginner question about pointers

  1. #1
    Registered User
    Join Date
    Mar 2009

    Beginner question about pointers

    void function(char * ptrA, char arr1[])
            ptrA = arr1[9];
    int main(...)
            char arr1[10];
            fillArray(arr1); // fills the array
            char *ptrA = arr1[1];
            function(ptrA, arr1);
    After the call to function(), does ptrA point to arr1[1], or arr1[9]? If it points to arr1[1], what do I need to do so that I can make persistent changes on ptrA inside of the scope of function()? Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    A function cannot change the value passed into it. ptrA was passed into a function, whatever happens inside that function stays in Vegas, and it has the same value (in main) afterwards.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    First of, let's just clarify that array indexes start at 0 (you may already know that, but I'm just making sure). Second, arr1[N] is a char. To get the address of a char (or any variable, for that matter), use the 'address-of' operator ('&'). Finally, all parameters are passed by value in C, so to 'repoint' a pointer, you need to pass it's address (in this case, a char**).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ newbie question about pointers
    By lafayette in forum C++ Programming
    Replies: 15
    Last Post: 09-30-2008, 12:38 PM
  2. Pointers Question
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 09-08-2008, 10:17 AM
  3. Utter beginner question about using notepad for c++
    By Borodin in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2008, 04:08 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM