newbie problems with pointers arrays and functions.

This is a discussion on newbie problems with pointers arrays and functions. within the C++ Programming forums, part of the General Programming Boards category; im trying to create a simple program that creates an array; tells every element of the array to become the ...

  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
    21,744
    Do you want a 2D array of characters or strings?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,744
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,744
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    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, 05:48 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08: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, 10:13 AM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM

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