The Right time to use Struct, Pointer, Function and Array


Struct

Struct is a data type that can perform multiple data storage interlinked (such as name, id number, age) as a whole. Struct consists of several members, members of the struct called members, and members can also be called a regular variable. Members in a structure may consist of different data types, such as pointers, integer, character, and others. Here is an example of structure.

the general form:


struct Tag {
  Member1;
  Member2;
  Member3;
};


Example:


struct Student{
char name[10];
int id_number;
int age;
};



The right time to use Struct is when we need to create a new data type that can store multiple variable. like the example in Student struct can contain multiple variable that is character and integer.


Pointer

A pointer is an address that contains the data object to memory location in which a stored value.
Variable - variable pointer variable declaration is declared as usual, just added the sign ( * ) behind variable name.
the general shape of the pointer is :
The type of data * variable name ;

Where the data type is the type of data stored at the address designated by variable_name.


Example:

Int * pointer ;
Float * pointer ;


The first * pointer is a pointer to an integer data type and second *pointer is a pointer to float data type.
The value of the pointer variables can be declared at the same time.
the first value is the address in the memory location.

The right time to use pointer is when we need to change a variable content on another's function.

Example :

int point(int *x) {
  *x = 5;
}
int main() {
   int y = 0;
   point(&y);
   printf("%d\n", y);
}


in this program the first value of y = 0 is change to 14 because of the pointer.

Function


function is several statements which together perform a task.  every program even most simple program has at least one function. So we always need function. 


Example:


#include <stdio.h>

/* function declaration */
int max(int num1, int num2);

int main () {

   /* local variable definition */
   int a = 100;
   int b = 200;
   int ret;

   /* calling a function to get max value */
   ret = max(a, b);

   printf( "Max value is : %d\n", ret );

   return 0;
}




The right time to use function is when we have the same loop and the loop used repeatedly so we just write it once.

Array


Array is used to store variables with the same data type. array can simplify the code.

The right time to use array when the number of inputs to be stored are too many and  when we want to simplify the loop.

font-family: "times" , "times new roman" , serif; font-size: large;">
font-family: "times" , "times new roman" , serif; font-size: large;">Example That Uses Combination Of Struct, Pointer/reference, Function, Array

#include <stdio.h>
int main(){
struct student{       // this is struct
    char name[10]; // this is array inside struct
    int id;
    int marks;
}
stud1 = {"Manchunian

",2,85}; // access struct inside function
struct student *ptr;
ptr = &stud1;  // this is pointer to struct

printf("Name :%s\n",(ptr)->name);
printf("ID: %d\n",(ptr)->id);
printf("Marks of Student : %d\n",(ptr)->marks);

return 0;
}



How to Install Qt Creator and SDL in Windows


How To Install Qt Creator


Whether you are creating a mobile app, desktop application or a connected embedded device, Qt Creator is the cross-platform IDE that makes application and UI development a breeze. Since time-to-market is key, the IDE includes productivity tools that speed up your development time.

Here is step by step to install Qt Creator in Windows :

1.  You must have Qt Creator installer, you can download it here.

          Look for look for "MinGW", for example Qt 5.5.1 for Windows 32-bit (MinGW 4.9.2, 1.0 GB)



2.  If done, click for the installer,



3.  This dialog will be shown, then click next,

4.  Then the Message for sign in will be shown, but we can skip this,



5. Set the directory, then next



6.  Select the components that you want to install, then next



7.  License agreement will be shown, choice I have read ........ then click next



8.  Start menu shorcuts, just click next



9.  Then click install




 10. Finish install, now you can launch the program or just click finish.



After the installation done we should install SDL as the library,

1.  Download this file,

2.  Extract file to new folder. Ex : C:\Software\SDL



3. To create a new SDL2 project in Qt Creator, choose File > New Project > Plain C++ Project, 
     
Ex : C:\Temp" with project name: "untitled1



4. Copy all *.dll files from your SDL folder (C:\Software\SDL\bin) to build folder. 
     
Ex :  C:\Temp/untitled1/here, Like this one.



5.  Add these 3 lines on your .pro project file.
   
INCLUDEPATH += C:/Software/SDL/include
     LIBS += -LC:/Software/SDL/lib -lmingw32 -mwindows -mconsole -lSDL2main -lSDL2
     CXXFLAGS = -std=c++11



6.  Try to make basic SDL program to test your installation.


7. Done.

Diberdayakan oleh Blogger.

Text Widget

Popular Posts

Recent Posts

Copyright © 2025/ Learn About C / C++

Template by : Urangkurai / powered by :blogger