i want to write a simple programme with the function malloc on a function..
i want to write a function where i will have a table MAX(define the MAX) with strings and read it
later i want to save every string on the table in that way that i don't have loose on my mamory (realloc)
i have write a code but i'm confuse with the 2 dimension table with arrow, can anyone help me....?
thanks
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 3
char *func();
char *func()
{
char *temp[MAX][100],*ptr;
int i,pin2[MAX];
for (i=0;i<MAX;i++)
{
*(temp+1)=malloc(100*sizeof(char));
gets(*(temp+i));
pin2[i]=strlen(*(temp+i))+1;
}
for (i=0;i<MAX;i++)
{
*(temp+i)=realloc(*(temp+i),pin2[i]*sizeof(char));
}
ptr=temp;
return ptr;
}
main()
{
char *str;
int i;
str=func();
for (i=0;i<MAX;i++)
puts(*(str+i));
}