PLEASE..

N

nichas

what does this part of the code mean.. iam not getting it..
............
typedef int RowArray[5];
RowArray *rptr;

...............
i didnt get this.. please reply fast...thank you.
 
C

ciubotariu

int v[5];
would declare an array of integers of length 5 called v.

typedef int RowArray[5];
declares the type of v - array of integers of length 5
 
W

Walter Roberson

int v[5];
would declare an array of integers of length 5 called v.

[this is -not- a flame!]

I would rewrite that to something like,

"would declare an array named v, with 5 elements, each of which is
of type integer."


In English, in the absence of braces or commas to set things off,
adjective and adverbal phrases refer to the nearest prior thing
that they -can- refer to. Thus, in the sentance as written,
the "called v" refers to "5", and "of length 5" refers to "integers".
"5 called v" doesn't usually make much sense, but "integers of length 5"
could plausibly be talking about the number of bytes the integers
each occupy.

(When I first read the sentance, I -did- parse it as the
"length 5" being a modifier to "integers".)
 
N

nichas

typedef struct name Name

it declares the data-type struct name with a short name-------- Name

but here what is this

typedef in RowArray[5] ;
RowArray *rptr;


doing.... i didnt get u ciubota...
 
M

Michael Mair

nichas said:
what does this part of the code mean.. iam not getting it..
...........
typedef int RowArray[5];
RowArray *rptr;

..............
i didnt get this.. please reply fast...thank you.

As mentioned by (e-mail address removed), the first line says
that RowArray from now on means "array of 5 elements of type int".
So, the second line makes rptr a pointer to array of 5 elements of
type int.
If you want, for example, to access the "rows" of an array of array
of 5 chars, you could use rptr to do it:
RowArray *rptr;
size_t index;
int mat[ROW_NUM][5];
init_mat(mat);
for (index = 0, rptr = mat; index < rownum; index++, rptr++)
{
/* Work on the indexth row of mat through rptr */
}
You also could instead declare mat equivalently as
RowArray mat[ROW_NUM];

Now, why would anyone want to do this?
Without the typedef, one would have to declare rptr like
int (*rptr)[5];
which is not exactly straightforward for newbies.

Note: typedef does not really create new types but convenient shortcuts.
New types are defined with struct.


Cheers
Michael
 
C

ciubotariu

A typedef declares a new name for a type.
Both declarations declare type aliases:

/* this one declares Name to be an alias for struct name */
typedef struct name Name;

/* these two are equivalent */
struct name v;
Name v;

/* RowArray stands for the type of integer array of size 5 */
typedef int RowArray[5];

/* these two are equivalent */
int (*rptr)[5];
RowArray *rptr; /* rptr is pointer to integer array of size 5 */
 
S

Servé Laurijssen

/* these two are equivalent */
struct name v;
Name v;

/* RowArray stands for the type of integer array of size 5 */
typedef int RowArray[5];

I think it is a bit weird syntax. Why isn't it
typedef int[5] RowArray;
?
 
M

Michael Mair

Servé Laurijssen said:
/* these two are equivalent */
struct name v;
Name v;

/* RowArray stands for the type of integer array of size 5 */
typedef int RowArray[5];


I think it is a bit weird syntax. Why isn't it
typedef int[5] RowArray;
?

Because typedef is supposed to look like a storage class specifier,
i.e. in order to know how your newly-minted "type" has to be declared
you can think of the declaration of a variable with
static/extern/auto/register storage class:
static int row[5];
-->
typedef int RowArray[5];

Even though this is supposed to be more convenient, it certainly
can be confusing at first.

Cheers
Michael
 
C

ciubotariu

Servé Laurijssen said:
/* these two are equivalent */
struct name v;
Name v;

/* RowArray stands for the type of integer array of size 5 */
typedef int RowArray[5];

I think it is a bit weird syntax. Why isn't it
typedef int[5] RowArray;
?

The typedef parses as a storage specifier such as static, auto, extern
etc
Thus,

typedef int RowArray[5];
extern int RowArray[5];

parse with the same grammar rule. Let other people say why this is so
....
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
what does this part of the code mean.. iam not getting it..
...........
typedef int RowArray[5];

RowArray is typedefed as an alias to the type 'array of 5 int'
RowArray *rptr;

This statement can be interpreted as
"rptr is a pointer to a RowArray".

Since RowArray is a typedef for an 'array of 5 int', this line can also be
interpreted as
"rptr is a pointer to an array of 5 int"



- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCiPztagVFX4UWr64RApqIAJ0TKqN6RcrukB9Nws0FbBJFuhlVgwCcDvW/
kC/X9kIGHXvEfMKAKmrhQuY=
=NP9Z
-----END PGP SIGNATURE-----
 
K

Keith Thompson

Michael Mair said:
Note: typedef does not really create new types but convenient shortcuts.
New types are defined with struct.

Or union, or enum, or [], or *, or (). There are numerous ways to
create new types in C; struct is just the most flexible.
 
M

Michael Mair

Keith said:
Note: typedef does not really create new types but convenient shortcuts.
New types are defined with struct.

Or union, or enum, or [], or *, or (). There are numerous ways to
create new types in C; struct is just the most flexible.

Hmmm, one can look at it like that. I guess I did not express myself
clear enough: I meant new as in "truly new".
For me, [], *, and () do not really create any _new_ type but sort of
perform the "usual derivations".
Enumerations are a convenient way to create identifiers for
constants, nothing more; to be considered truly creating a _new_
type, IMO enums lack a way to run through all enumeration constants
and only through them even for non-contiguous enumerations.
Similarly, unions are nice to save storage by storing logically
equivalent (and mutually exclusive) data together but the semantics
do not give us anything new (but for the common start sequence which
could be used to argue in favour of unions creating _new_ types).

However, this point can be debated (probably fruitlessly)...

Cheers
Michael
 
E

Eric Sosman

Michael said:
Keith said:
[...]

Note: typedef does not really create new types but convenient shortcuts.
New types are defined with struct.

Or union, or enum, or [], or *, or (). There are numerous ways to
create new types in C; struct is just the most flexible.


Hmmm, one can look at it like that. I guess I did not express myself
clear enough: I meant new as in "truly new".
For me, [], *, and () do not really create any _new_ type but sort of
perform the "usual derivations".
Enumerations are a convenient way to create identifiers for
constants, nothing more; [...]

The constant identifiers are not of any new type; they
are all `int'. The `enum foo' type itself, however, is
brand-new and did not exist in the language until the program
declared it.
[...] to be considered truly creating a _new_
type, IMO enums lack a way to run through all enumeration constants
and only through them even for non-contiguous enumerations.

It's true that enum types lack a lot of useful machinery.
However, the particular lack you mention is also shared by
struct types and floating-point types[*] (and arrays and unions
and pointers, but you've already said you don't consider them
"truly new"). And while it's possible to run through all the
values of an integer type, it's not exactly easy to do so while
remaining within the machinery of the type itself (that is,
without resorting to a known-to-be-wider type). This lack of
machinery for enum isn't unique among C's types.

[*] C99 provides nextafter() and nexttoward() and their
variants, which could be used for this purpose. However, they
are not "in the language" in the same way `double' itself is;
note that they need not be present in freestanding environments.
 
K

Keith Thompson

Michael Mair said:
Keith said:
Note: typedef does not really create new types but convenient shortcuts.
New types are defined with struct.
Or union, or enum, or [], or *, or (). There are numerous ways to
create new types in C; struct is just the most flexible.

Hmmm, one can look at it like that. I guess I did not express myself
clear enough: I meant new as in "truly new".
For me, [], *, and () do not really create any _new_ type but sort of
perform the "usual derivations".
Enumerations are a convenient way to create identifiers for
constants, nothing more; to be considered truly creating a _new_
type, IMO enums lack a way to run through all enumeration constants
and only through them even for non-contiguous enumerations.
Similarly, unions are nice to save storage by storing logically
equivalent (and mutually exclusive) data together but the semantics
do not give us anything new (but for the common start sequence which
could be used to argue in favour of unions creating _new_ types).

And a struct "merely" create a collection of components, not really
anything "new". All declared types are derived in some way from other
types. Structs provide a particularly flexible way of creating a new
type, but I don't see that they're fundamentally more "new" than other
types.
However, this point can be debated (probably fruitlessly)...

Probably.
 
M

Michael Mair

Eric said:
Michael said:
Keith said:
[...]

Note: typedef does not really create new types but convenient shortcuts.
New types are defined with struct.

Or union, or enum, or [], or *, or (). There are numerous ways to
create new types in C; struct is just the most flexible.

Hmmm, one can look at it like that. I guess I did not express myself
clear enough: I meant new as in "truly new".
For me, [], *, and () do not really create any _new_ type but sort of
perform the "usual derivations".
Enumerations are a convenient way to create identifiers for
constants, nothing more; [...]

The constant identifiers are not of any new type; they
are all `int'. The `enum foo' type itself, however, is
brand-new and did not exist in the language until the program
declared it.

You are right.
As mentioned above, I mainly use enumerations to obtain distinct
constant identifiers. Very seldom, I use the new type itself.
However, in conjunction with C99's designated initializers, I
at one time or another have wished for a way to run through
the "set" of values belonging to an enumeration type... Then, one
could really use the _type_.

[...] to be considered truly creating a _new_
type, IMO enums lack a way to run through all enumeration constants
and only through them even for non-contiguous enumerations.

It's true that enum types lack a lot of useful machinery.
However, the particular lack you mention is also shared by
struct types and floating-point types[*] (and arrays and unions
and pointers, but you've already said you don't consider them
"truly new"). And while it's possible to run through all the
values of an integer type, it's not exactly easy to do so while
remaining within the machinery of the type itself (that is,
without resorting to a known-to-be-wider type). This lack of
machinery for enum isn't unique among C's types.

Well, up to now I have never had the wish to iterate through
all struct members. However, in the case of
<[email protected]> (compute the offset
of all structure members automatically) this may have been
convenient, so I won't dispute the possible usefulness...
As it is, I quite egocentrically just thought of the possible
and conceivable uses of all the formally new types (for me);
as arrays and pointers fall under "usual derived" types, I will
skip them.
enums basically would be much more useful if they came closer to
the notion of a set and provided some membership relation. As they
don't, they are IMO reduced to mere identifier providers where the
useful identifiers usually do not belong to objects of the type
in question...
With unions, one probably can decide one way or the other.
Seldomly, it _would_ be nice to be able to pick the ith incorporated
member, but this is no criterion against union types as "new" types.
As one could create aggregates similar to structs using arrays of
unions, I probably underestimated unions.

Thinking about it, everything probably comes down to the "flexibility"
Keith mentioned; unfortunately, I lack better words to distinguish
between my notion of "truly new" and formally new.

[*] C99 provides nextafter() and nexttoward() and their
variants, which could be used for this purpose. However, they
are not "in the language" in the same way `double' itself is;
note that they need not be present in freestanding environments.

Yep; wish they would have been there when I was into this kind
of stuff -- then more of my old code would be less ugly...


Cheers
Michael
 
B

Barry Schwarz

what does this part of the code mean.. iam not getting it..
...........
typedef int RowArray[5];
RowArray *rptr;
It is a long winded way of writing
int (*rptr)[5];

This style of typedef is normally useful when the type is more
complicated.


<<Remove the del for email>>
 
R

ranjeet.gupta

int main () {

typedef int array[5] = {1, 2, 3, 5 , 6};
array *ptr;

for (int i = 0; i < 5; ++i) {

printf ("%d", *ptr);
++ptr;
}

return 0;
}

why this is not operating .... as you said it is the pointer to the
array of 5 int.
regrads
 
J

Jens.Toerring

int main () {

Better make that

int main( void )

o tell the compiler that main() does not take any arguments.
typedef int array[5] = {1, 2, 3, 5 , 6};

Here you try to create a typedef for an array of 5 ints. You can't
assign any values in that context.
array *ptr;

'ptr' is a pointer to an array of 5 ints. It's pointing nowhere yet.

What you need is someting like

typedef int array[5];
array p = {1, 2, 3, 5 , 6};
int *ptr;

You also can't have

array *ptr = = {1, 2, 3, 5 , 6};

since here 'ptr' would be a pointer to an array of 5 ints, but on
the right hand side you would have an array and not the address
of such an array. (Except for literal strings there are no anony-
mous arrays in C like you have them e.g. in Perl where you can
say e.g.

my $p = [ 1, 2, 3, 5, 6 ];

and now $p is a pointer (reference) to an array with the specified
elements. And for string literals the support is rather restricted
since you can't change the contents of what the pointer is pointing
to.)

What you could do is

array p = {1, 2, 3, 5 , 6};
array *x = &p;

But you would still not be able to iterate over the elements of 'p'
using 'x' - after incrementing 'x' it would point to the memory
location directly after the 'p' array. Using a variable of such a
type would really make sense only if you had an array of arrays of
5 ints.
for (int i = 0; i < 5; ++i) {

I hope you know that defining new variables at this point is only
allowed in C99 and not all compilers allow that yet (or need to
be called with some special flags to make them operate i C99 mode).
printf ("%d", *ptr);
++ptr;
}
return 0;
}
Regards, Jens
 

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

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top