Title: C strings malloc (output)
Tags: c
Question: I have some strange output here. Could you explain me why and how to solve it?

```int inp_str(char * string, char ** pointers[])
{
    char * tmp[stringsCount];
    if (strlen(string) &gt; maxlen)
    return (-1);
    else {
    tmp[count] = malloc(sizeof(char) * strlen(string));
    strcpy(tmp[count], string);
    pointers[count] = &amp;tmp[count];
    count++;
    }
    return count;

}


int main(){

    //char * strings[stringsCount];
    char ** pointers[stringsCount];
    inp_str( "sdasya", pointers);
    inp_str( "dasd", pointers);
    inp_str( "qwe", pointers);
    inp_str( "dasd", pointers);

    //sort(pointers, count);
    printf("%s", *pointers[0]);
    printf("\n%s", *pointers[1]);
    printf("\n%s", *pointers[2]);
    printf("\n%s", *pointers[3]);
}
```

Here is output:

```sdasya
��uNH��H�l$ H�\$L�d$(L�l$0H��8�f.�
qwe
�bs7
```

PS. stringsCount is constant; count = 0

Comment: Welcome to Stack Overflow. Please read the [About] page soon. You should read up on how to create an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). You code is fairly close to being an SSCE, but it looks like it needs just four more lines of code to be complete: two `#include` lines and the definitions of `stringsCount` and `count`. Including those would save you writing the last PS line.


Here is another answer: Because ```char * tmp[stringsCount];``` is a local variable, after the function ```inp_str``` returns, the memory of ```tmp``` is reclaimed by the system. So the pointers to that location are invalid after the function returns.


Here is another answer: I'm not sure I understand what you're trying to do here. But in any case, there are a few problems:

1) You're copying string into an uninitialized pointer. I.e. you create an array of (char *)'s which point to anywhere and you then copy the string to that location. If you're trying to point tmp to your string, don't use strcpy, just assign via tmp[count]=string;

2) tmp is created on the stack, so if you assign its value to pointers** and try to reference the address outside of the scope of this function, that memory is gone and you will likely see corrupt data.

Hope this helps. Out of curiosity, what are you trying to do in this function?

Comment for this answer: thanks, that helps. Function is for processing of input string: create pointer (in array of pointers for faster sorting), returns pointers/strings count.

Here is another answer: In addition to losing the ```tmp[]``` pointer after the function returns, you're also always allocating one byte short of the actual amount needed: ```strlen(s)``` returns the length of a string, which does not include the terminating NUL byte. What you need is (note that sizeof(char) is 1 by definition):

```  char *p = malloc(strlen(string) + 1);
  strcpy (p, string);
```

to duplicate a string.

Comment for this answer: why not malloc( strlen(string) * sizeof(char) + 1 )?
Comment for this answer: @user2858814 Because as mentioned already, sizeof(char) is 1 by definition. Always. On any compiler. It will never be a different value. Multiplying by 1 is pointless and a tell-tale of not grasping the C type system.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:10/08/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c>', 'Title': 'C strings malloc (output)', 'ViewCount': 128, 'id': 19249734, 'user_AboutMe': None, 'user_AccountId': 3408240.0, 'user_DisplayName': 'user2858814', 'user_Reputation': 11.0, 'user_Views': 1.0, 'user_id': 2858814.0}}