Thread: Program explanation.

  1. #1
    Registered User
    Join Date
    May 2010

    Program explanation.

    Can anyone explain this program?
    #include <stdio.h>
    #include <stdlib.h>
    struct Person{
    	   char name[20];
    	   int age;
    struct Person int_age(struct Person x){
    	   x.age = x.age + 1;
    	   return x;
    void changeName(char *arg_name, char *new_name){
    	 strcpy(arg_name, new_name);
    int main(void){
    	   struct Person a = {"Yorikas", 10};
    	   struct Person c; // struct Person = c !
    	   c = int_age(a);
    	   printf("c: %d A: %d \n", c.age, a.age);
    	   changeName(, "kostikas");
    return 0;

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    	   struct Person a = {"Yorikas", 10};
    This just initializes the struct with those values.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Finally we got two struct? The first named 'a' and the second 'c'?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    Yes, there are two instances of type struct Person. One of the variables is called a, and the other is called c. It's no different than when you are calling two integers a and c:

    int a;
    int c;

    A struct is nothing more but a complex data type which is made up of primitive data types and/or other nested structs.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to execute other program within c program?
    By KoYoungSuk in forum C Programming
    Replies: 7
    Last Post: 06-07-2010, 05:08 AM
  2. program quits unexpected
    By skelesp in forum C Programming
    Replies: 34
    Last Post: 12-10-2008, 09:10 AM
  3. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM