473,383 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

regarding typedef

Hi,

You all know typedef

typedef struct g
{
int a;
int b;
} google;

typedef google (*google)

google fl1;

fl1->a=10;
fl1->b=20;

see struct google was just a structure and was not a pointer
by using typedef we can change its name.

so after typedef google (*google); , why google variables are acting
like pointers.

Please help me in this.

thanks

lee

Mar 25 '06 #1
14 2220
ya*******@ausi.com opined:
Hi,

You all know typedef

typedef struct g
{
int a;
int b;
} google;

typedef google (*google)

google fl1;

fl1->a=10;
fl1->b=20;

see struct google was just a structure and was not a pointer
by using typedef we can change its name.

so after typedef google (*google); , why google variables are acting
like pointers.


After that, `google` variables can't act like anything, as that line is
illegal. You can't re-use names like that. Try:

typedef struct g
{
int a;
int b;
} goog;

typedef goog* google;

google fl1;

Also, your use of parentheses is misleading. Have a look above, and
into your textbook. It declares a type that is a pointer to a `struct`
of type `goog`. All should be clear now.

--
BR, Vladimir

*** NEWSFLASH ***
Russian tanks steamrolling through New Jersey!!!!
Details at eleven!

Mar 25 '06 #2
On 2006-03-25, Vladimir S. Oka <no****@btopenworld.com> wrote:
ya*******@ausi.com opined:
typedef struct g { int a; int b; } google;
typedef google (*google)
google fl1;
fl1->a=10;
fl1->b=20;

see struct google was just a structure and was not a pointer by using
typedef we can change its name.

so after typedef google (*google); , why google variables are acting
like pointers.
After that, `google` variables can't act like anything, as that line
is illegal. You can't re-use names like that. Try:

typedef struct g
{
int a;
int b;
} * google;

I think is closer to what he intends.
Also, your use of parentheses is misleading.


Why? That's where the parentheses would go, if you were to put them
anywhere.
Mar 25 '06 #3
Jordan Abel opined:
On 2006-03-25, Vladimir S. Oka <no****@btopenworld.com> wrote:
ya*******@ausi.com opined:
typedef struct g { int a; int b; } google;
typedef google (*google)
google fl1;
fl1->a=10;
fl1->b=20;

see struct google was just a structure and was not a pointer by
using typedef we can change its name.

so after typedef google (*google); , why google variables are
acting like pointers.


After that, `google` variables can't act like anything, as that line
is illegal. You can't re-use names like that. Try:

typedef struct g
{
int a;
int b;
}

* google;

I think is closer to what he intends.
Also, your use of parentheses is misleading.


Why? That's where the parentheses would go, if you were to put them
anywhere.


Quite right, on both counts. I blame it on too many googles in the OP.

--
BR, Vladimir

The climate of Bombay is such that its inhabitants have to live
elsewhere.

Mar 25 '06 #4
ya*******@ausi.com wrote:
Hi,

You all know typedef

typedef struct g
{
int a;
int b;
} google;

typedef google (*google)

google fl1;

fl1->a=10;
fl1->b=20;

see struct google was just a structure and was not a pointer
by using typedef we can change its name.

so after typedef google (*google); , why google variables are acting
like pointers.

Please help me in this.


Apart from what the others say also read the following thread:
http://groups.google.com/group/comp....35ae36c1b46ac4

Mar 25 '06 #5
<ya*******@ausi.com> wrote:
You all know typedef

typedef struct g
{
int a;
int b;
} google;

typedef google (*google)

google fl1;

fl1->a=10;
fl1->b=20;

see struct google was just a structure and was not a pointer
by using typedef we can change its name.

so after typedef google (*google); , why google variables are acting
like pointers.

Please help me in this.


You haven't taken the most elementary steps to help yourself.

It is a good idea, particularly when you are in unfamiliar waters, to give
different names to different things. It helps the thinking process
enormously.
Mar 25 '06 #6
Hi,

Following thing is confusing to me..
Its not clear to me..
typedef int (*MyFunctionP)();

typedef struct
{
MyFunctionP myFuncP;
void *mdlDescP;
} neutral;
I am typedefing the int to a pointer to a function.
How can a int is involving with a function..
what is happening here.. please tell me.

thanks
lee.
osmium wrote:
<ya*******@ausi.com> wrote:
You all know typedef

typedef struct g
{
int a;
int b;
} google;

typedef google (*google)

google fl1;

fl1->a=10;
fl1->b=20;

see struct google was just a structure and was not a pointer
by using typedef we can change its name.

so after typedef google (*google); , why google variables are acting
like pointers.

Please help me in this.


You haven't taken the most elementary steps to help yourself.

It is a good idea, particularly when you are in unfamiliar waters, to give
different names to different things. It helps the thinking process
enormously.


Mar 27 '06 #7
ya*******@ausi.com writes:
Following thing is confusing to me..
Its not clear to me..
typedef int (*MyFunctionP)();

typedef struct
{
MyFunctionP myFuncP;
void *mdlDescP;
} neutral;
I am typedefing the int to a pointer to a function.
How can a int is involving with a function..
what is happening here.. please tell me.


Please don't top-post. See <http://www.caliburn.nl/topposting.html>.

The declaration doesn't typedef "int" to anything. It creates a
typedef called "MyFunctionP", which becomes an alias for the type
"int (*)()", or "pointer to function returning int".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 27 '06 #8
On 2006-03-27, ya*******@ausi.com <ya*******@ausi.com> wrote:
Hi,

Following thing is confusing to me..
Its not clear to me..

typedef int (*MyFunctionP)();

typedef struct
{
MyFunctionP myFuncP;
void *mdlDescP;
} neutral;
I am typedefing the int to a pointer to a function.
It looks like that, but actually you're typedeffing MyFunctionP to be a
pointer to a function returning int.
How can a int is involving with a function..
what is happening here.. please tell me.


It's just rather difficult syntax. I've never met anyone who didn't find
function types in C a bit hard to read.

Think about this:

Some people write:

int* p;

which you can read as "p is a pointer to int", as if int* were a type.

Another style is:

int *p;

which you can read as "p is a pointer, and *p is an int".

Well you can read this:

int (*fn)(int) = f;

as "fn is a pointer, and *fn is a function that takes an int and return
an int"

typedefs work just like declarations. Practical advice for function
pointers is always typedef them and if you can't remember the syntax
copy one from somewhere (you'll usually find one in man qsort).

I'd appreciate a better explanation of function type syntax myself. I
think I've just got used to them rather than ever understood them.
Mar 27 '06 #9
In article <sl*********************@bowser.marioworld>
Ben C <sp******@spam.eggs> wrote:
... you can read this:

int (*fn)(int) = f;

as "fn is a pointer, and *fn is a function that takes an int and return
an int". ...
I'd appreciate a better explanation of function type syntax myself. I
think I've just got used to them rather than ever understood them.


The above is exactly the right description.

Dennis has said that the rule for C is "declaration mirrors use".
If one uses (*p)[3] to access one "double" object, then:

double (*p)[N];

is probably the actual declaration (and N is at least 4), giving
p the type "pointer to array N of double". Thus *p has type
"array N of double", and (*p)[3] has type "double".

Of course, (*p) and (p[0]) do the same thing in actual code, so
it is possible to write p[0][3] instead of (*p)[3]. But that
would imply a declaration like:

double p[M][N];

for some integral constants M > 0 and N > 3. Hence, "declaration
mirrors use" only if you happen to use the same format as the
declaration. This also causes confusion with function pointers:

#include <math.h>
...
double result, argument;
double (*fp)(double);

if (use_sine)
fp = sin;
else /* use cosine */
fp = cos;
...
result = (*fp)(argument);

As with arrays and pointers, function names "decay" to pointers to
those functions in most contexts. Here (*fp) "follows the pointer"
in "fp" to the appropriate function (sin or cos), then "un-follows"
it (via "decay") to get the pointer, and only then does the call,
using the pointer. So we can drop the asterisk, and write:

result = fp(argument);

This is the same technique (though a different end-point, as it
were) that we use when we write:

double some_table[SOME_SIZE];
double *dp;
...
dp = some_table;

instead of "dp = &some_table[0]".

Probably the most confusing of all C's type syntaxes is that for
casts (and, correspondingly, unnamed prototype arguments). To form
a proper cast, start by writing a declaration for a variable of
the given type. Let us tackle a tough one: a pointer to a function
that handles a signal. ANSI/ISO C says that a signal function
receives one "int" (the signal number) and returns "void". So the
function itself might look like:

void handle_signal(int sig) {
... some code here ...
}

A pointer to such a function has type:

pointer to function (int) returning void

which is declared as:

void (*fp)(int);

To turn this into a cast, start with the declaration and remove
the name:

void (*)(int)

Then just add parentheses around the whole thing, giving:

(void (*)(int))

Of course, you have to cast some actual value, such as 0. Add
parentheses to that "just in case", and you get a typical definition
for SIG_DFL:

#define SIG_DFL ((void (*)(int))0)

In a function prototype, the arguments can have names, or omit them:

double cos(double argument);
double sin(double);

In an implementation header -- one provided by an implementor -- the
implementor has to be careful not to use a user's name. Imagine
the troubles that occur if the user writes, for instance:

#define i "me, myself"
#include <stdlib.h>

and <stdlib.h> says something like:

int abs(int i);

which then expands to:

int abs(int "me, myself");

Hence, if you look at actual implementor's headers, they tend to use
either:

int abs(int); /* no identifier = no chance for the user to #define it */

or:

int abs(int __i); /* __ prefix is reserved, keep your hands off! */

So consider signal(), again. Its first argument is an "int",
the signal number; its second argument is a pointer to a signal
function.

We already came up with the spelling for the type "pointer to
function taking one int and returning void":

void (*)(int)

So signal's two arguments, using no names, are:

____ int, void (*)(int) ____

(where ____ represents the blanks to fill in later).

However, signal's return value has the same type as its second
argument: "pointer to function taking one int and returning void".
To spell *that*, without using a typedef, we have to declare
signal as:

void (*signal( ____ ))(int);

where ____ represents "the arguments to the actual function" (again,
"blanks to fill in later"). The parentheses around (*signal( ____ ))
are required to force the "*" to bind to the "signal(int)" part,
and not to the "void" part.

Finally, we can drop in the arguments, which we already worked out:

void (*signal(int, void (*)(int)))(int);

and we have a correct prototype for signal(), using no names. If
we are playing implementor -- writing <signal.h> -- and choose to
put in names, we have to use our (implementor) reserved names with
double underscores or similar, and write:

void (*signal(int __sig, void (*__func)(int)))(int);

As an ordinary (non-implementor) C programmer, of course, we would
have to *avoid* the double-underscore names.

This is a lot easier with a typedef, in part because signal's second
parameter -- "pointer to function taking int and returning void"
-- has the *same* type as its return value. So one typedef,
for "pointer to function taking int and returning void", removes
all the hair:

typedef void (*signal_function_pointer)(int);

signal_function_pointer signal(int sig, signal_function_pointer func);

Of course, if we put on our Implementor Hats and write a *new*
<signal.h>, we find that we cannot define the name
"signal_function_pointer". (It is reserved to the user. User gets
the non-"__" names, implementor gets the "__" names, and the two
never collide.) So we either write the hairy version, or typedef
a double-underscore name.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 27 '06 #10
On 2006-03-27, Chris Torek <no****@torek.net> wrote:
In article <sl*********************@bowser.marioworld>
Ben C <sp******@spam.eggs> wrote:
... you can read this:

int (*fn)(int) = f;

as "fn is a pointer, and *fn is a function that takes an int and return
an int". ...
I'd appreciate a better explanation of function type syntax myself. I
think I've just got used to them rather than ever understood them.


The above is exactly the right description.

Dennis has said that the rule for C is "declaration mirrors use".
[...]


Brilliant explanation, thank you very much.
Mar 27 '06 #11
Best explanation :)

Mar 29 '06 #12
Best explanation :)

Mar 29 '06 #13
fr*******@yahoo.com opined:
Best explanation :)


What is? Read:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

--
BR, Vladimir

If Patrick Henry thought that taxation without representation was bad,
he should see how bad it is with representation.

Mar 29 '06 #14
fr*******@yahoo.com wrote:

Best explanation :)


Please read the following before posting any more useless context
free messages.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 29 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DFP | last post by:
I am posting the gcc 3.3.2 compiler error and the source lines below them. Please explain this warning and how to fix it. crt/crtmap.hpp:17: warning: `std::map<K, D, C,...
4
by: Oskars Salnins | last post by:
Hi What does the statement: typedef unsigned my_type_name; mean? Cannot find an answer to this in ordinary books on C. -- Thanks, Osk
8
by: Dew | last post by:
Hi all, I needed a clue to get rid of this problem. Code work fine for few run but then fail with some segmentation fault error with next subsequent run. I am using Visual Age C++ under AIX...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
2
by: toton | last post by:
Hi, This is a silly question related to syntax only. I have a template class with template member function, How to write it separately in a file (not translation unit, just want to separate...
15
by: sam_cit | last post by:
I noticed that what is being done by typedef can very much be done by macro, and i also think there must be a specific reason as to why typedef was introduced, what is the exact difference, and...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.