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.
if(!dna1[].strand1){
Does the compiler let you do this? Then later you have this...
cout<<"<"<<sizeof(dna1.strand1)<<">";}
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.
sizeof(dna1.strand1)
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;
}
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
Question Answered as of 6 Months Ago by
phorce
and
VernonDozier