473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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
2 2152
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

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
7
by: Bruce Sam | last post by:
Is this a pointer array of function?If I was wrong,what does it means?
46
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have...
12
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
21
by: arnuld | last post by:
int main() { const char* arr = {"bjarne", "stroustrup", "c++"}; char* parr = &arr; } this gives an error: $ g++ test.cpp test.cpp: In function 'int main()': test.cpp:4: error: cannot...
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
5
by: coldstar | last post by:
In an ASP Querystring, how do I include the double quotes in the code below at "1.0" part as text in my string? I have tried ""1.0"" and almost everything else but can't get it to work. I need...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.