Check the validity of a date supplied from the command line in C

#include <stdio.h>   void convert(char **,int*,int*,int*); int valid(int,int,int);   int main(int argc,char *argv[]) { int d,m,y char *progname=argv[0];   if(–argc>0) { convert(++argv,&d,&m,&y); printf("The date %s is %sn",*argv, (valid(d,m,y))?"validn": "invalidn"); } else printf("Usage: %s dd-mm-yyyyn",progname); return 0; } void convert(char **ptrarr,int *pd,int *pm,int *py) { char *curptr,c; int n;   /*Points to the date string */ […]
Continue reading…

Enjoyed this post? Share it!

 
 

Program that prints the first characters of all the arguments except for the first

#include <stdio.h>   void flush_array(int,char *[]); void flush_1st_char(int,char *[]);   int main(int argc,char *argv[]) { flush_array(argc,argv); printf("n=======> "); flush_1st_char(argc,argv); return 0; }   void flush_array(int n,char *ptrarr[]) { while(–n > 0) printf("%s%c",*++ptrarr,(n>0)?’ ‘:’n’); }   void flush_1st_char(int n,char *ptrarr[]) { while(–n > 0) printf("%c",(*++ptrarr)[0]); printf("n"); }
Continue reading…

Enjoyed this post? Share it!

 
 

A program using pointers to functions in C

#include <stdio.h>   int main() { int x,y,operation; double result, operate(double (*)(),int,int); double add(), subtract(), multiply(), divide();   printf("Enter two integers : "); scanf("%d %d",&x,&y);   /*Throw away extra character in keyboard buffer*/ operation=getchar();   printf("Enter an operation (+,-,*,/) : "); operation=getchar();   switch(operation) { case ‘+’: result=operate(add,x,y); break; case ‘-‘: result=operate(subtract,x,y); break; case ‘*’: […]
Continue reading…

Enjoyed this post? Share it!

 
 

Display the first characters of the strings pointed by the array elements of an array of pointers

  #include <stdio.h>   int main() {   /*Define and Initialize the 2-D array*/ char *direction[]={ "North", "East", "West", "South", };   int i; const int SIZE = (int) sizeof(direction)/sizeof(direction[0]);   /*Print*/ for(i=0;i<SIZE;i++) printf("%c",direction[i][0]); return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Use pointers to display the strings pointed by the array elements of an array of pointers

/*Program using pointers to display the strings pointed by the array el- ements of an array of pointers*/   #include <stdio.h>   int main() { /*Define and Initialize the 2-D array*/ char *direction[]={ "North", "East", "West", "South", };   int i; const int SIZE = (int) sizeof(direction)/sizeof(direction[0]);   /*Print*/ for(i=0;i<SIZE;i++) printf("%s%c",direction[i],(i<SIZE-1)? ‘ ‘ : ‘n’); […]
Continue reading…

Enjoyed this post? Share it!

 
 

Print each element of the array together with their memory addresses

#include <stdio.h> const int MAX = 6; const int LEN = 10;   int main() { int i,j;   /*Define and Initialize the 2-D array*/ char language[MAX][LEN]={ "FORTRAN", "BASIC", "COBOL", "Pascal", "C", "Ada" }; /*Print*/ for(i=0;i<MAX;i++) { printf("n%pt",&language[i][0]); for(j=0;language[i][j];j++) printf("%c",language[i][j]); } return 0; }
Continue reading…

Enjoyed this post? Share it!

 
 

Function that returns the average of all the array elements except for the final element

/* (Version 1) The array version of the function */   float average(int m[]){ int total=0,i; float avg; for(i=0;m[i];i++) total+=m[i]; avg=(float)(total)/i; return avg; } /* (Version 2) The pointer version of the function */   float average(int *pm){ int total=0,*pt=pm; float avg; while(*pt){ total+=*pt; pt++; } avg=(float)(total)/(pt-pm); return avg; }
Continue reading…

Enjoyed this post? Share it!