very very simple code (struct question)

This is a discussion on very very simple code (struct question) within the C Programming forums, part of the General Programming Boards category; The simple code below is a learning exercise, aimed at undertanding the use of structs in practice. Please ignore it's ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    very very simple code (struct question)

    The simple code below is a learning exercise, aimed at undertanding the use of structs in practice. Please ignore it's lack of utility or necessity for any other purpose.

    So far, I have learnt that I can reference (the address to) a struct (i.e. the address to its first member) via a variable, as well as access it via a pointer. The two approaches are illustrated below to refer to the C struct tagged "stat", using the variable "a" and the pointer "p", resepctively.

    I understand pointers in concept but it's still not natural to me to use them. Hopefully, in time, it will feel more natural.

    At this point, I want to add another function from the C libraries to this simple code: ctime(), in order to convert the long int stored in st_birthtime to a human readable date.

    What is a very simple way I could add the ctime function below to convert the time to an ASCII string? (Keeping in mind I am not all that comfortable with pointers... yet.)

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <time.h>
    main(int argc, char **argv){
    struct stat a, *p; 
    stat(argv[1], &a); 
    p = &a;
    printf("%d", p->st_ino); 
    printf(" %d", a.st_birthtime); 
    printf("\n"); 
    return(0);
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    [solved]

    For a human readable st_birthtime, add the following:

    Code:
    time_t b;
    b = a.st_birthtime;
    printf(" %s", ctime(&b));

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. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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