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

regarding pointers to characters

P: n/a
Hi Everyone,

I have the following two cases,

typedef char* cptr;
const cptr ch;
//here ch is a pointer to a character and the pointer is a constant
here...

where as

#define CPTR char*
const CPTR ch;
//here ch is a pointer to character and the character is a constant
here...

What is the reason behind this???

Dec 17 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sa*****@yahoo.co.in writes:
Hi Everyone,

I have the following two cases,

typedef char* cptr;
const cptr ch;
//here ch is a pointer to a character and the pointer is a constant
here...

where as

#define CPTR char*
const CPTR ch;
//here ch is a pointer to character and the character is a constant
here...

What is the reason behind this???
Is this a homework problem?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 17 '06 #2

P: n/a
>
Is this a homework problem?
No, not at all, homeworks were a long time ago, i know that macro
(string replacement) is before compilation, but i'm not very clear with
typedefs, i thought answer to this might help me understand it better...

Dec 17 '06 #3

P: n/a
sa*****@yahoo.co.in writes:
I have the following two cases,

typedef char* cptr;
const cptr ch;
//here ch is a pointer to a character and the pointer is a constant
here...

where as

#define CPTR char*
const CPTR ch;
//here ch is a pointer to character and the character is a constant
here...

What is the reason behind this???
A typedef creates an alias for a type. Macro expansion works on text
(tokens, actually) with no regard for what those tokens might mean in
context.

In your second example, "CPTR" expands to "char*", so the declaration
is:

const char* ch;

Consider also the following example:

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.