The Right Time to Use Struct, Pointer/References, Array, Function and The Combination Example
When is the Right Time to Use Struct?
struct in c also called as structures. usually, struct provide a way of storing many different values with the same group or name. we used struct when we have a lot of data that need to be group for instance. we also used struct when we want to create a new data or information.
Example of Struct?
for example, i want to create a new information about three students. the information contains their name, age and grade. in this case i will use looping to named student 1, student 2, student 3.
#include <stdio.h>
struct student {
char name[50]; // declare name using array
int age;
int grade;
};
int main ()
{
struct student info[3]; //declare 3 students [3]
int i;
for(i=1;i<=3;i++) { // this is for the looping, student 1, student 2, student 3
printf("Enter name for student %d : ",i);
scanf("%s",info[i].name); // called struct using "." (dot)
printf("Enter age for sudent %d : ",i);
scanf("%d",&info[i].age);
printf("Enter grade for student %d : ",i);
scanf("%d",&info[i].grade);
}
return 0;
}
When is the Right Time to use Pointer/References?
pointer is a data type that store the address of another variable. but you must declare a pointer before you using it to store any variable address. we used pointer when we want to change the value or address of a variable that already declared. we used references if we want to give another name of variable. references also known as alias.
Example of pointer/references?
for example: i want to create a variable named "c" and i will give it another name (alias/references) called "si". so when i called the address of "si", it was the same with i called the address of "c".
#include <stdio.h>
int main () {
int c = 15;
int &si = c;
si = 20;
printf("%d\n",c); // it's ok if you change "c" in the printf into "si" because it's actually the same
}
When is the Right Time to Use Function?
we use function when we have the same code/command in our source code. so it will make it short. and if you want to change to the command you just need to do it in function.
Example of function?
for example: i want to make the same command for 2 pairs of variables. the first one is to declare the function. called "a" and "b". the second one is the actual variable declaration, called "i" and "j". the same command is sum or addition.
#include <stdio.h>
int jumlah(int a, int b) // these are parameters
{
int result;
result = a + b;
return result;
} // if you use function use int, you must return something.
int main ()
{
int i = 5;
int j = 15;
int sum;
sum = jumlah(i,j);
printf ("%d\n",sum);
return 0;
}
When is the right time to use array?
the right time to used array is when we have a lot of data. and we can simplify it into one variable.
Example of array?
for example : i want to make some elements of array by the input by user (eg. 5 element). and then he must write the 5 values for those 5 elements. (eg. 1,2,3,4,5). then the output will be element 1 = 1, element 2 = 2. something like that.
#include <stdio.h>
main()
{
int array[100], n, c;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Array elements entered by you are:\n");
for ( c = 0 ; c < n ; c++ )
printf("array[%d] = %d\n", c, array[c]);
return 0;
}
The combination of all above in one source code?
#include <stdio.h>
#include <string.h>
// declare a struct
struct person
{
int age;
char name [50]; // this is array in name
};
//declare a function
void combination(struct person *prsn);
int main ()
{
struct person prsn;
prsn.age = 15;
strcpy(prsn.name, "Miracle");
combination(&prsn);
return 0;
}
//function called
void combination(struct person *prsn) { //pointer to struct
printf("Person Name: %s \n",prsn->name);
printf("Person Age: %d \n",prsn->age);
}
struct student {
char name[50]; // declare name using array
int age;
int grade;
};
int main ()
{
struct student info[3]; //declare 3 students [3]
int i;
for(i=1;i<=3;i++) { // this is for the looping, student 1, student 2, student 3
printf("Enter name for student %d : ",i);
scanf("%s",info[i].name); // called struct using "." (dot)
printf("Enter age for sudent %d : ",i);
scanf("%d",&info[i].age);
printf("Enter grade for student %d : ",i);
scanf("%d",&info[i].grade);
}
return 0;
}
When is the Right Time to use Pointer/References?
pointer is a data type that store the address of another variable. but you must declare a pointer before you using it to store any variable address. we used pointer when we want to change the value or address of a variable that already declared. we used references if we want to give another name of variable. references also known as alias.
Example of pointer/references?
for example: i want to create a variable named "c" and i will give it another name (alias/references) called "si". so when i called the address of "si", it was the same with i called the address of "c".
#include <stdio.h>
int main () {
int c = 15;
int &si = c;
si = 20;
printf("%d\n",c); // it's ok if you change "c" in the printf into "si" because it's actually the same
}
When is the Right Time to Use Function?
we use function when we have the same code/command in our source code. so it will make it short. and if you want to change to the command you just need to do it in function.
Example of function?
for example: i want to make the same command for 2 pairs of variables. the first one is to declare the function. called "a" and "b". the second one is the actual variable declaration, called "i" and "j". the same command is sum or addition.
#include <stdio.h>
int jumlah(int a, int b) // these are parameters
{
int result;
result = a + b;
return result;
} // if you use function use int, you must return something.
int main ()
{
int i = 5;
int j = 15;
int sum;
sum = jumlah(i,j);
printf ("%d\n",sum);
return 0;
}
When is the right time to use array?
the right time to used array is when we have a lot of data. and we can simplify it into one variable.
Example of array?
for example : i want to make some elements of array by the input by user (eg. 5 element). and then he must write the 5 values for those 5 elements. (eg. 1,2,3,4,5). then the output will be element 1 = 1, element 2 = 2. something like that.
#include <stdio.h>
main()
{
int array[100], n, c;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Array elements entered by you are:\n");
for ( c = 0 ; c < n ; c++ )
printf("array[%d] = %d\n", c, array[c]);
return 0;
}
The combination of all above in one source code?
#include <stdio.h>
#include <string.h>
// declare a struct
struct person
{
int age;
char name [50]; // this is array in name
};
//declare a function
void combination(struct person *prsn);
int main ()
{
struct person prsn;
prsn.age = 15;
strcpy(prsn.name, "Miracle");
combination(&prsn);
return 0;
}
//function called
void combination(struct person *prsn) { //pointer to struct
printf("Person Name: %s \n",prsn->name);
printf("Person Age: %d \n",prsn->age);
}