Thread: Help with typedef struct name *name

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    23

    Help with typedef struct name *name

    So I have a header file and a library with functions and in the header it declares
    typedef struct board *board;

    Now I have to use the functions from the header:
    board create_board(int rows, int cols);

    void cell_on(board b, int r, int c);

    int display_board(board b);

    void delay(int msec);



    So in my program I:

    Code:
    #include "board.h"
    
    int main(void){
      board b;
    
      create_board(24, 80);
    
      cell_on(b, 2, 2);
    
      display_board(b);
    
      delay(2000);
    
    return 0;
    
    }
    And I am getting a segmentation fault. So I know something is wrong with me using b in the functions. Could anyone help? Thanks

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    The problem may be, you may forget to allocate the memory for that *board.

    If it is not a problem, then show us the coding for the following function

    void cell_on(board b, int r, int c);

    int display_board(board b);

    void delay(int msec);
    Last edited by murugaperumal; 03-08-2010 at 04:24 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    b is an unitialised pointer. At a guess, the line
    Code:
    create_board(24, 80);
    needs to be changed to;
    Code:
    b = create_board(24, 80);
    If it's not that, then you need to look in the implementation of the various functions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Avoid typedefs of pointers. They confuscate things unneedlessly.
    Furthermore, a board isn't a pointer to a board, so it's also misleading.
    When I create a board, I take for granted that it will create a board. But a pointer to a board isn't a board, and therefore when I create a board in your code, I haven't created a board because it's uninitialized. Beware.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    Well the code for the functions was not given to me because it is an assignment.
    But grumpy you were right... all I had to do was "b=", it makes sense now.
    Thank you.



    Quote Originally Posted by grumpy View Post
    b is an unitialised pointer. At a guess, the line
    Code:
    create_board(24, 80);
    needs to be changed to;
    Code:
    b = create_board(24, 80);
    If it's not that, then you need to look in the implementation of the various functions.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Creedy View Post
    But grumpy you were right... all I had to do was "b=", it makes sense now.
    Take note of Elysia's comments too. It is better to avoid unnecessary obfuscation when you can. People who have trouble understanding their own code also tend to have trouble getting it working correctly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    Okay I will... It was my teacher's doing this time, he gave us that.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you're feeling up to it, you can also remark that to your teacher that we said so.
    It's always fun to see how they react, and sometimes we might even make a teacher better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. typedef struct question
    By flevine100 in forum C Programming
    Replies: 1
    Last Post: 09-03-2002, 09:34 PM