It seems I'm a bit confused here. As with the use of a pointer to an array of pointers. if I could fetch more info regarding the error, I would have. All I received is a code for a segmentation fault.
a program to find the longest input string.
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main()
{
char **array;
int i, n, *L, maxlen;
printf("Enter the number of strings that you wish to store : ");
scanf("%d", &n);
array=malloc(n*sizeof(char*)); //acquire storage for an array of pointes of size n//
L=malloc(sizeof(int)*n);// for the length array//
for(i=0;i<n;i++)
{
printf("Enter string %d : ", i+1); //sometimes, prints upto this//
gets(array[i]); //sometimes skips the first string input and jumps to the second and stops at the third//
L[i]=strlength(array[i]);
}
maxlen=0;
for(i=0;i<n;i++)
{
if(L[i]>maxlen)
maxlen=L[i];
}
printf("The string(s) with the maximum length with length %d are : ", maxlen);
for(i=0;i<n;i++)
{
if(L[i]==maxlen)
{
printf("\n%s.", array[i]);
}
}
return 0;
}
int strlength(char *array)
{
int j=0;
while(array[j]!='\0')
{
j++;
}
return j;
}
getseven says "Never use gets". Take its advice and usefgets.