0
#include<stdio.h>
#include<string.h>

struct employee
{
   int id;
   char name[20];
   float salary;
   char addr[];   
}*emp;

void get_emp(struct employee **record)
{

   printf("\tplease enter details of employee:\n");

   printf("id= ");
    scanf("%d",(*record)->id);//code to get input value

   printf("Name= ");
   scanf(" %s",(*record)->name);

   printf("salary= ");
   scanf("%f",(*record)->salary);


}

int main()
{
get_emp(&emp);
printf("id=%d\n",emp->id); // code to display the value
printf("Name=%s\n",emp->name);
printf("salary=%f\n",emp->salary);


return 0;
}

I have a structure example and I want to pass structure pointer to the function without using normal variable but only pointer. Is the parameter(double pointer) in the function get_emp(struct employee **record) a correct way to do if not what changes should be made? Also how to get input value from users in the function get_emp(struct employee **record) and how to display the value?

2
  • 1
    You haven't compiled and run this code have you. You're dereferencing a null pointer: emp is initialized to NULL (static storage), and it's a pointer. You're not allocating the memory for the struct anywhere, so (*record)->id is like writing (NULL)->id, which is of course wrong Commented Mar 9, 2016 at 17:35
  • I have compiled and there's shows nothing wrong with code. whenever i try to take input it stop working and exit the program. Commented Mar 10, 2016 at 0:50

1 Answer 1

1

A double pointer is not needed. You can accomplish this with single pointer as well. You would however need to allocate memory for the structure before populating it.

struct employee *emp = malloc(sizeof(struct employee));

Also the size of char addr[] in your structure is undefined. That needs to be allocated separately similar to:

emp->addr = malloc(N*sizeof(*emp->addr));

All other fields in the structure which has a fixed size such as integer,float and a character array of 20 elements does not need allocation. The allocation for them would be done by malloc we did earlier. However since addr is a pointer, memory equivalent to size of pointer would be reserved for it. To store anything in the address the addr points to, we need to allocate.

Afterwards pass emp to your function. Remove the double pointer

Sign up to request clarification or add additional context in comments.

4 Comments

Don't cast the return value of malloc. Also: sizeof(char) is guaranteed to be 1, no point in adding that. The preferred way to allocate memory is: some_ptr = malloc(N * sizeof *some_ptr); Because it does what it's supposed to, always: malloc N times the size of the the type some_ptr points to. some_ptr can change type, the malloc call will still work. regardless of the levels of indirection you're dealing with, using the sizeof *destination_ptr will always work
Thanks. I'll make the correction. That's a very beneficial advice
Thanks sharad and elias. It was all about memory allocation to structure pointer. what is good code to get input as scanf("%d",(*record)->id) , scanf("%d",&record->id) and scanf("%d",record->id) works all fine.
If you are using single pointer then scanf("%d",&record->id) is correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.