By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,810 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,810 IT Pros & Developers. It's quick & easy.

#define a char pointer

P: n/a
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

Jul 24 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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;

--

Frederick Gotham
Jul 24 '06 #2

P: n/a
ma*********@gmail.com wrote:
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;

--
Eric Sosman
es*****@acm-dot-org.invalid
Jul 24 '06 #3

P: n/a
Frederick Gotham wrote:
(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.]

Jul 24 '06 #4

P: n/a
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).

--

Frederick Gotham
Jul 24 '06 #5

P: n/a
On Mon, 24 Jul 2006 14:43:22 GMT, Frederick Gotham
<fg*******@SPAM.comwrote:
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;
Much better, but even better to just use char * :-)

To the OP: Why?

--
Al Balmer
Sun City, AZ
Jul 24 '06 #6

P: n/a
Eric Sosman wrote:
ma*********@gmail.com wrote:
>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
Jul 25 '06 #7

P: n/a
Bart Rider wrote:
Eric Sosman wrote:
>ma*********@gmail.com wrote:
>>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.

--
Eric Sosman
es*****@acm-dot-org.invalid

Jul 25 '06 #8

P: n/a
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;

--

Frederick Gotham
Jul 25 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.