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));
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 3

char **func();
char **func()
{

    char **temp,**ptr;
    int i,pin2[MAX];

    for (i=0;i<MAX;i++)
    {
        *(temp+sizeof(char)*100*i)=malloc(100*sizeof(char));
        gets(*(temp+sizeof(char)*100*i));
        pin2[i]=strlen(*(temp+sizeof(char)*100*i))+1;
    }

    for (i=0;i<MAX;i++)
    {
        *(temp+sizeof(char)*100*i)=realloc(*(temp+sizeof(char)*100*i),pin2[i]*sizeof(char));

    }
    ptr=temp;
    return ptr;


}


main()
{
    char **str;
    int i,j;

    str=func();
/*
    for (i=0;i<MAX;i++)
    puts(str[i]);
*/
    for (i=0;i<MAX;i++)
    {
        for (j=0;j<5;j++)
        {
            printf("%c",str[i][j]);
        }
    }

}

this one is better but with still errors

You're vastly overcomplicating trivial stuff and ignoring important stuff. Compare and contrast your code with this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 3
#define LEN 100

char **func(void)
{
    char **ptr = malloc(MAX * sizeof *ptr);

    /* Always check malloc for failure */
    if (ptr) {
        size_t i;

        for (i = 0; i < MAX; i++) {
            ptr[i] = malloc(LEN * sizeof *ptr[i]);

            /* Leave a null pointer if malloc fails */
            if (ptr[i]) {
                /* Always check input for failure too */
                if (fgets(ptr[i], LEN, stdin)) {
                    /* Try to shrink wrap the memory to the string length */
                    char *temp = realloc(ptr[i], strlen(ptr[i]) + 1);

                    /* Take care not to lose the original pointer on failure */
                    if (temp)
                        ptr[i] = temp;
                }
            }
        }
    }

    return ptr;
}

int main(void)
{
    char **str = func();
    size_t i;

    for (i = 0; i < MAX; i++) {
        if (str[i])
            puts(str[i]);
    }

    /* Don't forget to release your allocated memory */
    for (i = 0; i < MAX; ++i)
        free(str[i]);
    free(str);

    return 0;
}

On top of actually working, my code also checks for error cases and handles them gracefully. Your code would probably crash and burn under those same cases.

This article has been dead for over six months. Start a new discussion instead.