typedef decalrations

A

Andrew Chalk

I can:

typedef char D1;

why can't I:

typedef char[1400] D2;

I.e. declare a typedef corresponding to 1400 bytes.

Thanks!
 
I

Ivan Vecerina

| I can:
|
| typedef char D1;
|
| why can't I:
|
| typedef char[1400] D2;
|
| I.e. declare a typedef corresponding to 1400 bytes.

You can, but you have to write it as follows:
typedef char D2[1400];

Remember that the expression that follows typedef should
always follow the same syntax as a variable declaration.


Note: for quick answers to simple problems, posting
to comp.lang.c++ (without cross-posting to the
*.moderated one) will provide you with faster
responses...

hth
 
R

Russell Hanneken

Andrew Chalk said:
I can:

typedef char D1;

why can't I:

typedef char[1400] D2;

I.e. declare a typedef corresponding to 1400 bytes.

You can, but you have to write it like this:

typedef char D2[1400];

Regards,

Russell Hanneken
(e-mail address removed)
 
A

Attila Feher

Andrew said:
I can:

typedef char D1;

why can't I:

typedef char[1400] D2;

I.e. declare a typedef corresponding to 1400 bytes.

You can:

typedef char D2[1400];

Just like a variable declaration.
 
I

Ivan Vecerina

| I can:
|
| typedef char D1;
|
| why can't I:
|
| typedef char[1400] D2;
|
| I.e. declare a typedef corresponding to 1400 bytes.

You can, but you have to write it as follows:
typedef char D2[1400];

Remember that the expression that follows typedef should
always follow the same syntax as a variable declaration.


hth
 
B

Ben Hutchings

I can:

typedef char D1;

why can't I:

typedef char[1400] D2;

I.e. declare a typedef corresponding to 1400 bytes.

That isn't normal declarator syntax. You wouldn't declare an
object D2 as:

char[1400] D2;

but instead as:

char D2[1400];

With minor exceptions, typedef statements use the same
declarator syntax as object and function declarations, so you
should write:

typedef char D2[1400];
 
D

Dhruv

I can:

typedef char D1;

why can't I:

typedef char[1400] D2;

I.e. declare a typedef corresponding to 1400 bytes.

It can, just place the array thing after D2. So, it would change to
something like:

typedef char D2[1400];

Would make D2 an array of 1400 chars, so using it like this would mean
using an array of chars.

D2 DObj;


Be careful of 1 thing though. If you use new to create a new D2 object,
be sure to delete[] it, and not delete it.


Regards,
-Dhruv.
 
M

Marco Manfredini

Andrew said:
I can:

typedef char D1;

why can't I:

typedef char[1400] D2;

I.e. declare a typedef corresponding to 1400 bytes.

try:
typedef char D2[14000];

Syntactically, a typedef works like a variable declaration, only that
you are declaring a type instead a variable.

Marco
 
D

Dan McLeran

I can:
typedef char D1;

why can't I:

typedef char[1400] D2;

I.e. declare a typedef corresponding to 1400 bytes.

Thanks!

D2 doesn't make much sense. What you're doing is creating an alias for
a type, not assigning a size, i.e. you would write:

char buffer[1400], not char[1400] buffer.

What you can do is something like this:

template<unsigned size>
struct wrapper
{
char buffer[size];
};

typedef wrapper<1400> t2;

int main()
{
return 0;
}
 
F

Francis Glassborow

Dan said:
D2 doesn't make much sense. What you're doing is creating an alias for
a type, not assigning a size, i.e. you would write:

char buffer[1400], not char[1400] buffer.

What you can do is something like this:

template<unsigned size>
struct wrapper
{
char buffer[size];
};

typedef wrapper<1400> t2;

int main()
{
return 0;
}
But why not use the simple answer of writing the original declaration
correctly:

typedef char D2[1400];

? There is no point in reaching for a Swiss-army knife because you hit
your thumb with a hammer instead of the nail.
 
A

Attila Feher

Francis Glassborow wrote:
[SNIP]
But why not use the simple answer of writing the original declaration
correctly:

typedef char D2[1400];

Like I did! I am a good boy.
? There is no point in reaching for a Swiss-army knife because you hit
your thumb with a hammer instead of the nail.

Hm. Why would I want to hit my thumb with a nail?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How Do I Do This (Class Definition)? 8
PWM Issues 0
Saving data 4
CUDA segmentation error I cannot resolve 0
Tic Tac Toe Game 2
Register Question 0
Could you explain this typedef to me? 45
strict-aliasing?? 5

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top