2

I would like to fill an array of strings using two functions: the first, if I have n strings to allocate, will allocate n memory spaces; the second will allocate memory for each string read

Here is the first function:

char** allocate(int n)
{
    char** t;
    t=(char**)malloc(n*sizeof(char*));
    if(!t) exit(-1);
    return t;
}

Here is the second one:

void fill(char*** t,int n)
    {
        int i;
        char* help=" ";
        for(i=0;i<n;i++)
        {
            printf("\n saisir la chaine n %d :",i+1);
                scanf("%s",help);
                *t[i]=(char*)malloc((strlen(help)+1)*sizeof(char));
                strcpy(*t[i],help);
        }
    }

I did not forget to call the second one in main like this : fill(&t,n);

The problem is that I get an error after reading the first string and program ends.

9
  • 3
    This is not going to work char* help=" "; Commented Dec 17, 2013 at 18:57
  • To expand on that: when you call scanf("%s", help) then help must point to a memory location large enough to hold the string that is being read in. Commented Dec 17, 2013 at 18:59
  • 1
    You do not need to cast the results of malloc in C. Commented Dec 17, 2013 at 19:00
  • @qwrrty These comments are perfectly useful. Commented Dec 17, 2013 at 19:09
  • @qwrrty Um, no. Firstly because it's not a distraction but a piece of good advice, secondly because it's not silly. Commented Dec 17, 2013 at 20:06

3 Answers 3

2

This line

char* help=" ";

just defines a pointer pointing to " ".

There is no memory allocated to then store the data to be scanned in via scanf().

If you have a maximum of character to be scanned in do it as follows:

#define SCAN_MAXIMUM (255)
#define SCAN_FMT_STRINGIFY(max) "%"#max"s"
#define SCAN_FMT(max) SCAN_FMT_STRINGIFY(max)

...

  char help[SCAN_MAXIMUM + 1]; /* Add one for the road^H^H^H^H`0`-terminator. */
  scanf(SCAN_FMT(SCAN_MAXIMUM), help);

Also these lines do not what you want:

    *t[i]=(char*)malloc((strlen(help)+1)*sizeof(char));
    strcpy(*t[i],help);

The [] operator binds tighter then the * operator, so the lines should look like

    (*t)[i] = malloc((strlen(help) + 1));
    strcpy((*t)[i], help);

Also^2: There is no need in C to cast the results of malloc/calloc/realloc, nor is it recommnended.


Also^3: sizeof(char) is defined to be equal 1.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you do not allocate the memory for help variable.

Change char* help=" "; to char help[512]="";

this way help points to a string literal (constant stored in memory block, which is not allowed to be changed.

1 Comment

@user3104033: Be aware that entering more then 511 characters will make the program fail again miserably, as the allocated memory will be overwritten and undefined behaviour will arise,
0

fill() should just take char * *, I think you're getting a operator precedence problem.

Since you don't need to re-allocate the array inside fill(), there's no point to have three levels of indirection. You should change it to just two, and of course call without the &.

2 Comments

As you told me, I took char** and I changed char* help=" "; to char aide[]=" "; Now the programs asks me for all the strings, but the problem now occures after writing the last string with this error : "Stack around the variable 'aide' was corrupted"
If t is declared char **t and he passes the address of this variable as fill (&t, n) then the declaration char ***t was quite correct. I agree that there appears to be no need for the additional level of indirection in the code that has been posted here, though, and if t does not get re-allocated then it makes sense to remove unnecessary levels of redirection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.