Friday, June 28, 2013

Anonymous Unions in C++

Unions in C++ are special type of classes. We can declare member functions and variables with in the union in c++ (all 'C' features of union remain same). 
But in the unions all the data members share the same memory location.

    Union members are public by default as like structures.

     Anonymous unions are the special type unions with the no name and so we can not create object for that union. All the member data in the anonymous unions share same memory location. We can directly access the these members without any dot operator with in the block where these unions declared. 

Monday, May 27, 2013

Qt QML Introduction and Simple Example Program



QML is declarative language used to develop user interface, is a part of the Qt Quick module. Qt Quick consists of set of technologies including Qml, C++ API's for integrating Qml with Qt application, Qt creator IDE, rum time support.
Qt Quick module comes with the Qt with 4.7 or later versions.

Qml we use elements to draw something on the screen(output) like text, image, rectangle etc.Every element in the qml is like a object. We cal also implement javascript code in side the qml file. Item is the base type for the all the elements.

To start a Qml Application we need to import Qt Quick module with version in the beginning of the every qml file, because the elements we are going to use included in this module. For example the following element displat a rectangle.
Import QtQuick 1.0
Rectangle {
    id: rect
    width: 300
    height: 300
    color: "red"
}

Friday, January 25, 2013

What is the sizeof array type when passed as a function argument?

Sizeof() operator is used to find out the no. of memory bytes occupied by any datatype i.e. it will calculate the size of the given datatype. So if we pass a variable called i of type int, it will give output as 4 bytes, or if we pass a array type say arr which contains declaration as int arr[10] , it will give 40 as output.

We generally use sizeof() operator on array type to find out the number of elements an array contain by dividing the sizeof array by oth element of array (or any element).. The following program will illustrate this

#include<stdio.h>
int main()
{
    int arr[10],s;
    s=sizeof(arr)/sizeof(arr[0]);
    printf("sizeof arr %d\n",sizeof(arr));
    printf("no. of elements of arr %d\n",s);
    return 0;
}

Here the problem is what is the size of the array type in function when we pass it as a argument. This is something different because when we pass array as an argument, the array type decays to pointer. So we can't calculate the no.of elements in the function. This is described by the following program.

Monday, December 31, 2012

Which algorithm is efficient for swapping?

Swapping of two numbers/characters can be done by different ways. Generally all people know swapping using a temporary variable. 
In this post i will tell you how to swap two variables in 3 different ways,  and which algorithm is efficient to swap the variables. 

Here all the below examples are shown in C code(executed in linux environment with gcc compiler).

Method 1:
  • Swapping using a temporary variable:

Friday, December 7, 2012

How to write a UDP socket program in Qt


We have seen how to write a simple UDP socket program in C (Linux) in the earlier post..

Now we are going to learn how to write a simple UDP socket program in Qt..

In the following example we used to send and receive data on localhost address.
And the readyRead() signal and slot is used whenever the data receives on the given socket (i.e. socket reday for read data), the control goes to slot method.
writeDatagram() and readDatagram() API's used for sending and receiving data on the given udp socket.

Before starting the Program to write, create a Project using following steps 
  • open Qt creator
  • Create Project
  • Select Other Project in Projects tab then select Qt Console Application, then click on choose...
  • Then give the Project name
  • And then click on Next, Finish in the following dialog boxes
  • Then open the .pro file and write QT += network 
  • To create the cpp file Right click on the project then choose Add new

Monday, November 26, 2012

Linux Shell scripting basics and Simple example to add two numbers

Shell scripting is a file which contains the list of commands for executing. What is Shell? Shell is special program or application provided for the user interaction.
There are different kinds of shells in Unix/Linux, they are
  • Sh: Bourne Shell
  • Csh: C Shell
  • Ksh: Korn Shell
  • Bash: Bourne again Shell.. etc
Some of the advantages of the shell scripting are,
  • Automation of tasks such as backups,log monitoring etc
  • To do repetition of of tasks
  • Save lots of time..
Unix/Linux Shell scripting also containg the functions, control statements,loops like other programming languages C, C++...

Monday, November 12, 2012

Signal handling in Linux using signal() call

signal() function in linux is used to sets of handler for the given signal number, the handler is any of SIG_DFL(default action) or SIG_IGN(ignore signal) or address of user defined function which we called signal handler

syntax:
         sighandler_t signal(int signum, sighandler_t handler);

the second parameter is the address of the signal handler function (function pointer), which has the following syntax

         typedef void (*sighandler_t)(int );

Here the parameter int represents the Signal number.

Here is the simple C program which describes the SIGINT Signal handling in linux

signal.c

#include<stdio.h>
#include<signal.h>

Thursday, November 8, 2012

gethostbyname example in C


gethostbyname() function will return the host details such as hostname, aliase names of the host,type of address, and length of host address of the given hostname.

This function will return the hostent (host entry) structure which contain the all the above details. h_addr_list[0]  field of “hostent” structure will contain struct in_addr equivalent of the host address.


Sysntax

struct hostent *gethostbyname(const char *name);

Here name is any valid hostname  (or) IPV4 address in standard dot notation (or) IPV6  address in colon or dot notation


Here is the simple C Program in Linux that illustrates the gethostbyname() function.