[prev] 73 [next]

Arrays (cont)

Arrays can be created automatically or via malloc()

int main(void)
{
    char str1[9] = "a string";
    char *str2;  // no array object yet

    str2 = malloc(20*sizeof(char));
    strcpy(str2, str1);
    printf("&str1=%p, %s\n", &str1, str1);
    printf("&str2=%p, %s\n", &str2, str2);
    printf("str1=%p, str2=%p\n",str1,str2);
    free(str2);
    return 0;
}

Two separate arrays (different &'s), but have same contents

(except for the unitialised parts of the arrays)