Monday, July 11, 2011

How to enter String using dynamic array.


//How to enter String using dynamic array.
#include<stdio.h>
#include<stdlib.h>
main()
{
 char **q,*p;
 int *col,n,r,i,j;
 printf("Enter the number of names you want to enter :");
 scanf("%d",&r);
 q=(char **)malloc(r*sizeof(int *));
 col=(int *)malloc(r*sizeof(int));

 for(i=0;i<r;i++)
 {
   printf("Enter length of name you want to save :");
   scanf("%d",&n);
   p=(char *)malloc((n+1)*sizeof(char));
   *(col+i)=n;
   *(q+i)=p;
   printf("Enter the %d name having length %d:",i+1,*(col+i));
   scanf("%s",*(q+i));
 }
 printf("Entered names are :\n");
 for(i=0;i<r;i++)
 {
  printf("%s \n",*(q+i));
 }
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More