Thread: newbie problems with pointers arrays and functions.

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    newbie problems with pointers arrays and functions.

    im trying to create a simple program that creates an array; tells every element of the array to become the letter 'a'. and then print the full array. As you will soon find out im a newbie, and the line of code i typed makes somewhat of sense to me. I did some research and tried describing the pointers in different ways but none have worked. if someone could shed dome light id be delighted.

    Code:
    #include <iostream>
    using namespace std;
    
    
    void resetTable(char *pTable[3][3]);
    void printTable(char *pTable[3][3]);
    
    int main(){
    char tTable[3][3];
    resetTable(&tTable[3][3]);
    printTable(&tTable[][]);
    system("PAUSE");
    return 0;
    }
    
    void resetTable(char *pTable[3][3]){
        for (int i=0;i<3;i++){
        for (int j=0;j<3;j++){
        *pTable[i][j] = {'a'};
        }
        }
    }
    
    void printTable(char *pTable[3][3]){
        for (int i=0;i<3;i++){
        for (int j=0;j<3;j++){
        cout<< *pTable[i][j];
        }
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you want a 2D array of characters or strings?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    assuming strings are more for word inputs and character are more leaning towards single character inputs, im wanting to use character. what im trying to do is make a Tic Tac Toe game, and i know this might not be the best approach, but im trying to do it however i know best, and after ill figure out the correct method.

    btw: thank you laser light. you've been more help then i could ask. Once i get going you wont here from me as much on the boards. except hopefully maybe helping others.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, okay. So you want a 2D array of chars. The problem is that your syntax is not quite correct. Try:
    Code:
    #include <iostream>
    using namespace std;
    
    void resetTable(char pTable[3][3]);
    void printTable(char pTable[3][3]);
    
    int main() {
        char tTable[3][3];
        resetTable(tTable);
        printTable(tTable);
        return 0;
    }
    
    void resetTable(char pTable[3][3]) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                pTable[i][j] = 'a';
            }
        }
    }
    
    void printTable(char pTable[3][3]) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                cout << pTable[i][j];
            }
        }
    }
    Observe how I pass the array to the functions, and how I indented the code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    thank you. so just to clear things up..

    is
    Code:
    tTable
    considered as an address. as i would normally use '&' in a normal int?

    and
    Code:
    char pTable[3][3]
    considered as a pointer? as i would normally use '*' in a normal interger?

    or am i looking at it the wrong way?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When passed as an argument, an array decays to a pointer to its first element. So tTable is an array, but it also works like a pointer, and in the functions pTable really is a pointer, yet works like an array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    tTable is an array (a two dimensional array in this case). char pTable[3][3] is the declaration of an array that is the parameter for the function. Since tTable and pTable have the same type, you just pass tTable directly to the function. There's no need to think about them in terms of pointers or addresses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. pointers, arrays, and functions oh my (newbie Q)
    By eazhar in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2002, 06:26 PM
  4. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM