Thread: Struct Fields Question

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    93

    Struct Fields Question

    Ah it's been a while since I coded. Anyway, I'm trying to do a program, and am curious as to why this doesn't work:

    Code:
    #define MONTH 9
    #define DAY  17
    #define YEAR 04
    
    #include <stdio.h>
    
    
    struct month
    {
    	   int number; //Cardinal number of the month
    	   int days;   //Number of days in the month
    	   char *name; //Name of the month
    };
    
    typedef struct month month;
    
    month months[11] = {
                       {1, 31, "January" },
                       {2, 28, "February"}
                                         };
    
          //months[0].number = 1;
          //months[0].days   = 31;
          //months[0].name   = "January";
    
    
    
    int main()
    {
        return 0;
    }
    The part commented out anyway. It will not work.

    Must this be done in main? Or what is going on?

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you can't assign a pointer to a string like that you have to use strcpy
    Code:
    char *ptr;
    *ptr="Hey";/*WRONG WRONG*/
    
    
    char ptr[BUFSIZ];
    strcpy(ptr,"Hey"); /*correct*/
    
    /*or*/
    char *ptr;
    ptr=malloc(BUFSIZ);
    if(!ptr){
         printf("Whoaa!\n");
         return 1;
    }
    strcpy(ptr,"Hey");

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Thanks I hadn't noticed that.

    But even when I have that part commented out and only have the first line included, it still gives me an error.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    1. #define YEAR 04 04 is not in octal try 2004
    2. I forgot to mention that you can assing strings to char *, only when you initialize then now it is a string literal and you can't modify it.
    Code:
    #define MONTH 9
    #define DAY  17
    #define YEAR 2004
    #include <stdio.h>
    
    
    struct month
    {
      int number;
      int days;
      char *name;
    };
    
    typedef struct month month;
    
    month months[11] = {
      {1, 31, "January"},
      {2, 28, "February"}
    };
    
    
    int main (){
           return 0;
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Beast()
    Code:
    month months[11] = {
    Where I come from we got 12 months! 'O course, C'll number 'em 0-11 in a array of 12. Er else, ya could just use empty brackets and let the compiler do the countin'.
    Last edited by Dave_Sinkula; 12-15-2004 at 10:36 PM. Reason: Added simley -- sounded somewhat crass.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by Dave_Sinkula
    Where I come from we got 12 months! 'O course, C'll number 'em 0-11 in a array of 12. Er else, ya could just use empty brackets and let the compiler do the countin'.
    Lol yea, I fixed that soon after. Thanks.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > //months[0].number = 1;
    Because these are assignments, not initialisations.
    Assigments must be inside a function.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by linuxdude
    you can't assign a pointer to a string like that you have to use strcpy
    Code:
    char *ptr;
    *ptr="Hey";/*WRONG WRONG*/
    
    
    char ptr[BUFSIZ];
    strcpy(ptr,"Hey"); /*correct*/
    
    /*or*/
    char *ptr;
    ptr=malloc(BUFSIZ);
    if(!ptr){
         printf("Whoaa!\n");
         return 1;
    }
    strcpy(ptr,"Hey");
    Actually I think your way is better suited for the task than copying in strings. I'd leave the code as you had it, but I'd call it a const char * just as a friendly reminder that the string the pointer is pointing to is not to be played with.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    psst, feburaury has 28.25xxxxxxxx days in it
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    misplaced does have a valid point. February doesn't have a constant number of days. If this is going to be used for a calender I'd go ahead and make that value a float.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. How to access struct fields in array
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2006, 09:51 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM