954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating own datatypes

How do we create datatypes in C++. Eg. creating datatype called "myowndatatype" with sizeof(myowndatatype) = 3

chound
Junior Poster
145 posts since Aug 2004
Reputation Points: 15
Solved Threads: 1
 

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

And so you know....

//This is invalid
sizeof(myowndatatype) = 3;  //You can't assign a value to a function...

//It should be this for seeing how big it is
int size = 0;
size = sizeof(myowndatatype);


If you need more help on this topic let us know ;)

C#Coder
Newbie Poster
19 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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 typeint can be:

Number i, cars;
Number *blocks[];


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

typedef char *Str;

Str s, a[5], *p;


The type being declared in atypedef 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

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 

by sizeof(myowndatatype) = 3 I meant that the size of the datatype should be 3 bytes

chound
Junior Poster
145 posts since Aug 2004
Reputation Points: 15
Solved Threads: 1
 

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

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 
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.

C#Coder
Newbie Poster
19 posts since Sep 2004
Reputation Points: 11
Solved Threads: 0
 

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.

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You