This blog is under construction

Saturday 5 May 2012

va_list, va_start, va_arg, va_end

Header file:
    stdarg.h

Synopsis:
     va_list obj;
     va_start(va_list obj, lastarg);
     type va_arg(va_list obj, type);
     va_end(va_list obj);

Description:
    
va_list variable argument list.  It is used by the macros va_start(), va_arg() and va_end().
va_start It initializes obj for the subsequent call of va_arg() and va_end().  This should be called first.  And the lastarg is the last argument that is present before the variable argument list.
va_arg It returns the next argument.
va_end It is called after processing all the arguments.





Sample program for va_list, va_start, va_arg and va_end in C:



  #include<stdio.h>
  #include<stdarg.h>
  #include<string.h>
  #include<stdlib.h>
  char *res;
  char *str_append(char *format, ...) {
        va_list obj;
        char *str;
        va_start(obj, format);
        str = va_arg(obj, char *);
        strcpy(res, format);
        while (1) {
                strcat(res, str);
                if (strcmp(str, "end") == 0)
                        break;
                str = va_arg(obj, char *);
        }
        va_end(obj);
        return res;
  }

  int main() {
        res = calloc(1, 100);
        res = str_append("hello", "world", "12345", "end");
        printf("Output:%s\n", res);
        return 0;
  }





  Output:
  jp@jp-VirtualBox:~/cpgms/stdarg$ ./a.out
  Output:helloworld12345end


2 comments:

  1. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

    Data Science Interview Questions

    Angular JS Interview Questions

    Big Data Training in Velachery

    AWS Certified Developer

    Best Resource of Learning Devops

    Blueprism Interview Questions

    Automation Anywhere Interview Questions

    ReplyDelete
  2. Do you need Finance? Are you looking for Finance? Are you looking for finance to enlarge your business? We help individuals and companies to obtain finance for business expanding and to setup a new business ranging any amount. Get finance at affordable interest rate of 3%, Do you need this finance for business and to clear your bills? Then send us an email now for more information contact us now via (financialserviceoffer876@gmail.com) whats-App +918929509036 Dr James Eric Finance Pvt Ltd Thanks

    ReplyDelete