Creating own datatypes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 140
Reputation: chound is an unknown quantity at this point 
Solved Threads: 1
chound chound is offline Offline
Junior Poster

Creating own datatypes

 
1
  #1
Sep 23rd, 2004
How do we create datatypes in C++. Eg. creating datatype called "myowndatatype" with sizeof(myowndatatype) = 3
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 19
Reputation: C#Coder is an unknown quantity at this point 
Solved Threads: 0
C#Coder C#Coder is offline Offline
Newbie Poster

Re: Creating own datatypes

 
1
  #2
Sep 23rd, 2004
You can create your own data type by either using a struct or by using a class.

And so you know....
  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
If you don't know it, learn it ;)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Creating own datatypes

 
1
  #3
Sep 23rd, 2004
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:

  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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 140
Reputation: chound is an unknown quantity at this point 
Solved Threads: 1
chound chound is offline Offline
Junior Poster

Re: Creating own datatypes

 
0
  #4
Sep 23rd, 2004
by sizeof(myowndatatype) = 3 I meant that the size of the datatype should be 3 bytes
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Creating own datatypes

 
0
  #5
Sep 23rd, 2004
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 19
Reputation: C#Coder is an unknown quantity at this point 
Solved Threads: 0
C#Coder C#Coder is offline Offline
Newbie Poster

Re: Creating own datatypes

 
0
  #6
Sep 26th, 2004
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.
If you don't know it, learn it ;)
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Creating own datatypes

 
0
  #7
Sep 26th, 2004
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC