Just check this if I am right please.

This is a discussion on Just check this if I am right please. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> struct emp { char name[25]; int age; int salary; }; void fn1(char*,int,int); void main() { ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    15

    Just check this if I am right please.

    Code:
    #include <stdio.h>
    #include <conio.h>
    struct emp
    {
    	char name[25];
       int age;
       int salary;
    };
    void fn1(char*,int,int);
    void main()
    {
    	struct emp emp_1[3];
       int i;
       for(i=0;i<3;i++)
       {
    		printf("Enter name age and salary of number %d\n",i);
    	   scanf("%s%d%d",&emp_1[i].name,&emp_1[i].age,&emp_1[i].salary);
       }
       for(i=0;i<3;i++)
       {
          fn1(emp_1[i].name,emp_1[i].age,emp_1[i].salary);
       }
       getch();
    }
    void fn1(char *ch,int a,int b)
    {
    	printf("\n\nName is %s\nAge is %d\nSalary is %d\n",ch,a,b);
    }
    That above is my answer to the qtn.
    WAP to accept values in structure array and pass it to a function to print the values
    Is there any way that I can pass the whole structure to fn1 without putting it in a for loop?

    Code:
    for(i=0;i<3;i++)
       {
          fn1(emp_1[i].name,emp_1[i].age,emp_1[i].salary);
       }
    just straightaway like

    Code:
    void fn1(struct emp empl_1)
    or something like that?

    Thanks in advance
    Last edited by aditya_t90; 04-11-2009 at 05:47 AM. Reason: Make it easier to understand my qtn

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you can pass a structure to a function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    15
    Quote Originally Posted by matsp View Post
    Yes, you can pass a structure to a function.

    --
    Mats
    And how may I do that?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by aditya_t90 View Post
    And how may I do that?
    Using a pointer:
    Code:
    #include <stdio.h>
    
    struct eg {
            char *data;
    };
    
    void test (struct eg *here) {
            printf("voila: %s\n",here->data);
    }
    
    int main () {
            char this[]="message";
            struct eg look;
            look.data=this;
            test(&look);
            return 0;
    }
    * for pointer
    -> for pointer property (indirect notation)
    & address of operator (so a pointer to the address)
    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

  5. #5
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    just a general tip. I would avoid using variable named this. It may be confusing, especially for programmers using mainly c++. Ive personally used _this, when i've had a struct whých has been used like a class in cpp. I can also imagine that variable named this might cause problems in interface functions declaration, which may be used with cpp. Although extern "C" might help compiler to handle that correctly.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    15
    Quote Originally Posted by Maz View Post
    just a general tip. I would avoid using variable named this. It may be confusing, especially for programmers using mainly c++. Ive personally used _this, when i've had a struct whých has been used like a class in cpp. I can also imagine that variable named this might cause problems in interface functions declaration, which may be used with cpp. Although extern "C" might help compiler to handle that correctly.
    Yes "this" is a keyword in some languages like ecmascript+html
    I removed the word "this" with something else.
    I seem to have finally got the program right.
    Anyone will check if this conforms to standards

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    struct emp
    {
       char name[25];
       int age;
       int salary;
    };
    
    void fn1(struct emp *);
    
    void main()
    {
       struct emp emp_1[3];
       int i;      
       for(i=0;i<3;i++)
       {
    	   printf("Enter name age and salary of number %d\n",i);
    	   scanf("%s%d%d",&emp_1[i].name,&emp_1[i].age,&emp_1[i].salary);
       }
       fn1(emp_1);
       getch();
    }
    
    void fn1(struct emp *here)
       {
    	int y;
    	for(y=0;y<3;y++)
       {
    		printf("\n\nName is %s\nAge is %d\nSalary is %d\n",here[y].name,here[y].age,here[y].salary);
       }
    }
    The code might not be indented properly here coz, I just copy pasted it from borland c++
    The above program takes a struct array of employee names ages and salaries and passes it to a function which prints it.
    It works just as indented in borland c++

    Thanks for all the help
    Last edited by aditya_t90; 04-12-2009 at 02:30 AM.

  7. #7
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    i am browsing the site with mobile, and it is a bit painfull to browse the code... But quick check reveals a few problems regarding standards. Void main and getch

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also here
    Code:
    scanf("%s%d%d",&emp_1[i].name,&emp_1[i].age,&emp_1[i].salary);
    red ampersand to be removed

    as a side not function prototype could be make to accept const pointer as it does not modify the struct:
    Code:
    void fn1(const struct emp * pArr);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    15
    Thanks everyone.
    Vart could you please elaborate on...

    Quote Originally Posted by vart View Post
    also here
    as a side not function prototype could be make to accept const pointer as it does not modify the struct:
    Code:
    void fn1(const struct emp * pArr);
    what is pArr?
    is is a variable name or something or is is a keyword?

    Thanks
    Last edited by aditya_t90; 04-12-2009 at 05:49 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    It is a parameter name, though in the context of a function prototype the parameter name is not significant.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by aditya_t90 View Post
    Thanks everyone.
    Vart could you please elaborate on...



    what is pArr?
    is is a variable name or something or is is a keyword?

    Thanks
    yes it is argument name - better practice is not to leave it out even in the function declaration...

    naming it pArr or some other variant like empArray etc - I just hint the reader of the code that function expects an array of structs and not just one struct...

    of course mode descriptive prototype would look like
    Code:
    void fn1(const struct emp * pArr, size_t arrSize);
    so your function should not guess the correct array size and thus could not read beyond the array bounds when the array size in the calling function will be changed by someone

    PS. on the other hand the prototype
    Code:
    void fn2(const struct emp * pCurrentEmployee);
    hints the reader that the function works with only one struct instance
    Last edited by vart; 04-12-2009 at 08:02 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

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