Pages

Showing posts with label c programming. Show all posts
Showing posts with label c programming. Show all posts

Monday, April 27, 2015

Understanding pointer in C programming language

**dynamic memory allocation

manage memory.

get block of memory of any size and control using pointer.

pointer = location in memory.



ex:

#include <stdio.h>

main(){
int angka;
int* pointer;



angka = 5;


pointer = &angka;  //  & = alamat memory dari.


  printf( "angka:  %i\n", angka ); // 5
  printf( "alamat memory dr angka", &angka ); // 0xbfffc5c
  printf( "alamat pointer", pointer ); // 0xbfffc5c

  printf( "value pointer: %i\n", *pointer ); // 5
}



======
decimal vs hexadecimal


Decimal has 10 possible values for each digit:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Hexidecimal has 16 possible values for each digit:                
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f


0xbfffc5c

0x -> prefix / menandai sebuah bilangan hexadecimal.  bffc5c -> angkanya
======

int* pointer  -> pointer declaration

*pointer ->  get pointer value.



======

** dynamic memory.

understanding memory allocation in C programming language

int Human    ->   4 bytes of memory.


memory bisa terdapat di 2 tempat :   data segment   /   stack.


jika dideklarasikan diluar function ( global var ), masuk ke data segment.
Selama program dijalankan , memory tetap akan digunakan.


stack ->  berisi variabel yang terdapat didalam function.
 sifatnya temporary.
 setelah selesai digunakan, memory dibebaskan dan dapat digunakan oleh program / function lain.


======

#include <stdio.h>

int number_of_worker = 10;


void addWorker(){
int stackWorker = 3;  -> memory berada di stack.
number_of_worker += stackWorker;
}


main(){
printf ("there is %i number of worker", number_of_worker);
addWorker();
printf ("there is %i number of worker", number_of_worker);
addWorker();
printf ("there is %i number of worker", number_of_worker);
}


c programming basic documentation

Basic C Concepts


**compiler creates useable programs from C source
**typed variables kind of data that a variable contains
**typed functions the kind of data returned from a function
**header files (.h) declare functions and variables in a separate file
**structs groups of related values
**enums lists of predefined values
**pointers aliases to other variables



compiled to binary program ( faster than scripting ).
need to porting to other os in more complex program.


without pointer C is identically to php




cth program  : hello.c

#include <stdio.h> main () { printf ("I'm a C program\n"); }

$ gcc hello.c -o hello
$ ./hello



=====
** typed variables.


in scripting, var can be declare and change to one or other type freely.

ex: $variable = 2; $variable = 1.618; $variable = 'A';

in c, we must declare the type of data first!

int variable1 = 2; float variable2 = 1.618; char variable3 = 'A';