C Structure and Function
C Structure and Function
C Structure and Function
Similar to variables of built-in types, you can also pass structure variables
to a function.
C structures
C functions
User-defined Function
Here's how you can pass structures to a function
#include <stdio.h>
struct student {
char name[50];
int age;
};
// function prototype
void display(struct student s);
int main() {
struct student s1;
return 0;
}
Output
Displaying information
Name: Bond
Age: 13
#include <stdio.h>
struct student
{
char name[50];
int age;
};
// function prototype
struct student getInformation();
int main()
{
struct student s;
s = getInformation();
printf("\nDisplaying information\n");
printf("Name: %s", s.name);
printf("\nRoll: %d", s.age);
return 0;
}
struct student getInformation()
{
struct student s1;
return s1;
}
#include <stdio.h>
typedef struct Complex
{
float real;
float imag;
} complex;
int main()
{
complex c1, c2, result;
return 0;
}
void addNumbers(complex c1, complex c2, complex *result)
{
result->real = c1.real + c2.real;
result->imag = c1.imag + c2.imag;
}
Output
result.real = 4.5
result.imag = -5.6