pointers to constant characters and constant pointers to characters

S

sam_cit

Hi Everyone,

I understand the meaning and usage of pointers to constant characters
and constant pointers to characters, however i often get confused with
the syntax of expressing them, can anyone help me out with few examples?
 
R

Richard Heathfield

(e-mail address removed) said:
Hi Everyone,

I understand the meaning and usage of pointers to constant characters
and constant pointers to characters, however i often get confused with
the syntax of expressing them, can anyone help me out with few examples?

char *p - p is a pointer to a char
const char *q - q is a pointer to a const char
char const *r - r is a pointer to a const char (don't ask!)
char * const s - s is a const pointer to a char
const char * const t - t is a const pointer to a const char
char const * const u - u is a const pointer to a const char
 
G

Guest

Hi Everyone,

I understand the meaning and usage of pointers to constant characters
and constant pointers to characters, however i often get confused with
the syntax of expressing them, can anyone help me out with few examples?

For the simple cases, when no parentheses are involved, you can read
declarations from right to left:

const char *p;
/* 1: p is a pointer to char which is const */

char const *p;
/* 2: p is a pointer to const char (same as 1) */

char *const p;
/* 3: p is a const pointer to char */

char const *const p[4];
/* 4: p is an array of 4 const pointers to const char */

When parentheses are involved it gets a bit more complicated, but it
would probably be better to getting to understand the simple cases
first.
 
R

Randy Howard

Richard Heathfield wrote
(in article said:
(e-mail address removed) said:


char *p - p is a pointer to a char
const char *q - q is a pointer to a const char
char const *r - r is a pointer to a const char (don't ask!)
char * const s - s is a const pointer to a char
const char * const t - t is a const pointer to a const char
char const * const u - u is a const pointer to a const char

.... and people keep claiming that C is hard to grasp for the
beginner...

lol
 
K

Keith Thompson

I understand the meaning and usage of pointers to constant characters
and constant pointers to characters, however i often get confused with
the syntax of expressing them, can anyone help me out with few examples?

See if you can find a program called "cdecl".

% cdecl
Type `help' or `?' for help
cdecl> declare a as pointer to char
char *a
cdecl> declare b as const pointer to char
char * const b
cdecl> declare c as pointer to const char
const char *c
cdecl> declare d as const pointer to const char
const char * const d
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top