Structure demo

This is a discussion on Structure demo within the C Programming forums, part of the General Programming Boards category; Need a little help understanding why the addDecade function doesn't work. It is supposed to add 10 years to the ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    68

    Structure demo

    Need a little help understanding why the addDecade function doesn't work.

    It is supposed to add 10 years to the birthdate, but it doesn't do that.

    Code:
    //structuredemo2.c//demonstrating Structures and functions
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct Date 
    {
    	int Month; //members
    	int Day;
    	int Year;
    };//end structure definition
    
    
    void AddDecade(struct Date);
    
    
    int main(int argc, char *argv[])
    {
    	struct Date BDay;
    
    
    	char buffer[50];
    
    
    	printf("What month were you born?  ");
    	BDay.Month = atoi(fgets(buffer, 50, stdin));
    
    
    	printf("What day were you born?  ");
            BDay.Day = atoi(fgets(buffer, 50, stdin));
    
    
    	printf("What year were you born?  ");
            BDay.Year = atoi(fgets(buffer, 50, stdin));
    
    
    	printf("You were born on %d, %d, %d.\n", BDay.Month, BDay.Day, BDay.Year);
    
    
    	AddDecade(BDay);
    
    
    	printf("You will be 10 years older on %d, %d, %d\n", BDay.Month, BDay.Day, BDay.Year);
    
    
    }//end main
    
    
    void AddDecade(struct Date Target) 
    {
    	Target.Year += 10;
    }//end AdDecade

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Look up the return value of fgets.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Read from here the swap that uses an extra variable. You will find one that does not use one in the link, but that's not the point. Then try to explain why your code behaves like that.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by Tclausex View Post
    Look up the return value of fgets.
    This is not the problem of course. On success, the function returns the buffer. Check it on the manual too!
    If you want to do some error handling or you were reading a file, then you would have to change the code at this part.....
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,347
    You need to pass a pointer to your structure to the function, to modify it.

    The way it stands, you're passing the structure itself, which creates a local copy within that function. Any changes you make only occur to that local copy (not the structure in "main()"), then the local copy is destroyed when the function ends.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by std10093 View Post
    This is not the problem of course. On success, the function returns the buffer. Check it on the manual too!
    If you want to do some error handling or you were reading a file, then you would have to change the code at this part.....
    LOL Derp!

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by Matticus View Post
    You need to pass a pointer to your structure to the function, to modify it.

    The way it stands, you're passing the structure itself, which creates a local copy within that function. Any changes you make only occur to that local copy (not the structure in "main()"), then the local copy is destroyed when the function ends.
    Oh it would be better for him to find out himself after checking the swap example I provided.. For sure he has been taught that and would remind him of what is happening (I hope)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,347
    Quote Originally Posted by std10093 View Post
    Oh it would be better for him to find out himself after checking the swap example I provided.. For sure he has been taught that and would remind him of what is happening (I hope)
    Perhaps. Another possibility might be that the OP would gloss over the link and not notice such a subtle clue, since on the surface, a swap example doesn't appear to answer their question.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Code:
    //structuredemo2.c
    //demonstrating functions and functions
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct Date 
    {
    	int Month; //members
    	int Day;
    	int Year;
    };//end structure definition
    
    
    struct Date AddDecade(struct Date);//can return a date 
    
    
    int main(int argc, char *argv[])
    {
    	struct Date BDay;//using the datatype we just created
    
    
    	char buffer[50];//char array of fifty charaters
    
    
    	printf("What month were you born?  ");//enter what month you were born in
    	BDay.Month = atoi(fgets(buffer, 50, stdin));//ascii string to integer takes in input
    
    
    	printf("What day were you born?  ");
            BDay.Day = atoi(fgets(buffer, 50, stdin));
    
    
    	printf("What year were you born?  ");
            BDay.Year = atoi(fgets(buffer, 50, stdin));
    
    
    	printf("You were born on %d, %d, %d.\n", BDay.Month, BDay.Day, BDay.Year);//prints your DOB
    
    
    	BDay = AddDecade(BDay);//calls function to add ten years to DOB
    
    
    	printf("You will be 10 years older on %d, %d, %d\n", BDay.Month, BDay.Day, BDay.Year);
    
    
    }//end main
    
    
    struct Date AddDecade(struct Date Target) 
    {
    	Target.Year += 10;//add ten years to DOB
    	return Target;
    }//end AdDecade
    it works now

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Yes it works, but I have to say that you have to think that when you return a struct, you return a copy of it. In cases where the struct is big (like in real programs), you might not want to pay this cose, because coping is linear to amount of information you copy . On the other hand, passing a pointer is cheap.

    But of course, what you have done is correct in terms will it work or not
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Hey, man. If you know a way to make this work faster and more efficiently, preach on.
    THanks

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Matticus was right. You didn't check on the link.
    Pass a reference like this
    Code:
    #include<stdio.h>
    #include<stdlib.h>
     
     
    struct Date
    {
        int Month; //members
        int Day;
        int Year;
    };//end structure definition
     
     
    void AddDecade(struct Date*);
     
     
    int main(int argc, char *argv[])
    {
        struct Date BDay;
     
     
        char buffer[50];
     
     
        printf("What month were you born?  ");
        BDay.Month = atoi(fgets(buffer, 50, stdin));
     
     
        printf("What day were you born?  ");
            BDay.Day = atoi(fgets(buffer, 50, stdin));
     
     
        printf("What year were you born?  ");
            BDay.Year = atoi(fgets(buffer, 50, stdin));
     
     
        printf("You were born on %d, %d, %d.\n", BDay.Month, BDay.Day, BDay.Year);
     
     
        AddDecade(&BDay);
     
     
        printf("You will be 10 years older on %d, %d, %d\n", BDay.Month, BDay.Day, BDay.Year);
     
        return 0;
    }//end main
     
     
    void AddDecade(struct Date* Target)
    {
        Target->Year += 10;
    }//end AdDecade
    The reason is in #10 post.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Actually, I did look at your link. I look at a lot of your code. You are a very skilled programmer and I'm afraid I didn't understand the code.
    What is the "->" operator?
    Last edited by jwall; 02-27-2013 at 05:46 PM.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    2,347
    That is referred to as the "arrow operator." It's a short cut for referencing a structure member via a pointer to the structure. Consider the following three examples.

    Code:
    // Example 1
    // Referencing a structure member locally in "main()" with the "dot operator"
    
    #include <stdio.h>
    
    struct Test
    {
        int x;
    };
    
    int main(void)
    {
        struct Test sTest;
    
        sTest.x = 2;
    
        printf("x = %d\n",sTest.x);
    
        return 0;
    }
    Code:
    // Example 2
    // Referencing a structure member in a function, using the "dot operator"
    //    and standard pointer notation
    
    #include <stdio.h>
    
    struct Test
    {
        int x;
    };
    
    void updateStruct(struct Test *y);
    
    int main(void)
    {
        struct Test sTest;
    
        sTest.x = 2;
    
        printf("x = %d\n",sTest.x);
    
        updateStruct(&sTest);
    
        printf("x = %d\n",sTest.x);
    
        return 0;
    }
    
    void updateStruct(struct Test *y)
    {
        (*y).x = 3;  // line of interest
    }
    Code:
    // Example 3
    // Referencing a structure member in a function, using the "arrow operator"
    
    #include <stdio.h>
    
    struct Test
    {
        int x;
    };
    
    void updateStruct(struct Test *y);
    
    int main(void)
    {
        struct Test sTest;
    
        sTest.x = 2;
    
        printf("x = %d\n",sTest.x);
    
        updateStruct(&sTest);
    
        printf("x = %d\n",sTest.x);
    
        return 0;
    }
    
    void updateStruct(struct Test *y)
    {
        y->x = 3;  // line of interest
    }
    Compare the "line of interest" from examples 2 and 3.

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    I'm beginning to understand how this works. I'm going to go over it a little more, but you guys have been a great help. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crysis Demo Available
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-17-2007, 12:59 PM
  2. Blizzard Demo
    By dazza33 in forum C++ Programming
    Replies: 5
    Last Post: 08-26-2007, 04:45 PM
  3. Health Bar demo
    By jdinger in forum Game Programming
    Replies: 7
    Last Post: 04-12-2002, 12:27 PM
  4. 3D demo
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-12-2001, 02:53 PM
  5. My new RPG demo...
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2001, 02:00 PM

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