Selasa, 24 November 2015

Struct in C

Struct in C

Struct?
struct is structures, 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. struct contains information about the data.

Defining Struct?
to define structure, you must used struct statement. the syntax for struct is:
struct student {
char name [100];
int age;
int height;
int weight;
};

Accessing Struct members?
you can access any member of the structures, and commonly we used access operator (.). to make it clear, you can see the full example how to use struct:
#include <stdio.h>
struct students {
char name[100];
int age;
int weight;
int height;
};

int main () {
struct students student1;            //declare student1 of the student
struct students student2;            //declare student2 of the student

//access the information about student1
strcpy(student1.name, "Serafin William");
student1.age = 18;
student1.weight = 52;
student1.height = 170;

//access the information about student2
strcpy(student2.name, "Winner2nd");
student2.age = 17;
student2.weight = 56;
student2.height = 170;

//print the information about student 1
printf("Student 1 name: %s\n",student1.name);
printf("Student 1 age: %d\n",student1.age);
printf("Student 1 name: %d kg\n",student1.weight);
printf("Student 1 name: %d cm\n",student1.height);

//print the information about student2
printf("Student 2 name: %s\n",student2.name);
printf("Student 2 age: %d\n",student2.age);
printf("Student 2 name: %d kg\n",student2.weight);
printf("Student 2 name: %d cm\n",student2.height);

return 0;
}

Another example, How to use in function?
#include <stdio.h>
struct Books {
char  title[50];
char  author[50];
char  subject[100];
int   book_id;
};

/* function declaration */
void printBook( struct Books book );

int main( ) {
struct Books Book1;        /* Declare Book1 of type Book */
struct Books Book2;        /* Declare Book2 of type Book */
 
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali"); 
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;

/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
 
/* print Book1 info */
printBook( Book1 );

/* Print Book2 info */
printBook( Book2 );

return 0;
}

void printBook( struct Books book ) {
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
}

How to use structure in pointer?
#include <stdio.h>
struct Books {
char  title[50];
char  author[50];
char  subject[100];
int   book_id;
};

/* function declaration */
void printBook( struct Books *book );

int main( ) {
struct Books Book1;        /* Declare Book1 of type Book */
struct Books Book2;        /* Declare Book2 of type Book */
 
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali"); 
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;

/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
 
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );

/* print Book2 info by passing address of Book2 */
printBook( &Book2 );

return 0;
}

void printBook( struct Books *book ) {
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}

References?
#include <stdio.h>
struct someStruct {
unsigned int total;
};
    
int test(struct someStruct* state) {
state->total = 4;
}
    
int main () {
struct someStruct s;
s.total = 5;
test(&s);
printf("s.total = %d\n", s.total);
}

Senin, 23 November 2015

Pointers and reference in C

Pointers and Reference in C

     Definition of Pointers:
  • is a data type that store the addres of another variable. but you must declare a pointer before you using it to store any variable address.
  • you can declare a pointer using asterisk (*). example: int *a. the asterisk function is used to designate a variable as a pointer.
     Step to use Pointers?
  • create and define a pointer variable. (ex. int *x;).
  • create another variable that you want to store to pointer. (ex. int y = 10;).
  • access the value at the address in the pointer variable. (ex. x = &y).
  • that means that now variable x contains the address of variable y.
     to use pointers in another way?
  • define a variable that contains the address. (ex. int x = 10;)
  • and then create a pointer variable but point them into the variable that contains the address.  (ex. int *y = &x;)
    The example of to use pointers?

     #include <stdio.h>
     int main ()
     {
     int  x = 10;                         // create a variable
     int *y;                                // pointer variable declaration
     y = &x;                              // store address of x into y

     printf("address of x variable: %x\n",x);
     printf("address store of x into y pointer variable: %x\n",y);

     //access the value of the store address into y using pointer.
     printf("the value of y: %d\n",*y);
     
     return 0;
     }

     passing by pointers and the relation with function?
     #include <stdio.h>

     //function declaration
     int passing(int *x) {
     *x = 10;
     }

     int main ()
     {
     int y = 1;                             // x points to y
     passing(&y);                      // pass address of y to x
     printf("&d\n",y);                // now it will print 10 instead of 1
     return 0;
     }

     The relation of pointer with array?
     #include <stdio.h>
     int main ()
     {
     char c[4];
     int i;
     for (i=0;i<4;i++) {
     printf("Address of c[%d]=%x\n",i,&c[i]);
     }
     return 0;
     }

     How to pass pointer in array?
     int arr[5] = {1,2,3,4,5};
     int *x;
     x = &arr[1];                           // now it will point to arr[1]
     *x = 6;                                   //now arr[1] = 2 become x[1] = 6... {1,6,3,4,5}
     x+= 1;                                   //now move x to next address. start from arr[1].
     x[0] = 12;                              //now the result is {1,6,12,4,5}
     x[1] = 13;                              //now the result is {1,6,12,13,5}
     now you can access the data. (ex.you want to access 13, so you can call "arr[3]")

     References?
     references or alias is to make another name of a variable. example :
     int x = 10;
     int &y = x;                              // now x and y is the same variable
     y = 5;                                      //y=5 and the value of x also will be changed. x=5.