Structure of a program

This is a discussion on Structure of a program within the C Programming forums, part of the General Programming Boards category; Hi, I'm working on my first (100 line+) program, i've been reading alot(hole day) and the problem is the build ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Structure of a program

    Hi,

    I'm working on my first (100 line+) program, i've been reading alot(hole day)
    and the problem is the build up of the program and functions that passes information between eachother.

    I first made all my vars Globals and everything would run. Since i understood this wasen't the way your ment to program thinks i would make something else. could somebody post a example program post on a structure, point to it and printing a string from another function change the string and give it back to the structure. or just how to get a pointer to a structure and able to update it from any location?

    Stef

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    I did read the faq but I don't understand the faq or the faq doesn't understand me
    I'm realy having trouble with the pointing to the struct.

    I tried to do something very easy, read from the struct(Both in Main & the function). and then write a character to players->symbol.
    Code:
    #include <stdio.h>
    
    	struct {						/* Struct used to store names of 2 players and a symbol. */
        char Player1[25];              /* Name of the first player */
        char Player2[25];             /* Name of the second player */
        char activePlayer[25];      /* Name of the active player. */
    	char symbol;				/* symbol of theplayer eg. 'O' */
    }players={"name1", "name2", "Active",'O'}; //One record players init names and O we only need 1 row since we use vars.
    
    //pointers
    struct players *player;					//Player points to players.
    
    //Prototypes
    void print_person ( struct players *player );
    
    int main( struct players *player){		//parameters for main is the pointer value leading to players
    
          printf ( "Player1:   %s\n", (*player).Player1 );	//print the value of player1 on the screen
          printf ( "Player2:    %s \n", player->Player2 );	//print the value of player2 on the screen(other syntax does the same thing)
          printf ( "Symbol: %c\n", player->symbol );			//print the symbol on the screen.
          //scanf("%c",players.symbol);						//Can't do?
          print_person ( &players );							//pass to another function.
          return 0;
    }
    
    void print_person ( struct players *player )
    {
          printf ( "Name:   %s\n", player->Player1 );
          printf ( "Symbol:    %s \n", (*player).Player2 );
    }

    Returns 5 errors and 1 warning:

    Code:
    Compiling...
    game.c
    \visual studio 2005\projects\game.c(18) : error C2037: left of 'Player1' specifies undefined struct/union 'players'
    \visual studio 2005\projects\game.c(19) : error C2037: left of 'Player2' specifies undefined struct/union 'players'
    \visual studio 2005\projects\game.c(20) : error C2037: left of 'symbol' specifies undefined struct/union 'players'
    \visual studio 2005\projects\game.c(22) : warning C4133: 'function' : incompatible types - from '*__w64 ' to 'players *'
    \visual studio 2005\projects\game.c(27) : error C2037: left of 'Player1' specifies undefined struct/union 'players'
    \visual studio 2005\projects\game.c(28) : error C2037: left of 'Player2' specifies undefined struct/union 'players'
    4OpEenRij - 5 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    I'm doing something terrible wrong, or I implent a tought of myself totaly wrong please enlighten me :d
    Last edited by stef569; 12-02-2006 at 11:10 AM.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct {                        /* Struct used to store names of 2 players and a symbol. */
        char Player1[25];           /* Name of the first player */
        char Player2[25];           /* Name of the second player */
        char activePlayer[25];      /* Name of the active player. */
        char symbol;                                /* symbol of theplayer eg. 'O' */
    } players;
    
    //pointers
    players *player;                                        //Player points to players.
    
    //Prototypes
    void print_person ( players *player );
    
    int main(void){         //parameters for main is the pointer value leading to players
    
    player = malloc(sizeof(players));
    
    strcpy(player->Player1, "name1");
    strcpy(player->Player2, "name2");
    strcpy(player->activePlayer, "Active");
    player->symbol = '0';
    
    printf ( "Player1:   %s\n", player->Player1 );  //print the value of player1 on the screen
    printf ( "Player2:    %s \n", player->Player2 );        //print the value of player2 on the screen(other synt$
    printf ( "Symbol: %c\n", player->symbol );                      //print the symbol on the screen.
    //scanf("%c",players.symbol);                                           //Can't do?
    print_person ( player );                                                        //pass to another function.
    }
    
    void print_person (players *player )
    {
      printf ( "Name:   %s\n", player->Player1 );
      printf ( "Symbol:    %s \n", (*player).Player2 );
    }
    output:
    Player1: name1
    Player2: name2
    Symbol: 0
    Name: name1
    Symbol: name2

    you don't need to comment every line of code some things you've even commented twice.
    Code:
    //pointers
    struct players *player;					//Player points to players.
    Last edited by sl4nted; 12-02-2006 at 11:26 AM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    just better instead of the global variable
    Code:
    //pointers
    players *player;                                        //Player points to players.
    use local one
    Code:
    int main(void)
    {
       players* player = malloc(sizeof(*player));
       if(player == NULL)
       {
          //not enough memory
          return 1;
       }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Talking

    Thx that indeed worked perfectly it's amazing it's working, a +-4 hours search and you guy's did it in what 10 minutes?

    I think i leave the pointer as a global because i need it in every function.

    Thx again Stef

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    i need it in every function
    Better put it as a parameter as in the sample with print_person
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program structure with select sockets
    By n3v in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-03-2006, 06:34 AM
  2. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  3. Simple program structure.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-15-2002, 04:36 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. structure program
    By prlove71 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2002, 08:01 PM

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