unable to access structure within a function

This is a discussion on unable to access structure within a function within the C Programming forums, part of the General Programming Boards category; Hi Everybody, My mission is to understand basic Abstract data types such as ADT array, linked lists double linked lists ...

  1. #1
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    unable to access structure within a function

    Hi Everybody,

    My mission is to understand basic Abstract data types such as ADT array, linked lists double linked lists stacks etc...

    My immediate objective is to make an array of employees to which I can assign values. With that purpose I created a pointer structure which I called *emp_ptr . The idea is that I develop this program to the point where I use linked lists, arrays etc...

    Anyway, To make things more challenging, I decided not to have global variables, so I buried the structure in a function which is called employee_create(); However it turns out that despite having tried I still don't understand how to access the values of the structure within the function 'employee_create();' from another function which I called 'employee_data();'

    Here is my code:

    Code:
    /*
     * test1.c
     *
     *  Created on: Feb 9, 2010
     *      Author
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    //creates an employee
    void employee_create() {
    
        //Structure employee
    
        typedef struct employee {
       
            char name[30];
            char address[100];
            int age;
            float salary;
        }*emp_ptr[100];
    
        emp_ptr = malloc(sizeof (emp_ptr)); // memory allocation for variable emp - is this right?
        
    }
    
    // Takes input for employee
    
    void employee_data(employee_create(employee *emp_ptr)) { // I thought to pass a //parameter but this of course will not work as this function  does not know the //local variables of the employee_create();
        
      
        printf("Enter the name of the employee: ");
        scanf("%s", emp.name);  // this will not work because I cannot access the data as it is now
        printf("Enter the address of the employee: ");
        scanf("%s", emp.address);
        printf("Enter the age of the employee: ");
        scanf("%d", &emp.age);
        printf("Enter the salary of the employee: ");
        scanf("%f", &emp.salary);
    
    }
    
    // prints employee
    
    employee print_employee() {
    
        printf("employee information summary:\nName: %s\nAddress: %s\nAge: %d\nSalary: %f\n", emp.name, emp.address, emp.age, emp.salary);
    
    }
    
    int main(void) {
    
        employee_data(); //takes employee data
        print_employee(); //prints employee data
    
        printf("The mmeory address of emp is: %p\n", &emp);
    
    
        return 0;
    
    }

    Can somebody point out how to access the structure's values when they are buried inside another function? I thought to use pointers... Am I going through the right direction?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I like that. You must have read too much about "global variables are bad".
    There is no way to access emp_ptr from outside employee_create()
    Both the type employee and the array emp_ptr exist only in the context of employee_create() .
    Kurt

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    o_O
    What's this?
    >>void employee_data(employee_create(employee *emp_ptr))

    Why does print_employee return something while you don't have any return statement?
    >>employee print_employee()

    Code:
        typedef struct employee {
       
            char name[30];
            char address[100];
            int age;
            float salary;
        }*emp_ptr[100];
    
        emp_ptr = malloc(sizeof (emp_ptr)); // memory allocation for variable emp - is this right?
    Here you create 100 pointers to employee. And then you try to use malloc to create an employee object... yet you do not specify which out of all the 100 pointers should get the address returned from malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    o_O
    What's this?
    >>void employee_data(employee_create(employee *emp_ptr))

    Why does print_employee return something while you don't have any return statement?
    >>employee print_employee()

    Code:
        typedef struct employee {
       
            char name[30];
            char address[100];
            int age;
            float salary;
        }*emp_ptr[100];
    
        emp_ptr = malloc(sizeof (emp_ptr)); // memory allocation for variable emp - is this right?
    Here you create 100 pointers to employee. And then you try to use malloc to create an employee object... yet you do not specify which out of all the 100 pointers should get the address returned from malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    unable to access structure within a function

    Hi all,

    Thank you all for the answers!!!

    My mistake .....Elysia >>void employee_data(employee_create(employee *emp_ptr))

    That was a bad mistake! I was pllaying around with parameters, I wanted to make the function more simple and I thought I could make it return a value, perhaps an employee instead of all those scanf and printf but I just gave up on it, and then I forgot to remove the return type.... my mistake....


    However, regarding Zuk's answer.... is there absolutely no way to access structures that are buried inside functions then? I know that structures and even functions can be passed as parameters, and that there can be a structure within a structure but why can we not access a structure within a function? is there no way around? What if I declare a typedef struct employee as a global variable and then I parametrize it on the functions where I need? could that work? would that be reliable?

    br,

    Blue
    Last edited by bluetxxth; 02-16-2010 at 03:02 PM. Reason: missing words

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    The whole point with local variables is that they can be accessed only within those functions. You should pass along the necessary parameters to functions that needs them.
    Is there a reason you do not want to do this?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    unable to access structure within a function

    Hi Elysia,

    That is exactly what I want but I think that I am doing it wrong. That is I am not sure which parameters to pass to the function employee_data(); so it knows about the variables inside the structure.

    so far I did this

    Code:
    void employee_data(employee_create(employee *emp_ptr)){} // I am trying to pass the structure as a parameter but this is wrong, I think...


    Here is the whole function in question and the structure I want to access:


    Code:
    
    
    // Takes input for employee
    
    void employee_data(employee_create(employee *emp_ptr)) { // here I am trying to pass // the structure as a parameter but I think I am doing it wrong
        
        printf("Enter employee id: ");
        scanf("%d",emp.index);
        printf("Enter the name of the employee: ");
        scanf("%s", emp.name);
        printf("Enter the address of the employee: ");
        scanf("%s", emp.address);
        printf("Enter the age of the employee: ");
        scanf("%d", &emp.age);
        printf("Enter the salary of the employee: ");
        scanf("%f", &emp.salary);
    
    }
    Last edited by bluetxxth; 02-17-2010 at 01:20 AM. Reason: further elaboration

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    A function is a function. It takes parameters, but you must specify what parameters to pass when calling it! So, you need to call this employee_create first and attain the data you need, then call employee_data with the information.
    You cannot specify such things in declarations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 07-23-2009, 03:20 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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