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 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 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
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.]
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: s |
last post by:
Can I do this:
#define MYSTRING "ABC"
..
..
..
char mychar = MYSTRING;
..
..
..
|
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
|
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 .
|
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...
|
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...
|
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)
...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |