Is anyone here familiar with using typedef like this:
What exactly does that do?Code:typedef string type[];
Printable View
Is anyone here familiar with using typedef like this:
What exactly does that do?Code:typedef string type[];
A typedef declaration does not introduce a new type,only a synonym for the type specified.
In your example
Code:typedef string type[];
...
type strArray = { "Hello","World"};
yeah, like Wledge said. it makes 'type' represent a string array. now you dont need all that harsh typing of "string[]" you can just use "type".Quote:
Originally posted by volk
What exactly does that do?
Why wouldn't that typedef be written like this instead:
typedef string[] type;
im not sure if this is true for c++ (ive been programming alot of Java at work lately) but i dont think it matters where the brackets go.Quote:
Originally posted by 7stud
Why wouldn't that typedef be written like this instead:
typedef string[] type;
string [] var; == string var[];
I'm pretty sure that only:
type name[];
is valid in C++.
I thought with a typedef what's on the right is a synonym for what's on the left. So, it seems to me if you declared your typedef like this:
typedef string type[]
then type[] would be the synonym for string, and you would have to declare a string like this:
type[] text;
What's the deal with typedef?
this is the way it's used:
this is C++ - i'm not sure about java...Code:...
typedef char string20[20];
string20 name;
cin.getline(name,20,'\n');
...
Not necessarily, exactly. Its syntax is supposed to resemble that of a variable declaration. Think of these:Quote:
Originally posted by 7stud
I thought with a typedef what's on the right is a synonym for what's on the left. So, it seems to me if you declared your typedef like this:
typedef string type[]
then type[] would be the synonym for string, and you would have to declare a string like this:
type[] text;
What's the deal with typedef?
foo's type is a char[], just like "bar" is a synonym for char[]. So the example works because it's analogous to the way you declare an array of something.Code:char foo[5];
typedef char bar[] // the example in question
I think it *does* matter where the brackets go:
But I have not yet been able to check these.Code:char* foo; // Either the * in front
char bar[5]; // or brackets after
typedef char* foo; // ditto
typedef char foo[];