Lesson II
Pointers and Array
Pointer
Pointer is a variable that store the address of another variable.
e.g.:
int numb=5; //address:664512
int *pointer;
pointer=&numb;
the value of pointer will be 664512.
to print the value of the numb itself using the pointer, you will have to use '*', before the pointer's name.
in this case: *pointer
and they will give you the value of the address stored in the pointer.
in pointers, you can also point a pointer, the rules is you only got to point the one who got less than 1 star from you.
Array
Array is a collection of the same data type and stored in a name.
e.g.:
int a[5]= {10,11,12,13,14}
a can only store 5 elements, if you're giving more than five elements, it'll causing an error. and if you're giving less than five elements, they'll think the rest as 0.
although they have five elements, on C language, they're counting from index 0, so a[4] is 14, and a[5] does not exist.
p.s.: you could write a[4] as *(a+4)
Char and string
there's a slightly difference between the array of char and the array of int.
as for the array of integers, i have already got you the explanation above, so now let's take a look to the array of char.
e.g.:
char name[20];
it means that we save 20 spaces for 'name'.
the difference between char and string is usually by how they were given the elements.
char[20]={'b','i','n','u','s'}; ->char
char[20]="binus'; ->string
and string has a \0 at the very back. e.g.: binus\0, and the \0 will take 1 space.
Thank you for visiting.
Let me know in the comment if you got a question or anything, or you can just stop by saying hi!
Pointers and Array
Pointer
Pointer is a variable that store the address of another variable.
e.g.:
int numb=5; //address:664512
int *pointer;
pointer=&numb;
the value of pointer will be 664512.
to print the value of the numb itself using the pointer, you will have to use '*', before the pointer's name.
in this case: *pointer
and they will give you the value of the address stored in the pointer.
in pointers, you can also point a pointer, the rules is you only got to point the one who got less than 1 star from you.
Array
Array is a collection of the same data type and stored in a name.
e.g.:
int a[5]= {10,11,12,13,14}
a can only store 5 elements, if you're giving more than five elements, it'll causing an error. and if you're giving less than five elements, they'll think the rest as 0.
although they have five elements, on C language, they're counting from index 0, so a[4] is 14, and a[5] does not exist.
p.s.: you could write a[4] as *(a+4)
Char and string
there's a slightly difference between the array of char and the array of int.
as for the array of integers, i have already got you the explanation above, so now let's take a look to the array of char.
e.g.:
char name[20];
it means that we save 20 spaces for 'name'.
the difference between char and string is usually by how they were given the elements.
char[20]={'b','i','n','u','s'}; ->char
char[20]="binus'; ->string
and string has a \0 at the very back. e.g.: binus\0, and the \0 will take 1 space.
Thank you for visiting.
Let me know in the comment if you got a question or anything, or you can just stop by saying hi!
Comments
Post a Comment