how does one write: sizeof(array of 5 "pointers to double")

A

anon.asdf

Hi!

Q. 1)
How does one write:
sizeof(array of 5 "pointers to double")
???

I know that
sizeof(pointer to an array of 5 doubles) can be written as:
sizeof(double (*)[5]);

Q. 2)

Why does this work:
int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
double *X[5] = \
{&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
return 0;
}

but this NOT WORK
int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
double *X[5];
X = \
{&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
return 0;
}
????

How can the bottom code snippet be written?

Thanks. -anon.asdf
 
T

Thad Smith

Q. 1)
How does one write:
sizeof(array of 5 "pointers to double")
???

sizeof (double* [5])
Q. 2)

Why does this work:
int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
double *X[5] = \
{&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
return 0;
}

but this NOT WORK
int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
double *X[5];
X = \
{&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
return 0;
}
????

Because arrays cannot be assigned. The first example was an array
initialization.
How can the bottom code snippet be written?

Like the top one. ;-)
To set members of an existing array, use an assignment for each array
element or copy an existing array with memcpy(), etc.
 
M

Mike Wahler

Hi!

Q. 1)
How does one write:
sizeof(array of 5 "pointers to double")
???

sizeof(double *[5]);

However, it's normally preferred to use an object
rather than a type as the argument to 'sizeof'.
Then if you later change the array's size (and/or
element type), 'sizeof' will still report the correct
value. E.g.

double *array[5];
sizeof array;
I know that
sizeof(pointer to an array of 5 doubles) can be written as:
sizeof(double (*)[5]);

Yes. But again, I recommend using the object name rather
than a type name.
Q. 2)

Why does this work:
int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
double *X[5] = \

This is an array of five pointers to double.
{&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};

... and you've initialized each pointer to each of the
addresses of the doubles in the array.
return 0;
}

but this NOT WORK
int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
double *X[5];
X = \
{&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
return 0;
}
????

It "doesn't work" because assignment to an array is not allowed.
With assignment, each array element must be assigned individually.
How can the bottom code snippet be written?

double *X[5];
size_t i;
for(i = 0; i < sizeof X / sizeof *X; ++i)
X = &array_of_5_double;


But why not just use initialization as in your first example?


int i = 0; /* initialization */
int j;
j = 0; /* assignment */

Initialization and assigment are not the same thing.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top