problem in functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cube
    New Member
    • Feb 2008
    • 18

    problem in functions

    Hi, I'm a newbie and I have to make the following assignment.

    We are asked to use this function
    void get_input(char P[3][3], int *line, int *column);
    It is a part of a tic-tac-toe game and we have to use the above function
    in order to ask the player to give the coordinates of the cell. In that cell will be placed the letter "X".
    I have many problems.
    I have written the code and if I use it without a function (just place it directly in the main code) it works fine.
    But we have to use the above function, and the coordinates should be returned to the main programm by reference.
    When I try to place it in a function I get confused.

    Fist of all I want to ask, in which order are the values passed to the function.
    What I mean is that,
    first I will ask the player for the "line" and "column"
    then I have to pass to the array (P[3][3])) the character "X", according to the values of "line" and "column".

    Therefore the
    1st value that I get is the value of "line"
    2st value that I get is the value of "column"
    3d value that I declare according to the value of "line" and "column" is the X in the P array.

    But the order of the values in the function are (char P[3][3], int *line, int *column)
    1st value "X" in the array
    2st value of "line"
    3d value of "column"

    Does this mean that the fist value that I get (and that value is "line") goes to the first value in the parenthesis of the function, which is the "char P[3][3])?
    How to I place an order to that mess.

    Here is part of the code, where you can see how I call the function in the main program.

    The problem is that when I print the values of the function in order to check what I've done, I get crazy things.
    for example
    for input: line = 1, column=1, character =X,
    when I print them, I get
    line= ' , column= 8 , character = 4006960




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void get_input(char P[3][3], int *line, int *column);
    
    int main()
    {
      char P[3][3];
      int l, c;
        get_input(P, &l, &c);
       printf("%c %d %d\n", P, l, c);
           system("PAUSE");
    }
    
     
     
     void get_input(char P[3][3], int *line, int *column)
     {
    (here is some code)
    .
    .
    .
    
      printf("Choose cell (line, column):");    
      scanf("%d %d", &l, &c);
           
      D[l][c]='X';
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by cube
    Fist of all I want to ask, in which order are the values passed to the function.
    What I mean is that,
    first I will ask the player for the "line" and "column"
    then I have to pass to the array (P[3][3])) the character "X", according to the values of "line" and "column".

    Therefore the
    1st value that I get is the value of "line"
    2st value that I get is the value of "column"
    3d value that I declare according to the value of "line" and "column" is the X in the P array.

    But the order of the values in the function are (char P[3][3], int *line, int *column)
    1st value "X" in the array
    2st value of "line"
    3d value of "column"

    Does this mean that the fist value that I get (and that value is "line") goes to the first value in the parenthesis of the function, which is the "char P[3][3])?
    How to I place an order to that mess.
    No, these 2 orders are unrelated, the have no bearing on each other. If you choose to use the variable line first in your function to receive input from the user that is fine and the result will end in *line.

    Originally posted by cube
    Code:
       printf("%c %d %d\n", P, l, c);
    This printf line is wrong. The %c indicates that the first value is of type char but you pass P a value of type char **. You probably mean P[l][c] which is of type char.
    Originally posted by cube
    Code:
     void get_input(char P[3][3], int *line, int *column)
     {
    ...
      scanf("%d %d", &l, &c);
           
      D[l][c]='X';
    None of variables D, l or c at this point in the function is defined in you code and additionally you haven't shown what happens to them next making quite hard to tell if it is right or wrong.

    Comment

    Working...