
initializing array of pointers to int
Quote:
> Help! why doesn't this work:
> #include <stdio.h>
> static int *daytab[2] = {
> {0,31,28,31,30,31,30,31,31,30,31,30,55}
> {0,31,29,31,30,31,30,31,31,30,31,30,112} };
Because `int *daytab[2]' defines an array of 2 `int *' pointers, not of
2 `int[13]' arrays. Here is code which does not use pointers:
#include <stdio.h>
static int daytab[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,55},
{0,31,29,31,30,31,30,31,31,30,31,30,112}
Quote:
};
int main()
{
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j <= 12; j++)
printf("ans for i=%d j=%d is %d\n", i, j, daytab[i][j]);
return 0;
Quote:
}
> seems that this only
> works if I initialize and access strings, as type char.
That's right, if you use a quoted string where a `char*' variable is
expected, the string will be allocated elsewhere and a pointer to it is
assigned to the variable. Ie
char *strs[2] = { "foo", "bar" };
does this:
char foo_str[] = "foo", bar_str[] = "bar";
char *strs[2] = { foo_str, bar_str };
and not this:
char strs[2][4] = { "foo", "bar" };
which is the same as this:
char strs[2][4] = { {'f', 'o', 'o', '\0'}, {'b', 'a', 'r', '\0'} };
--
Regards,
Hallvard