Okaaay here we goooo
First, STRUCT.
You can find my post about STRUCT to know more about STRUCT. I just want to review some explanation about STRUCT. STRUCT is the collection of variables of different types under a single name for better handling. When the right time to use STRUCT? When the programmers have a lot of data of different kinds that needs to be grouped together, it can be used to hold or to represent records from a database.
For the example, when you want to store records of students which consist of student name, address, phone number, age, student id, etc.
The syntax is:
struct struct_name {
//Statments//
};
Second, POINTER or REFERENCE.
POINTER is a memory address. POINTER is not the variable, but it is the content of the variable. When the right time to use POINTER? When the programmers want to access the addresses of the variable and manipulate the contents of the variables. We must use POINTER correctly, it could improve the efficiency and performance of our program, but if we use it incorrectly, it could lead our program to many dangerous problem, from un-readable to infamous bugs such as memory leaks, etc.
The syntax is:
type *ptr;
or
type* ptr;
or
type * ptr;
REFERENCE allows you to create a second name for the variable that you can use to read or modify the original data stored in the variable. When we declare a REFERENCE and assign it variable, it will allow us to treat the REFERENCE exactly as thought it were original variable for the purpose.
The syntax is:
type& reference = variable;
Third, FUNCTION.
FUNCTION is a module of code that takes information in, does some computation, and returns a new piece of information based on the parameter informations. A function declaration tells the compiler about a function's name, return type, and parameter. When program call the function, the program control is transferred to the called function. A called function performs a defined task and when it return statement is executed. There're 3 aspects in C FUNCTION, they are:
No. | C function aspects | syntax |
1 | function definition (contains all the statements to be executed) | return_type function_name ( arguments list ) { Body of function; } |
2 | function call (calls the actual functions) | function_name ( arguments list ); |
3 | function declaration (informs compiler about the function name, parameters, and return value's data type) | return_type function_name ( argument list ); |
Last, ARRAY.
ARRAY is sequence of data item of same type. ARRAY is used to store collection of data, but it is often more useful to thin of an ARRAY as a collection of variable of the same type. All ARRAY consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. ARRAY allows us to use a for loop to access the elements by their index. When writing embedded software or an operating system an ARRAY may often be the better choice. While an ARRAY offers less functionality, it takes up less RAM, and the compiler can optimized code more efficiently for looks-up into ARRAY. The look-up time for a random access is much faster for lots of memory locations that containing the values, only taking a constant number of operations.
The syntax is:
type_name[number of elements];
Here some examples of combination STRUCT, POINTER/REFRENCE, FUNCTION,and ARRAY
1.
#include <stdio.h>
#include <string.h>
struct student {
int id;
char name[9];
float score;
};
int main() {
struct student record1 = {2, "Nikita", 85.5};
struct student *ptr;
ptr = &record1;
printf("Records of STUDENT: \n");
printf("Id: %d\n", ptr->id);
printf("Name: %s\n", ptr->name);
printf("Score: %f\n", ptr->score);
return 0;
}
Output:
Records of STUDENT:
Id: 2
Name: Nikita
Score: 85.500000
2.
#include <stdio.h>typedef struct complex {
float real;
float imag;
} complex;
complex add(complex n1,complex n2);
int main() {
complex n1,n2,temp;
printf("For 1st complex number \n");
printf("Enter real and imaginary respectively:\n");
scanf("%f%f",&n1.real,&n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter real and imaginary respectively:\n");
scanf("%f%f",&n2.real,&n2.imag);
temp=add(n1,n2);
printf("Sum=%.1f+%.1fi",temp.real,temp.imag);
return 0;
}
complex add(complex n1,complex n2) {
complex temp;
temp.real=n1.real+n2.real;
temp.imag=n1.imag+n2.imag;
return(temp);
}
Output:
For 1st complex number
Enter real and imaginary respectively:
For 2nd complex number
Enter real and imaginary respectively:
OK!
That's all from me! Hope u will understand! As usually, have a great time with coding! Jesus bless u!