GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Monday, May 30, 2011

Function Pointer As Struct Member

Function pointer as struct member - with arguments:

#include <stdio.h>

struct aritmathic{
    int a;
    int b;
    int (*add)(int, int);
    int (*subtract)(int, int);
    int (*multiply)(int, int);
};

int Add(int a, int b){
    return (a+b);
}

int Subtract(int a, int b){
    return (a-b);
}

int Multiply(int a, int b){
    return (a*b);
}

int main(){

    struct aritmathic ar;
    ar.a = 4;
    ar.b = 5;

    ar.add = Add;
    ar.subtract = Subtract;
    ar.multiply = Multiply;

    int a = ar.add(ar.a, ar.b);
    int b = ar.subtract(ar.a, ar.b);
    int c = ar.multiply(ar.a, ar.b);   

    printf("%d\n%d\n%d", a, b, c);

    return 0;
}


Simpler version for above:
#include <stdio.h>

struct aritmathic{
    int a;
    int b;
    int (*add) (struct aritmathic*);
    int (*subtract) (struct aritmathic*);
    int (*multiply) (struct aritmathic*);
};

int Add(struct aritmathic* ar){
    return (ar->a + ar->b);
}

int Subtract(struct aritmathic* ar){
    return (ar->a - ar->b);
}

int Multiply(struct aritmathic* ar){
    return (ar->a * ar->b);
}

int main(){

    struct aritmathic ar;
    ar.a = 4;
    ar.b = 5;

    ar.add = Add;
    ar.subtract = Subtract;
    ar.multiply = Multiply;

    int a = ar.add(&ar);
    int b = ar.subtract(&ar);
    int c = ar.multiply(&ar);   

    printf("%d\n%d\n%d", a, b, c);

    return 0;
}

Share/Bookmark
Below is a simple example of function pointer as a member of struct:

#include <stdio.h>

struct data{
    void(*hello)();
};

void hello(){
    puts("Hello comes from C");
}

int main(){
    // the first way
    struct data d;
    d.hello = hello;
    d.hello();

    // the alternate way
    struct data dd;
    struct data* ptr = &dd;
    ptr->hello = *hello;
    ptr->hello();
  
    return 0;
}

Share/Bookmark

Friday, May 20, 2011

Better Looking Search Box

<div id="search">
    <form>
         <input type="text" id="text"/>
         <input type="submit" id="submit" value="Search"/>
     </form>
</div>

<style>
#search{
     border: 1px solid #ccc;
     max-width: -moz-fit-content;
     padding: 1px;
     -moz-border-radius: 5px;
}
#text{
     border: 0px solid #ccc;
     -moz-border-radius: 5px 0px 0px 5px;
     padding-left: 10px;
}
#submit{
     background: #88f;
     color: #fff;
     font: 14px arial;
     font-weight: bold;
     border: 0px solid #000;
     padding: 1px 5px;
     -moz-border-radius: 0px 5px 5px 0px;
     cursor: pointer;
}
#submit:hover{
     background: #55f;
}
form{
     margin: 0px;
     padding: 0px;
}
</style>

The result:


Share/Bookmark

Tuesday, May 17, 2011

SSL - Generate RSA Key

To simply generate rsa key to stdout in default 512
OpenSSL> genrsa
For 1024
OpenSSL> genrsa 1024
For saving it to file
OpenSSL> genrsa -out mykey.pem 1024

To generate public RSA key, use
OpenSSL> rsa -in mykey.pem -pubout

Read more on here
Share/Bookmark

OpenSSL Introduction

To encrypt a text file use:
OpenSSL> enc -base64 -in myfile.txt
or
OpenSSL> enc -base64 -in myfile.txt -out myfile.txt.enc
or just a test using pipe
D:\git\bin>echo "Hello World" | openssl enc -base64
To decrypt, use
OpenSSL> enc -base64 -d -in myfile.txt.enc
To decrypt from pipe, use
D:\git\bin>echo "encrypted" | openssl enc -base64 -d
Learn more on here
Share/Bookmark

Sunday, May 15, 2011

YUI - Starting

Introduction

Here's a sample for how to start using YUI framework.
First, linking for the API

Code:

<script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js" charset="utf-8"></script>
Second, make your control

Code:

<input type="text" id="ac-input"/>
Third, write the code

Code:

<script>
YUI.use('autocomplete', function(Y){
        Y.one('#ac-input').plug(Y.Plugin.AutoComplete, {
            source: ['apple','amazon','air','america']
    });
});
</script>


Share/Bookmark

Saturday, May 14, 2011

CSS - Advanced

Introduction

Here's some code that show some advanced of CSS that is practical for use.

Rounded Box

It's easy to make a rounded box. Even you don't need use image for it.

Code

<div>You don't need image for making box looks like rounded</div>

<style>
div{
    border: 1px solid #af0;
    padding: 5px;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    border-radius: 4px;
}
</style>
The result will be:
You don't need image for making box looks like rounded


Share/Bookmark