Thread: Save struct with pointer

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Save struct with pointer

    Hi,

    I have the following 2 structs:

    Code:
    typedef struct _date
    {
      int day;
      int month;
      int year;
    }DATE;
    
    typedef struct _worker{
      int id;
      char name[100];
      DATE * birthdate;
    }WORKER;
    Then I do:

    Code:
    if(fwrite(n->data, sizeof(WORKER), 1, fp) != 1){
    	printf("<Error: writting to file.>\n");
    }
    Will this save the worker info and the birthdate?

    Regards.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No, you have to follow the pointer yourself
    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It would be better to intgrate the birthdate struct into the worker struct ... it's easy, just lose the *

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  3. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  4. Replies: 12
    Last Post: 04-01-2008, 08:58 AM
  5. Save data from two struct arrays in one .dat file
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 03-27-2008, 03:50 PM