Alright guys,
I've been looking at this on google and I can't seem to find an answer. Any help is definitely appreciated!
Here is the code, then I will explain my problem.

 void printMenu()
             cout<<"||=========Main Menu==========||"<<endl;
             cout<<"||===1.Begin DNA Creation";
             cout<<"||===2.Load the Files";
             cout<<"||===3.Save the Files=========||"<<endl; 
             cout<<"||===Enter a Choice <1-4>: ";

Now I'm assuming that my variable dna1 in the structure I created "dna" would behave like any other array and I won't need to pass it over. If this is incorrect then thanks for the heads up.

Basically this function is supposed to print the menu and it will check if the user has ran the function to create the array. If they havent, meaning they just started it up, The desired output is this:

||=========Main Menu============||
||===1.Begin DNA Creation=======||
||===2.Load the DNA Strand<0>===||
||===3.Save the DNA Strand======||
||===Enter a Choice <1-4>:           

And if the first function (option 1) has been run then I would like it to print out the size of it.

Is it not possible to get the size of an array or even check to see if it's initialized?
I know


would run fine and print out 4.
Is there a way to get this to work? If anymore info is needed than let me know; I tried to include everything I thought was needed.

4 Years
Discussion Span
Last Post by Prysm[VA]

Here's the structure data incase it's needed:

struct dna{     
       int location;
       char strand1;
       char strand2;};         

Thank you


Couldn't you just have a count set to = 0 at the beginning and each time a value get's inserted into the array, it increments by 1? Or, if you just need to know whether someone has entered something - Have a Boolean? Here's an example anyway:

#include <iostream>

using namespace std;
int main(int argc, char *argv[]) {

    int numbers[8];
    int count = 0;

    for(int i=0; (i < 3); i++)
        numbers[i] = i+i+2*i;

    cout << count; // prints "3" only 3 elements added to an 8 integer array


Edited by phorce


Yes I could do that. However I want the actual number of bytes it takes up. And taking the number of items in an int array and multiplying by 4 seems tedious so I was hoping there was a legit way. Thank you for your reply, I'm probably going to end up doing it that way unless someone has a solution ;3



I don't think it is possible with Arrays (Someone correct me?) I've only ever seen/used counting the elements as they have been inserted.

Can you not use STL vectors?


You have not posted the declaration of dna1, so it's hard to say exactly where you are going with this. Regardless, having the brackets operator without an array index seems like an error.


Does the compiler let you do this? Then later you have this...


Is dna1 an array or not? Seems like at least one of these lines can't make sense, even if you add an array index. Again, need to see the declaration of dna1 and whether it is an array.


seems like it would be 1, since the size of a character is 1 and strand1 is defined as a character.

I am guessing that dna1 is an array, and a dynamic array at that from your post description. Looking at sizeof(dna1) rather than sizeof(dna1.strand1) might be more in line with what you are looking for, but still won't work, I think. Read on.

If dna1 is a pointer to dynamic array, I don't see how you can use sizeof because you'd be returning the size of the pointer, which on a 32-bit machine would be 4. Not what you are looking for. If dna1 is a STATIC array, you might be in business, but I'm a little confused about what exactly you're doing as far as the counting.

#include <iostream>
#include <cstdlib>
using namespace std;

// total size = 6 on a 32 bit machine, but might be 8 due
// to optimization
struct dna{
       int location; // size = 4
       char strand1; // size = 1
       char strand2;}; // size = 1

int main()
    dna dna1[10];
    dna* dna2 = new dna[10];
    cout << sizeof(dna1) << endl; // prints 60 or 80
    cout << sizeof(dna) << endl; // prints 6 or 8
    cout << (sizeof(dna1) / sizeof(dna)) << endl; // prints 10
    cout << sizeof(dna2) << endl; // prints 4
    delete []dna2;
    return 0;
Votes + Comments
Very informative, Thanks

Thanks everyone.
Vernon, I will definitely check out the pointers, that will probably help me alot. No,


did not work. I apologize for leaving the code like that, I was just very unsure where to go with it.

Phorce, Thank you also! I shall do some research on the STL vectors also =]

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.