memory allocation for array of pointers

前端 未结 4 799
梦如初夏
梦如初夏 2021-02-10 01:45

I need to allocate memory for a pointer which needs to be used as a 2d array.I know how to allocate memory for char pointers and int pointers I am confused how memory is allocat

4条回答
  •  伪装坚强ぢ
    2021-02-10 02:35

    What you're doing is allocating space for 5 chars. You could write this and it'll have the same result:

    char *names = (char *)malloc(sizeof(char) * 5);
    

    If you want to have an array of pointers, I think this'd be the best code

    char **names = (char **)malloc(sizeof(char *) * 5);
    

    I'm not a super-coder, but as what I now, this is the right solution.

提交回复
热议问题