#define a char pointer

M

mailursubbu

Hi,

Will it be possible to #define a char pointer...
It means if write some thing like

#define CHAR_PTR (char *) // I know this wont work


I should be able to use CHAR_PTR to define a variable of type char *.

Best Regards,
Subramanya
 
F

Frederick Gotham

posted:
Hi,

Will it be possible to #define a char pointer...
It means if write some thing like

#define CHAR_PTR (char *) // I know this wont work


I should be able to use CHAR_PTR to define a variable of type char *.


(1) The disgusting option:

#define CHAR_PTR char*


This will break if you use multiple definitions:

CHAR_PTR p1,p2,p3,p4;

~

(2) The beautiful option:

typedef char *charptr;
 
E

Eric Sosman

Hi,

Will it be possible to #define a char pointer...
It means if write some thing like

#define CHAR_PTR (char *) // I know this wont work


I should be able to use CHAR_PTR to define a variable of type char *.

This will work all right for

CHAR_PTR p;
CHAR_PTR q;

.... but will not operate as intended for

CHAR_PTR p, q;
/* equivalent to
char *p, q;
equivalent to
char *p; -- a pointer
char q; -- not a pointer
*/

If you *must* do this, use a typedef:

typedef char *CHAR_PTR;
CHAR_PTR p, q; /* both are pointers */

.... but most people find that using typedefs for "simple"
pointers makes the code harder to read without improving
its flexibility.

If you want to "opacify" the data type, consider hiding
the pointed-at thing rather than the pointer to it:

typedef char CHAR_TYPE;
CHAR_TYPE *p, *q;
 
T

Tom St Denis

Frederick said:
(2) The beautiful option:

typedef char *charptr;

Just an aside... I like how you've progressed in this group. Good
answer and please keep up the helpful replies.

Tom

[No, I'm not being sarcastic. It's rare and nice to see people join a
group and become contributors in short order.]
 
F

Frederick Gotham

Tom St Denis posted:
Just an aside... I like how you've progressed in this group. Good
answer and please keep up the helpful replies.

Tom

[No, I'm not being sarcastic. It's rare and nice to see people join a
group and become contributors in short order.]


Thanks. I wish I could contribute more, but my knowledge of the C Standard is
a bit shotty at the moment. Over on comp.lang.c++, I can give more
authoritative answers, quoting from the C++ Standard and so forth; maybe I'll
become familiar with the C Standard in time (although it's a little wishy-
washy given the C89 Vs C99 situation).
 
A

Al Balmer

posted:



(1) The disgusting option:

#define CHAR_PTR char*


This will break if you use multiple definitions:

CHAR_PTR p1,p2,p3,p4;

~

(2) The beautiful option:

typedef char *charptr;

Much better, but even better to just use char * :)

To the OP: Why?
 
B

Bart Rider

Eric said:
Hi,

Will it be possible to #define a char pointer...
It means if write some thing like

#define CHAR_PTR (char *) // I know this wont work


I should be able to use CHAR_PTR to define a variable of type char *.


This will work all right for

CHAR_PTR p;
CHAR_PTR q;

... but will not operate as intended for

CHAR_PTR p, q;
/* equivalent to
char *p, q;
equivalent to
char *p; -- a pointer
char q; -- not a pointer
*/

[...skip...]

Hmm, the original #define of the OP was:
#define CHAR_PTR (char *)
Now I use this in
CHAR_PTR p, q;
This is equivalent to
(char *) p, q;

Do the parenthesis anything to the type definition?
Or is it compiles as you said (p a pointer to char, q
a char)?

Bart
 
E

Eric Sosman

Bart said:
Eric said:
Hi,

Will it be possible to #define a char pointer...
It means if write some thing like

#define CHAR_PTR (char *) // I know this wont work


I should be able to use CHAR_PTR to define a variable of type char *.



This will work all right for

CHAR_PTR p;
CHAR_PTR q;

... but will not operate as intended for

CHAR_PTR p, q;
/* equivalent to
char *p, q;
equivalent to
char *p; -- a pointer
char q; -- not a pointer
*/

[...skip...]

Hmm, the original #define of the OP was:
#define CHAR_PTR (char *)
Now I use this in
CHAR_PTR p, q;
This is equivalent to
(char *) p, q;

Do the parenthesis anything to the type definition?
Or is it compiles as you said (p a pointer to char, q
a char)?

You're overlooking the "as if" rule, which in this case
is concisely stated "Eric writes `as if' he were right, even
when he's wrong."

In other words: Ooops! You're right, the parentheses are
erroneous. I must have mentally deleted them to make the macro
fit the O.P.'s stated purpose, and then wrote about the still-
in-my-feeble-brain corrected version instead of about the
original. Thanks for pointing out my error.
 
F

Frederick Gotham

Bart Rider posted:
Hmm, the original #define of the OP was:
#define CHAR_PTR (char *)
Now I use this in
CHAR_PTR p, q;
This is equivalent to
(char *) p, q;

Do the parenthesis anything to the type definition?
Or is it compiles as you said (p a pointer to char, q
a char)?


You learn much more by testing these things yourself.

Check what type-mismatch errors you get:

(char*) a, b;

a = 5;
b = 4;
 

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,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top