473,385 Members | 1,409 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,385 software developers and data experts.

#define a char pointer

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
8 6692
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
12
by: Riley DeWiley | last post by:
I am looking for a graceful way to declare a string const that is to be visible across many files. If I do this: //----hdr.h const char * sFoo = "foo"; //file.cpp
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
7
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N elements, and every element is a strings tailoring...
10
by: Christian Christmann | last post by:
Hi, what is the difference between a "#define" and typedef? A "#define" is handled by the preprocessor and serves as a name substitude whereas "typedef" is a declaration of a new data type...
29
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) ...
6
by: Cric | last post by:
Hi, how can be the declaration of a pointer which points to dynamic array of pointers and each pointer from this array points to an dynamic array of pointers which points to character strings...
6
by: wongjoekmeu | last post by:
I need to rewrite some typedef to #define. For instance I rewrote typedef void* handle to #define handle void* But I saw for instance another typedef in my code which I don't understand,...
1
by: sophia | last post by:
Dear all, the following are the differences b/w #define and typedef ,which i have seen in Peter van der lindens book. is there any other difference between thes two ? The right way to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.