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

Recommended Answers

All 8 Replies

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 ;)

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:

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 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

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

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

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.

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.

Ok so i have a question... This is crazy i know but just hear me out.

Ive been trying to learn of how to create datatypes etc. ive also been interested in how microsoft sets up their data types, for instance, do they just set it up with crazy amounts of 'if' statements or is it billions of set values that you can only enter but ive found nothing... anyways to the point sorry. . . Ive been trying to learn how the identifiers like 'long' 'short' 'unsigned' etc work where they go in front of a data type and modify it. The reason for this is ive been wanting to create my own data type "super" that will be placed before a "bool" and modify it in the sense where instead of only 2 values (true/false) i wish to add a third "unknown". I use bools a lot and i wish it had a third value for invalid entries or if the answer to it is truely "unknown" and well its not out there. I know i could just simply set up an int variable and just check if1=true if2=false if3=unknown but i want to go into this field and i would like to know some of the inner workings and not just the long "typical" way to go about it where any beginner could do it (if you know what i mean). If you could help me out that would be awesome thanks!

commented: can you give full code for begeiners because they dont about c++ +0

Dude! This is an EIGHT-YEAR-OLD thread! You should probably start a new thread for your question :0)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.