1

This program works perfectly:

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

#define MAX_NUM 2

int tempfunction (char **comments)
{
    char str1[]="First string\n";
    char str2[]="This is the second string\n";

    *(comments+0)=(char *) malloc(strlen(str1)+1);
    *(comments+1)=(char *) malloc(strlen(str2)+1);

    strcpy(*(comments+0), str1);
    strcpy(*(comments+1), str2);
    return 0;
}

int main(void)
{
    char **comments;

    /* This is the section I am talking about */
    comments=(char **) malloc(MAX_NUM*sizeof(char *));
    if (comments==NULL)
    {
        printf("\n### ERROR: malloc failed.\n");
        exit(EXIT_FAILURE);
    }
    /* Upto here............................. */

    tempfunction(comments);
    printf("%s%s", comments[0], comments[1]);
    return 0;
}

But for future convenience I would like to put the malloc section inside the tempfunction. When I do that, I get a segmentation fault error.

I thought it might be due to initialization, so instead of char **comments; I write:

char a = 'a';
char *P = &a;
char **comments = &P;

But, it still doesn't work. I would be very grateful if you could help me understand why this happens and how to fix it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
makhlaghi
  • 3,856
  • 6
  • 27
  • 34
  • 4
    [Don't cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858)! –  Jul 10 '13 at 11:58
  • `char *comments[2] = {NULL,NULL};` will do. – wildplasser Jul 10 '13 at 11:58
  • 1
    http://c-faq.com/malloc/mallocnocast.html – devnull Jul 10 '13 at 12:01
  • H2CO3, devnull: thanks, I understand, but for this particular problem it works perfectly when the section is in main(). So casting is not the problem! – makhlaghi Jul 10 '13 at 12:13
  • wildplasser: thanks, but the problem is that I don't know the number of comments before hand! – makhlaghi Jul 10 '13 at 12:15
  • @astroboy I know it isn't the problem, that's why I wrote a comment and not an answer. However, you should still not do that. –  Jul 11 '13 at 19:32
  • @H2CO3: you are right! I corrected my style, thank you very much ;-). – makhlaghi Jul 12 '13 at 00:01

3 Answers3

2

Try:

int tempfunction (char ***comments)
{
  char str1[]="First string\n";
  char str2[]="This is the second string\n";

  *comments = malloc(MAX_NUM * sizeof(**comments)); /* you must check the return of malloc */

  (*comments)[0] = strdup(str1);
  (*comments)[1] = strdup(str2);

  return 0;
}

and you call it like that:

tempfunction(&comments);

Of course you'll have to free at the end in order to avoid memory leaks

Alexis
  • 2,149
  • 2
  • 25
  • 39
0

If you want to change comments inside a function, you will have to pass address of it, so that its reflected appropriately. So you need to pass &comments ie char *** to tempfunction().

I would suggest to update code as:

int tempfunction (char ***ref_comments)
{
    char str1[]="First string\n";
    char str2[]="This is the second string\n";

    char **comments = malloc(MAX_NUM*sizeof(char *));

    *(comments+0)=(char *) malloc(strlen(str1)+1);
    *(comments+1)=(char *) malloc(strlen(str2)+1);

    strcpy(*(comments+0), str1);
    strcpy(*(comments+1), str2);

    //now update the passed variable
    *nef_comments = comments;
    return 0;
}

int main(void)
{
    char **comments;

    /* This is the section I am talking about */
    comments=(char **) malloc(MAX_NUM*sizeof(char *));
    if (comments==NULL)
    {
        printf("\n### ERROR: malloc failed.\n");
        exit(EXIT_FAILURE);
    }
    /* Upto here............................. */

    // pass address of comments
    tempfunction(&comments);
    printf("%s%s", comments[0], comments[1]);
    return 0;
}
Rohan
  • 52,392
  • 12
  • 90
  • 87
-2

Don't pass char **comments from main function, In stead of doing this, you can declare char **comments inside tempfunction then return reference of comments to main function. It will work.

Jay Shukla
  • 5,794
  • 8
  • 33
  • 63
  • Thanks, but I don't want to return anything (other than a 0 or 1) because there are many more things this function should do. – makhlaghi Jul 10 '13 at 12:08