Jump to content
Hash, Inc. - Animation:Master

C++ Week 14 Sizeof and Arrays


robcat2075

Recommended Posts

  • Hash Fellow

It wouldn't be hard to memorize the sizes of standard data types and hard code those when programs needed to use them.

But part of the power of C++ is that you will be able to create your own data types... and make arrays of them, which could be any length.

sizeof() helps you to bullet proof your code so that even if you change the size of your data type or the length of an array, your code can still operate on them without over-running or under-running the total data.

 

project.

Write a program that declares an array of strings without specifying a specific number of elements and initializes the elements with a set of data in that same declaration line.

  • Report the size of the array in bytes, the size of an element in bytes, and the number of elements
  • Print out all the array elements

 

For example if the data in the declaration were: aardvark bat cat dingo eel

The program output would be...
 

Report on Array!

The array is 200 bytes in size.
One element is 40 bytes long.
The array has 5 elements.

0       aardvark
1       bat
2       cat
3       dingo
4       ferret

End of report.

 

If the initialization data in the declaration line were changed to a different number of values:

mockingbird narwhal owl parrot quail serpent tapir

...the program should need no other modification to correctly report...

Report on Array!

The array is 280 bytes in size.
One element is 40 bytes long.
The array has 7 elements.

0       mockingbird
1       narwhal
2       owl
3       parrot
4       quail
5       serpent
6       tapir

End of report.

 

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...