micro to declare and initialize a struct pointer does not work, why?

K

KOFKS

code like that:

#include <stdlib.h>

#define decPtr(type, name) do { (type) * (name) = malloc(sizeof
(type) ); } while (0);

typedef struct pt
{
int x;
int y;
} pt;

int main( int argc, char *argv[])
{
decPtr(pt, pt0);
return 0;
}

The compiler complains:
pt0 is an identifier has not been declared$B!J(Bsomething means this, I
interprete it from native language other than English$B!K(B

Is my implementation wrong, or this method does not work through?
Anyone help? Thanks a lot~
 
P

Peter Nilsson

KOFKS said:
code like that:

#include <stdlib.h>

#define decPtr(type, name) do { (type) * (name) = malloc(sizeof
(type) ); } while (0);

You can't declare pointers like...

(int) *my_int_ptr;

Even if you remove the parentheses, the do/while block limits
the scope of the declared identifier. You'll simply have a
memory leak.
typedef struct pt
{
int x;
int y;
} pt;

int main( int argc, char *argv[])
{
decPtr(pt, pt0);

C is not C++. Creating a struct pt does not make pt a type
synonym for struct pt.
return 0;
}

The compiler complains:
pt0 is an identifier has not been declared$B!J(Bsomething means
this, I interprete it from native language other than English$B!K(B

It's not a very informative error message. However, the way
you've written your code isn't that common!
Is my implementation wrong, or this method does not work
through?

Even if it worked, why bother using a macro to cloud such a
simple line of code...?

struct pt *pt0 = malloc(sizeof *pt0);
 
K

Kaz Kylheku

code like that:

#include <stdlib.h>

#define decPtr(type, name) do { (type) * (name) = malloc(sizeof
(type) ); } while (0);

Although parentheses may be used in the declarator syntax, for instance:

int (x);

the same is not true of the list of declaration specifiers. There is no
provision for parenthesis in their syntax:

(int) x; /* wrong */

You are falling victim to macro parenthesitis: surrounding the expansion of
every macro parameter with parentheses, just in case they are needed to protect
against some kind of ambiguity, without actually thinking about it. Most C
newbies get this malady in their first year of C life, thereby becoming immune
against a second infection.
typedef struct pt
{
int x;
int y;
} pt;

int main( int argc, char *argv[])
{
decPtr(pt, pt0);
return 0;
}

The compiler complains:
pt0 is an identifier has not been declared(something means this, I
interprete it from native language other than English)

The syntax

(pt) * (pt0) = malloc(...)

is not a declaration at all. It's an assignment expression with a cast on the
left hand side. We can add parentheses to clarify the syntax:

((pt) (*(pt0))) = malloc(...)

It's complete nonsense, which means: dereference the pointer pt0 (which doesn't
exist), cast the result to type pt, and then treat that as the target of an
assignment.
 
G

Guest

code like that:

no don't!
#include <stdlib.h>

#define decPtr(type, name) do { (type) * (name) = malloc(sizeof
(type) ); } while (0);

most of your subsequent problems could have been avoided by *not*
using
a macro in the first place.
 
G

Guest

no don't!



most of your subsequent problems could have been avoided by *not*
using a macro in the first place.

Oh, and the subject line reads:
"micro to declare and initialize a struct pointer does not work, why?"
that should be "macro" not "micro"
 

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
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top