Thread: pointer/array/struct problem

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    pointer/array/struct problem

    this compiles with no errors
    Code:
    printf("%d",map[player.x][player.y]);
    but this doesn't
    Code:
    int *p_map = &map[0][0];
    printf("%d",*p_map[player.x][player.y]);
    error is an invalid indirection
    i know its probably something simple, but i have no clue what it is.

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Try
    Code:
    printf("%d",*p_map);
    The one who says it cannot be done should never interrupt the one who is doing it.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >printf("%d",*p_map[player.x][player.y]);
    The subscript operator dereferences for you, so unless the elements of map are pointers, this is one indirection too much. Actually, it's two indirections too much because p_map is a pointer, not a pointer to a pointer or a pointer to an array. In this case you're pretty much stuck with calculating the dimensions yourself unless you want to change the declaration of p_map:
    Code:
    #define sz(arr) sizeof arr / sizeof arr[0]
    printf("%d\n",p_map[player.x * sz(map[0]) + player.y]);
    My best code is written with the delete key.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    lol
    i think im going to have to find another way around it. or i will just have to make the array global...
    *shudders*

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you show us exactly what you're trying to do then we can suggest some solutions.
    My best code is written with the delete key.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    well here is the entire code i got now. its very rough, but i finally got it working last night.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define WALL 1
    #define NOWALL 0
    #define MAXMAP 9
    #define MINMAP 0
    
    bool NORTH = true;
    bool SOUTH = true;
    bool EAST = true;
    bool WEST = true;
    
    struct object
    {
        int x;
        int y;
        char name[15];
    
    };
    struct object player = {1,1};
    
    void init();
    
    void check();
    void move(char *);
    
    int map[10][10] =
                      {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
                       1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                       1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 ,
                       1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
    
    int main()
    {
    
        init();
        char dir = '\0';
        do
        {
            scanf(" %c",&dir);
            system("cls");
            move(&dir);
            check();
        }while(dir != 'q');
    
        return 0;
    }
    
    void init()
    {
        //pre-processing
        printf("Enter you character's name \n");
        scanf(" %s",player.name);
        system("cls");
        printf("Welcome %s\n\n",player.name);
    
        check();
    }
    
    void move(char *p_dir)
    {
        //select direction
        switch(*p_dir)
        {
            case 'n':
                if (NORTH == true) player.x--;
                else printf("There is a wall in the way\n\n");
                break;
    
    
            case 's':
                if (SOUTH == true) player.x++;
                else printf("There is a wall in the way\n\n");
                break;
    
    
            case 'e':
                if (EAST == true) player.y++;
                else printf("There is a wall in the way\n\n");
                break;
    
    
            case 'w':
                if (WEST == true) player.y--;
                else printf("There is a wall in the way\n\n");
                break;
    
            case 'q':
                return;
        }
    }
    
    void check()
    {
        //check exits
        printf("Player is at %d, %d\n\n",player.x, player.y );
        if (map[player.x-1][player.y] == WALL) NORTH = false;
        else NORTH = true;
    
        if (map[player.x+1][player.y] == WALL) SOUTH = false;
        else SOUTH = true;
    
        if (map[player.x][player.y+1] == WALL) EAST = false;
        else EAST = true;
    
        if (map[player.x][player.y-1] == WALL) WEST = false;
        else WEST = true;
    
        //print exits
        printf("Exits are \n\n");
        if (NORTH == true) printf("(N)orth ");
        if (SOUTH == true) printf("(S)outh ");
        if (EAST == true) printf("(E)ast ");
        if (WEST == true) printf("(W)est ");
    
        printf("\n(Q)uit\n");
    }
    Last edited by sand_man; 06-07-2004 at 07:51 PM.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    bool is C++. system("cls"); is system dependant. Your function prototypes have empty arguments. You should pass dir to move without the & operator. You should also expect both upper and lowecase characters in your imputs.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    1. i know bool is c++ (ill change it when i can be bothered)
    2. system("cls") works for me
    3. you mean i should have void check(void) ?
    4. why would i not use the & operator? im passing the address to a pointer.
    5. i told you it was rough.
    6. thanks for the suggestions
    Last edited by sand_man; 06-07-2004 at 08:35 PM.

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    for question 3, yes
    and for preference I would change
    Code:
    void move(char *);
    to something like
    Code:
    void move(char *where);
    Or some variable name. And now that I think about it. main should be
    Code:
    int main(void)

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1. i know bool is c++ (ill change it when i can be bothered)
    Add this:
    Code:
    typedef enum { false, true } bool;
    And your code will still run when compiled as C. But it strikes me that you're compiling as C++. This can be dangerous because there are subtle differences between C and C++ that would cause you trouble.

    >2. system("cls") works for me
    That's fine, just be aware that your code is not portable to systems that don't support the cls command and will behave differently for systems that use cls for something other than clearing the screen.

    >3. you mean i should have void check(void) ?
    Yes. In C++ an empty argument list in a function prototype says that the function takes no arguments. In C an empty parameter list is a throwback to K&R C where it says the function takes an unknown number and type of arguments. It's generally a bad idea to mix old and new function declarations and definitions, so you should always use void to signify no arguments in your declarations.

    >4. why would i not use the & operator? im passing the address to a pointer.
    There's no need to pass a pointer when all you're doing is passing a single character. It's not a string, so a pointer to char is not required, and char is very likely smaller than pointer to char, so you're actually introducing inefficiency as well as obfuscating the code with needless indirection.

    >init();
    >char dir = '\0';
    This won't work in C unless you use a compiler that supports C99. Because there aren't many people that do, we assume C89/90 here, and C89/90 requires that all declarations be placed at the beginning of a block, before any processing statements.
    My best code is written with the delete key.

  11. #11
    ---
    Join Date
    May 2004
    Posts
    1,379
    >But it strikes me that you're compiling as C++.
    im a stupid n00b

    thanks for the tips prelude.
    so what is an alternative to system("cls") ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    #define DEPTH 2
    #define WALL 1
    #define NOWALL 0
    #define MAXFLOOR 2
    
    typedef enum { false, true } bool;
    
    bool NORTH = true;
    bool SOUTH = true;
    bool EAST = true;
    bool WEST = true;
    bool STAIRS = false;
    
    struct object
    {
        int x;
        int y;
        int z;
        int gold;
        int hp;
        char name[15];
    };
    struct object player = {1,1,0,300,50};
    
    void init(void);           // pre-processing
    void process(void);        // controls game loop
    
    void check(void);          // checks available directions
    void move(char *);         // moves player
    void panel(void);
    
    int map[3][10][10] =
                      {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // Floor 1 (0)
                       1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                       1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 ,
                       1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
    
                       1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // Floor 2 (1)
                       1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                       1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 ,
                       1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
    
                       1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , // Floor 3 (2)
                       1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
                       1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 ,
                       1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 ,
                       1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
    
    int main(void)
    {
        init();
        process();
        return 0;
    }
    
    void init(void)
    {
    
        printf("Enter you character's name \n");
        scanf(" %s",player.name);
        system("cls");
        printf("Welcome %s\n\n",player.name);
    
        check();
    }
    
    void process()
    {
        char dir = '\0';
        do
        {
            panel();
            scanf(" %c",dir);
            system("cls");
            move(&dir);
            check();
        }while(dir != 'q');
    }
    
    void move(char *p_dir)
    {
        //select direction
        switch(*p_dir)
        {
                // Processing for direction
            case 'n':
                if (NORTH == true) player.x--;
                else printf("There is a wall in the way\n\n");
                break;
    
            case 's':
                if (SOUTH == true) player.x++;
                else printf("There is a wall in the way\n\n");
                break;
    
            case 'e':
                if (EAST == true) player.y++;
                else printf("There is a wall in the way\n\n");
                break;
    
            case 'w':
                if (WEST == true) player.y--;
                else printf("There is a wall in the way\n\n");
                break;
    
    
                // Processing for STAIRS
            case 'u':
                if (STAIRS == true)
                {
                    if (player.z != MAXFLOOR) player.z++;
                    else printf("You cant go up here\n\n");
                }
                break;
    
            case 'd':
                if (STAIRS == true)
                {
                    if (player.z != 0) player.z--;
                    else printf("You cant go down here here\n\n");
                }
                else printf("You cant go down here here\n\n");
                break;
    
            case 'q':
                return;
        }
    }
    
    void check(void)
    {
        //check exits
        printf("Player is at %d, %d on floor %d\n\n\n",player.x, player.y, player.z );
        if (map[player.z][player.x-1][player.y] == WALL) NORTH = false;
        else NORTH = true;
    
        if (map[player.z][player.x+1][player.y] == WALL) SOUTH = false;
        else SOUTH = true;
    
        if (map[player.z][player.x][player.y+1] == WALL) EAST = false;
        else EAST = true;
    
        if (map[player.z][player.x][player.y-1] == WALL) WEST = false;
        else WEST = true;
    
        if (map[player.z][player.x][player.y] == DEPTH) STAIRS = true;
        else STAIRS = false;
    }
    
    void panel()
    {
        //print exits
        printf("Exits are \n");
        if (NORTH == true) printf("(N)orth ");
        if (SOUTH == true) printf("(S)outh ");
        if (EAST == true) printf("(E)ast ");
        if (WEST == true) printf("(W)est ");
    
        //print if STAIRS
        if (STAIRS == true && player.z < MAXFLOOR) printf("(U)p ");
        if (STAIRS == true && player.z > 0) printf("(D)own ");
    
        printf("\n(Q)uit\n\n");
    
        textcolor(DARKGRAY);
        cprintf("%s's Stats ",player.name);
        textcolor(WHITE);
        cprintf("HP: ");
        textcolor(RED);
        cprintf("%d ",player.hp);
        textcolor(WHITE);
        cprintf("GOLD: ");
        textcolor(YELLOW);
        cprintf("%d ", player.gold);
    
    }
    Last edited by sand_man; 06-08-2004 at 09:26 AM.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so what is an alternative to system("cls") ?
    There isn't a good solution to the clearing-the-screen problem. I usually recommend that you either think long and hard about why you want to clear the screen, or isolate the non-portable parts:
    Code:
    void
    clear_screen(void)
    {
      system("cls");
    }
    By only using clear_screen to clear the screen, you only have to make minor changes when porting the program. It won't run everywhere unchanged, but when it comes to useful and nontrivial programs, you have to loosen your definition of "portable" a bit.

    You can find various ways to clear the screen in the FAQ.
    My best code is written with the delete key.

  13. #13
    ---
    Join Date
    May 2004
    Posts
    1,379
    thanks, ill work around it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM