099 指针数组

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

static char words[5][32] = { 
    "Let", "us", "learn", "c", "language"
};

int set_word(char **pstr)
{
    static int wi = 0;
    if (NULL == (*pstr = malloc(strlen(words[wi]))))
        return -1; 
    strcpy(*pstr, words[wi++]);
    return 0;
}

#define ARR_SIZE 5
int main(int argc, char *argv[])
{
    char *ptrarr[ARR_SIZE];
    // set words to pointer array
    for (int i = 0; i < ARR_SIZE; i++)
        set_word(&ptrarr[i]);
    // print words
    for (int i = 0; i < ARR_SIZE; i++)
        printf("%s ", ptrarr[i]);
    printf("\b.\n");
    // compare size
    printf("size of words[5][32]: %ld\n", 
        sizeof(words));
    size_t ptrarr_size = 0;
    ptrarr_size += sizeof(ptrarr);
    for (int i = 0; i < ARR_SIZE; i++)
        ptrarr_size += strlen(ptrarr[i]);
    printf("size of pointer array: %ld\n",
        ptrarr_size);
    // free memory
    for (int i = 0; i < ARR_SIZE; i++)
        free(ptrarr[i]);
    return 0;
}