943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5381
  • C++ RSS
Sep 23rd, 2004
1

Creating own datatypes

Expand Post »
How do we create datatypes in C++. Eg. creating datatype called "myowndatatype" with sizeof(myowndatatype) = 3
Similar Threads
Reputation Points: 15
Solved Threads: 1
Junior Poster
chound is offline Offline
143 posts
since Aug 2004
Sep 23rd, 2004
1

Re: Creating own datatypes

You can create your own data type by either using a struct or by using a class.

And so you know....
C++ Syntax (Toggle Plain Text)
  1. //This is invalid
  2. sizeof(myowndatatype) = 3; //You can't assign a value to a function...
  3.  
  4. //It should be this for seeing how big it is
  5. int size = 0;
  6. size = sizeof(myowndatatype);

If you need more help on this topic let us know
Reputation Points: 11
Solved Threads: 0
Newbie Poster
C#Coder is offline Offline
19 posts
since Sep 2004
Sep 23rd, 2004
1

Re: Creating own datatypes

Greetings chound,

C provides typedef, a facility for creating new data type names. It makes your name a synonym of the defined data-type:

typedef int Number;
The type Number can be used in declarations and casts in exactly the same ways that the defined type int can be:

C++ Syntax (Toggle Plain Text)
  1. Number i, cars;
  2. Number *blocks[];

Using a synonym for "char *" is similarily declared. Example:

typedef char *Str;

Str s, a[5], *p;

The type being declared in a typedef appears in the position of a variable name, not after the word typedef. The typdef sytax is like the storage classes extern, static, and many others. The typedef declaration does not create a new data-type of any sense. It's simply stated as it adds a new name for some existing type.

typedef is similar to #define, expect that it is interpreted by the compiler. There aren't any new semantics. Variables declared this way have exactly the same properties as variables whose declarations are spelled out explicitly.


Hope this helps,
- Stack Overflow
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Sep 23rd, 2004
0

Re: Creating own datatypes

by sizeof(myowndatatype) = 3 I meant that the size of the datatype should be 3 bytes
Reputation Points: 15
Solved Threads: 1
Junior Poster
chound is offline Offline
143 posts
since Aug 2004
Sep 23rd, 2004
0

Re: Creating own datatypes

C provides a compile-time unary operator called sizeof that can be used to compute the size of any object:

sizeof (type name)

It is impossible to set data to a variable using the sizeof operator, as its only purpose is to tell you the size of your object. Using the following example may help get the size of your object.
typedef int Number;

Number size;

size = sizeof(Number);

Hope this helps,
- Stack Overflow
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Sep 26th, 2004
0

Re: Creating own datatypes

Quote originally posted by chound ...
by sizeof(myowndatatype) = 3 I meant that the size of the datatype should be 3 bytes
So you want to create a custom data type that has a size of 3 bytes? Why do you want/need a datatype of this specific size?

If we know where you're coming from we might be able to better help you find a solution.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
C#Coder is offline Offline
19 posts
since Sep 2004
Sep 26th, 2004
0

Re: Creating own datatypes

This could be 3 bytes:

struct AThreeByteDatatype
{
char s[3];
};

but on Windows the compiler is usually set to an alignment of 2 or 4, meaning that an array of these things will be a multiple of 4 not a multiple of 3.

struct AThreeByteDatatype twoOfThem[2];

sizeof(toOfThem) will likely be 8, not 6.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem getting SDL to work with Dev-C++
Next Thread in C++ Forum Timeline: Error message help?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC