Thread: warning assignment makes interger from pointer without cast

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    warning assignment makes interger from pointer without cast

    what is wrong with this function
    Code:
    void update_board_array(char board[][ROW_MAX + 1], char place_character[10], int x_coordinate, int y_coordinate)
    {
        board[x_coordinate][y_coordinate] = (((y_coordinate % 2 == 0) && (x_coordinate % 2 == 0)) ||
                                             ((y_coordinate % 2 != 0) && (x_coordinate % 2 != 0))) ? place_character : INVALID_SQUARE;
    }
    both the board and the place_character are declared as type char and the x_coordinate and y_coordinate are declared as type int
    many thanks
    coop

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    place_character is an array, but you don't supply a subscript.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok im obviously going about this the wrong way. i want to assign a string to place_character and with in the function above assign that string to board[x][y] ie

    Code:
    int main()
    {
        char my_string[10] = "hello world", board[8][8];
        update_board_array(board, my_string,x,y);
    }
    void update_board_array(char board[][ROW_MAX + 1], char place_character[10], int x_coordinate, int y_coordinate)
    {
        board[x_coordinate][y_coordinate] = (((y_coordinate % 2 == 0) && (x_coordinate % 2 == 0)) ||
                                             ((y_coordinate % 2 != 0) && (x_coordinate % 2 != 0))) ? place_character : INVALID_SQUARE;
    }
    only reason i want to do this is so i can highlight the piece selected when the board is refreshed
    many thanks
    coop

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    im a complete moron at times when i will get all this though my thick skull i just don't know

    string[10] is 10 characters long not 10 words long right???????

    if i want to store a string with in each element of a 8x8 array would i need a 3rd dimention ie an 8x8x10 would hold 64 lots of an array of 10 characters

    is this right or am i still barking up the wrong tree

    many thanks
    coop

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    nope now whats wrong this was copied out of a book
    Code:
    int main()
    {
        int i;//, j, k;
        char *movies[3] = {"batman",
                           "gone in 60 seconds",
                           "get carter"};
    
    
        for (i = 0; i < 3; i++)
        {
            printf("%s\n",*movies[i]);
        }
    }
    this is getting beyond a joke
    coop

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > char my_string[10] = "hello world", board[8][8];
    > update_board_array(board, my_string,x,y);
    Leaving aside the problem that "hello world" needs 12 characters to store it, what are you hoping to see in your 8x8 board.

    Are you trying to create say a crossword puzzle?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    You've declared movies as an array of pointers to char:
    Quote Originally Posted by cooper1200
    Code:
    char *movies[3] = {"batman",
                       "gone in 60 seconds",
                       "get carter"};
    And trying to print individual chars (printf expects a pointer due to "%s") -- line 11:
    Quote Originally Posted by cooper1200
    Code:
    printf("%s\n",*movies[i]);
    The correct printf() call should be:
    Code:
    printf("%s\n", movies[i]);

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    a single character but i want the extra space so when i put in the co-ordinates of the character on the board i can use escape codes to colour it red to show it has been selected.
    many thanks
    coop

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It seems like you'd be better off making the place_character a single char (not an array), then from main calling
    update_board_array(board, my_string[0],x,y); // place 'H' somewhere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    am i trying to do the impossible here because no matter how many *'s i use or what way i word everything all i get is the same warning unless i set everything at deceleration which i cant do unless i declare 128 different strings and 64 different board arrays

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to start thinking of what the declarations and expressions mean then. Ask yourself, what is the type of this object? Ask yourself, what is the type of this expression?
    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

  12. #12
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thats just it if i declare char mystring[2][10]; for example then do a printf("%s", mystring[0][10]) where have i declared it as a type int?? and why do i get a warning abut %s expecting type char but argument 2 has type int

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the type of mystring[0][10]?
    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

  14. #14
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    char as i declared it as char
    yes????

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, although note that you're accessing out of bounds, so it's actually mystring[1][0].

    Now, %s expects a string argument, which effectively means a char*. A char is not a char*, so you have a mismatch. What you want is thus mystring[0].
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-09-2014, 09:19 PM
  2. Replies: 4
    Last Post: 03-03-2010, 01:06 PM
  3. Replies: 1
    Last Post: 12-23-2008, 09:39 AM
  4. Replies: 17
    Last Post: 02-13-2006, 01:19 PM
  5. Replies: 3
    Last Post: 01-14-2002, 12:13 PM

Tags for this Thread