Thread: Using pointers with Struct

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44

    Using pointers with Struct

    I was trying to make a program that will use a struct to take in 3 employee ID's, first and last names. But have to use a pointer for passing to the function. Here is the code I have, sorry as I am new to programming so there are a few flaws. Any tips would be appreciated.

    Code:
    #include <stdio.h>
    struct employee
    {
    	char firstname[40];
    	char lastname[40];
    	int id;
    };
    typedef struct employee Employee[3];
    
    /* Input the employee data interactively from the keyboard */
    void InputEmployeeRecord(Employee *ptrEmployee);
    
    void main()
    {
        Employee e1;
        InputEmployeeRecord(&e1);
    }
    
    void InputEmployeeRecord(Employee *ptrEmployee)
    {
    	int i=0;
    	for(i=0;i<3;i++)
    	{
    		printf("Enter the employee ID")
    		scanf("%d", Employee.id);
    		printf("Enter the employee first name")
    		gets(firstname);
    		printf("Enter the employee last name")
    		gets(lastname);
    	}
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The best approach is to make things incrementally, and test. Instead of writing everything at once, then compile. You have the data type Employee, which is an array of 3 struct employee. You then pass a pointer to this array of struct employee to your function.

    This means that you either need to cast Employee inside InputEmployeeRecord or use the -> convenience syntax. And, Employee is an array, so you need an index as well. Which means if you need to refer to id, you have to do like this: ptrEmployee[i]->id

    Note that, Employee is a type you can not use that by itself, but instead *ptrEmployee that you have declared being a pointer of type Employee.

    You shouldn't use gets, but fgets, and main should return 0 and have int as a return type.
    Last edited by Subsonics; 03-10-2011 at 12:48 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > typedef struct employee Employee[3];
    Typedefs which just make an array out of a type, or make a pointer out of a type don't add a lot of value, but add a hell of a lot of confusion.

    Without this, your code looks like this
    Code:
    #include <stdio.h>
    struct employee
    {
    	char firstname[40];
    	char lastname[40];
    	int id;
    };
    typedef struct employee Employee;
    
    /* Input the employee data interactively from the keyboard */
    void InputEmployeeRecord(Employee *ptrEmployee);
    
    int main()  /*!! yes, really, it is int */
    {
        Employee e1[3];
        InputEmployeeRecord(e1);
        return 0;
    }
    
    void InputEmployeeRecord(Employee *ptrEmployee)
    {
    	int i=0;
    	for(i=0;i<3;i++)
    	{
    		/// input code, not using gets()
    	}
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    Why not use gets? Does that not run as well, just curious? And I will start trying to fix up what I have, Ill let you know how it goes. Also why is it better to have main as an int not void?

  5. #5

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    Thanks for that, I really hadn't known before so thanks for the advice, I changed up the program but when I run I get multiple errors still, any advice on what I am doing wrong? I am pretty sure its with the pointer in the function and with the word employee inside the function.

    Code:
    #include <stdio.h>
    struct employee
    {
    	char firstname[40];
    	char lastname[40];
    	int id;
    };
    typedef struct employee Employee;
    
    void InputEmployeeRecord(Employee *ptrEmployee);
    
    int main()
    {
        Employee e1[3];
        InputEmployeeRecord(&e1);
        return 0;
    }
    
    void InputEmployeeRecord(Employee *ptrEmployee)
    {
    	int i=0;
    	for(i=0;i<3;i++)
    	{
    		printf("Enter the employee ID");
    		scanf("%d",Employee[i].id);
    		printf("Enter the employee first name");
    		scanf("%s",Employee[i].firstname);
    		printf("Enter the employee last name");
    		scanf("%s",Employee[i].lastname);
    	}
    }

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    When passing an array to a function, it automatically gets converted to a pointer. So when you call InputEmployeeRecord(&e1) you're actually passing a pointer to a pointer. Get rid of the dereference and you should be fine.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    I actually solved it, and just saw your post now Clairvoyant1332, this is what I ended up with:

    Code:
    #include <stdio.h>
    struct employee
    {
    	char firstname[40];
    	char lastname[40];
    	int id;
    };
    typedef struct employee Employee;
    
    void InputEmployeeRecord(Employee *ptrEmployee);
    
    int main()
    {
        Employee e1[3];
        InputEmployeeRecord(e1);
        return 0;
    }
    
    void InputEmployeeRecord(Employee *ptrEmployee)
    {
    	int i=0;
    	for(i=0;i<3;i++)
    	{
    		printf("Enter the employee ID:\n");
    		scanf("%d",&ptrEmployee[i].id);
    		printf("Enter the employee first name:\n");
    		scanf("%s",ptrEmployee[i].firstname);
    		printf("Enter the employee last name:\n");
    		scanf("%s",ptrEmployee[i].lastname);
    	}
    }
    guess I just had to fiddle around, and use your tips. Thanks everyone for helping, really appreciate it.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    scanf("%d",Employee[i].id);
    You need to provide the address of a variable when using scanf so that it has a memory location to read into. Use

    Code:
    scanf("%d",&Employee[i].id);

  10. #10
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    Yea, I forgot about that before, good call there, and thanks again.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Actually, &e1 (in this case) is a pointer to an array, not a pointer to a pointer.

    To pass &e1, the function prototype would need to be.
    void InputEmployeeRecord(Employee (*ptrEmployee)[3] );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Your actually no better off, using scanf("%s", *p) than gets() if you don't restrict how many characters scanf() can read.

    In your case you can do like this if you want to use it: scanf("%39s", *p);
    That will leave room for your string and terminating null character.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    I never knew about being able to restrict with scanf, thanks for showing me that, and I think the e1 works, I will have to check else I can change that too, this is a really helpful board . It all works, I built another function to print it out and verify too, I like how you don't give the answer only assist, much better for learning.
    Last edited by JamesD; 03-10-2011 at 01:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. mandelbrot set program improvements
    By mad_muppet in forum Game Programming
    Replies: 3
    Last Post: 07-14-2010, 05:58 AM
  3. I'm donating my vector lib.
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 06-24-2010, 06:10 PM
  4. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  5. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM