Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to pass nested struct to function using pointers in C

3 Answers

0 votes
#include <stdio.h> 
  
typedef struct
{
   int data;
} info;
 
typedef struct
{
   info age;
   float salary;
} worker;
  
void print_struct(worker *w);
  
int main(void)
{
    worker w;
     
    printf("Enter Age: ");
    scanf("%d", &w.age.data);
    printf("Enter Salary: ");
    scanf("%f", &w.salary);
 
    print_struct(&w);
  
    return 0;
}
 
void print_struct(worker *w)
{
    // option 1
    printf("age = %d\n", w->age.data);
    printf("salary = %.2f\n", w->salary);
     
    // option 2
    printf("age = %d\n", (*w).age.data);
    printf("salary = %.2f\n", (*w).salary);
}
 
 
 
/*
run:
  
Enter Age: 45
Enter Salary: 40000
age = 45
salary = 40000.00
age = 45
salary = 40000.00
 
*/


answered Oct 8, 2014 by avibootz
edited Dec 5, 2024 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>  
   
typedef struct
{
   int data;
} info;
  
typedef struct
{
   info age;
   float salary;
   char *address;
} worker;
   
void print_struct(worker *w);
   
int main(void)
{
    worker w;
      
    printf("Enter Age: ");
    scanf("%d", &w.age.data);
    printf("Enter Salary: ");
    scanf("%f", &w.salary);
    fflush(stdin); fgetc(stdin);
     
    w.address = (char*) malloc(32 * sizeof(char));
    if (w.address == NULL)
    {
        printf("malloc failed\n");
        return -1;
    }
    printf("Enter Address: ");
    
    char ch;
    int i;
    while ((ch = fgetc(stdin)) != EOF) {  
        if (ch == '\n') {  
            break;  
        }
        w.address[i++] = ch;
    } 
    
    print_struct(&w);
     
    free(w.address);
     
    return 0;
}
  
void print_struct(worker *w)
{
    // option 1
    printf("age = %d\n", w->age.data);
    printf("salary = %.2f\n", w->salary);
    printf("address = %s\n", w->address);
      
    // option 2
    printf("age = %d\n", (*w).age.data);
    printf("salary = %.2f\n", (*w).salary);
    printf("address = %s\n", (*w).address);
}
  
  
  
/*
run:
   
Enter Age: 49
Enter Salary: 42000
Enter Address: 112 Mercer Street
age = 49
salary = 42000.00
address = 112 Mercer Street
age = 49
salary = 42000.00
address = 112 Mercer Street
  
*/


answered Feb 23, 2015 by avibootz
edited Dec 5, 2024 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>  
   
typedef struct
{
   int data;
} info;
  
typedef struct
{
   info age;
   float salary;
   char *address;
} worker;
   
void print_struct(worker *w);
   
int main(void)
{
    worker w;
      
    printf("Enter Age: ");
    scanf("%d", &w.age.data);
    printf("Enter Salary: ");
    scanf("%f", &w.salary);
    fflush(stdin); fgetc(stdin);
     
    w.address = (char*) malloc(32 * sizeof(char));
    if (w.address == NULL)
    {
        printf("malloc failed\n");
        return -1;
    }
    printf("Enter Address: ");
    
    fgets(w.address, 31, stdin);
    
    print_struct(&w);
     
    free(w.address);
     
    return 0;
}
  
void print_struct(worker *w)
{
    // option 1
    printf("age = %d\n", w->age.data);
    printf("salary = %.2f\n", w->salary);
    printf("address = %s\n", w->address);
      
    // option 2
    printf("age = %d\n", (*w).age.data);
    printf("salary = %.2f\n", (*w).salary);
    printf("address = %s\n", (*w).address);
}
  
  
  
/*
run:
   
Enter Age: 60
Enter Salary: 51000
Enter Address: 112 Mercer Street
age = 60
salary = 51000.00
address = 112 Mercer Street

age = 60
salary = 51000.00
address = 112 Mercer Street
  
*/

 



answered Dec 5, 2024 by avibootz

Related questions

1 answer 180 views
180 views asked Oct 8, 2014 by avibootz
1 answer 104 views
1 answer 44 views
2 answers 132 views
1 answer 305 views
1 answer 115 views
...