Wednesday, September 10, 2014

Variable Argument Lists in C and C++

If you want to use a function which can take variable number of arguments, you can use variable argument list. For example you want to design a function which accepts any number of integers and returns the average of those integers.You don't know the number of arguments will be passed to the function. There are some library functions also which take variable number of arguments for example printf().

first of all you have to include  <stdarg.h> header file.
Then you have to define va_list type variable which will store the arguments. 

C language
C language
Let's try to understand with example:
#include<stdio.h>
#include<stdarg.h>


techshow irshad(char *arr, ...)
{
int num;
va_list ptr; /* create a new list ptr */
 

va_start(ptr,arr); /* attach ptr with arr */
printf("\n num=%d",arr); /* it will print 1st argument */
 

num = va_arg(ptr,int); /* fetch 2nd argument */
printf("\n num=%d",num);
 

num = va_arg(ptr,int); /* fetch 3rd argument */
printf("\n num=%d",num);


va_end ( arguments ); /* Cleans up the list */
}
int main()
{

printf("\n variable argument:");
techshow(10,20,30,40,50,0);
return 0;
}


/* output */

variable argument:
num=10
num=20
num=30 



You may also like :How to check whether particular bit is on or off in C language?  

Tuesday, September 9, 2014

How to fix: -Grub rescue (simplest solution)

Just before few days ago, my laptop was fallen down. After when I powered on it , it was showing error, and not allow to boot me any of my OS (windows and ubuntu 12.10 I had ).

Error was like this :
                  error:attempt to read or write outside of disk 'hd0'.
                  grub rescue>

I first started searching on google (using another laptop). Got some basic ideas like 
> it's problem with hard disk
> Either hard disk is damaged or corrupted.

First thing, i was looking for my data . I want to backup  my data anyway. 
 > I came to know that we can do it by using : "Live Ubuntu".

I took bootable ubuntu pendrive from friend and started. but it was not showing my hard disk. I tried 2-3 times but same problem . Then I find one option while booting to repair hard disk problem . After that I got all my data , i was looking for.

But still it was not able to boot any of my OS , but now I had no fear to format the disk. I started formatting using Ubuntu but it's not working. Then I tried with windows7 .  I have to format my disk  2-3 times - first C: drive only than all drive but at last have to delete all my partitions. 

Now it is working  fine.