Kamis, 12 Maret 2020

Summary By Jonathan Widarta 2301862203

Let's discuss pointer,


But before that look at this explanation,
when we declare a variable we need data type, variable's name, value.

e.g

 int x = 6;

int, is a data type
x, is variable's name
6, is the value

Here integer will allocate 2 bytes (in 16 bit system) or 4 bytes ( in 32 bit system) of memory to hold the value of 6. It also make symbol table to add the symbol x and the address in the memory where those 2/4 bytes were set.

So, pointer is a data type alike whose value refers to another value store.

e.g

int x = 9;
int *y = &x;   (data type y needs to be the same with x which is int)

In using pointer like the example above, we need this symbol (*) and pairs with (&).

(*) means that it will store an address;
(&) means that it is an address;

Another example,
e.g
int z = 9;
int *x = &z;      (x gets the address of z)

printf("%d\n", x);
//       (x prints the address z)
printf("%d\n", *x);
//      (x  prints the value of z)
printf("%d\n", &x);
//     (x  prints the address of pointer x)

int *y = x;

printf("%d\n", y);   
//   (y prints the address x)
printf("%d\n", *y); 
//  (y prints the value of x)
printf("%d\n", &y);
//   (y prints the address of pointer y)


It is a perfect example to make us understand the concept of pointer.

It

Let's discuss , about array

Array is a collection of similar data. It is similar because it has the same data type. Elements in array are stored in consecutive memory locations and are referenced by an index for example

int x[10];

int, is a data type
x, is a variable name
[10] ,  means 10 memory allocated

It is a one dimensional array declaration. Declaration needs the memory allocation needed, which is 10 in this example.
You can initialize it by using,

int x[3] = { 4, 11, 3};

or

int x[] = {4, 11 , 3};

The two dimensional would be like this,

int x[10][10]; // declaration

int x[2][2] = {{1,1},{1,2}} // initialisation

or
int x[][] = {{1,1},{1,2}}

if we call the print function it would be like this,

printf("%d\n", x[1][1]); // it will print out 2, why?? It is because index called starts from 0.


Easy right??

We can scan numbers into an array by using as below,

for(int i =0; i<3;i++){
for(int j=0;j<3;j++{

scanf("%d", &x[i][j]); getchar();
}
}

Let's discuss , about Struct or Structure


A struct has an abstract data type which means it could store different data types at one structure.

How to declare it??

Struct structName{

int x;
char name[101];
char a;

}callStruct;


So, we have a struct consists of integer x, char name[101], and char a. To call this struct
we can use it by calling the callStruct in main.

e.g

int main(){

scanf("%d %s %c", &callStruct.x , callStruct.name, &callStruct.a);

printf("%d %s %c", callStruct.x , callStruct.name, callStruct.a;

}

If we want to make the struct as an array (array of struct) we could use,

e.g

Struct structName{

int x;
char name[101];
char a;

}callStruct[3];

int main(){


scanf("%d %s %c", &callStruct[0].x , callStruct[0].name, &callStruct[0].a);

printf("%d %s %c", callStruct[0].x , callStruct[0].name, callStruct[0].a;

}


It's easy right?? Goodluck on the next session. Only continuing if there are 30 comments.










Tidak ada komentar:

Posting Komentar