By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,757 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,757 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Aug 12 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
an*******@gmail.com wrote:
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.

--
Thad
Aug 12 '07 #2

P: n/a

<an*******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
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[i] = &array_of_5_double[i];
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
Aug 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.