Clockwise/Spiral Rule: See Also
Clockwise/Spiral Rule: See Also
Clockwise/Spiral Rule: See Also
char * const
and
const char *
LB. asked
12.7k ● 22 ● 61 ● 101 May 20 '09 at 22:16
amiregelz edited
1,808 ● 7 ● 22 ● 36 Nov 27 '12 at 15:46
Add a comment
Report this ad
There is also a
Note:
const char *
and
char const *
char const
over
const char
workmad3 answered
24.2k ● 4 ● 34 ● 56 May 20 '09 at 22:21
edited
Feb 28 '14 at 12:02
1 @supercat
I believe const int *foo,*bar; would declare b
oth foo and bar to be int const *
: Yes.
but int const *foo, *bar would declare foo to
be a int const * and bar to be int *
: No! It would be exactly the same as the previous case.
(See ideone.com/RsaB7n where you get the same error for
both foo and bar).
I think typedef int * intptr; const intptr foo
,bar; would declare both variables to be int *
const
: Yes.
I don't know any way to use a combined declara
tion to create two variables of that type with
out a typedef
: Well, int *const foo, *const bar; . C declarator
syntax... – gx_ Aug 28 '13 at 18:35
3 @supercat (oh, C-only, sorry for the C++ code link, I got
here from a C++ question) It's all about the C declaration
syntax, with a ("pure") type part followed by a declarator. In
" int const *foo, *volatile bar " the type part is
int const (stops before the * ) and the declarators are
*foo (the expression *foo will denote an int const )
and *volatile bar ; reading right-to-left (good rule for
cv-qualifiers), foo is a pointer to a const int, and bar is a
volatile pointer to a const int (the pointer itself is volatile, the
pointed int is [accessed as] const). – gx_ Aug 28 '13 at
21:23
int * mutable_pointer_to_mu
table_int;
int const * mutable_pointer_to_co
nstant_int;
int *const constant_pointer_to_m
utable_int;
int const *const constant_pointer_to_c
onstant_int;
diapir answered
2,543 ● 1 ● 16 ● 24 May 21 '09 at 0:08
This:
See also:
Const in C
edited
Jan 13 '12 at 15:19
AAnkit answered
26.5k ● 10 ● 56 ● 69 Jul 4 '12 at 18:19
Add a comment
and
Javier answered
4,352 ● 6 ● 33 ● 45 May 20 '09 at 22:21
Aadishri answered
1,121 ● 2 ● 16 ● 26 Jan 25 '13 at 9:43
Clockwise/Spiral Rule
A)
B)
char * const a;
chutiya answered
3,013 ● 2 ● 23 ● 45 Apr 14 '17 at 18:01
1 Also known as right-left rule (at least that's how I learnt it):
jdurrett.ba.ttu.edu/3345/handouts/RL-rule.html
– Tomas Pruzina May 9 '18 at 12:05
Add a comment
Yogeesh H T answered
2,251 ● 19 ● 17 Nov 27 '15 at 10:49
edited
Nov 27 '15 at 11:03
Michael answered
52.1k ● 5 ● 113 ● 139 May 20 '09 at 22:21
Your first two are actually the same and your third is a
compiler error :) – workmad3 May 20 '09 at 22:22
Add a comment
/*const char * p;
char * const p;
const char * const p;*/ // these are t
he three conditions,
#include<stdio.h>
/*int main()
Megharaj answered
1,491 ● 2 ● 20 ● 31 Mar 4 '13 at 10:21
int main(void)
{
char ca1[10]= "aaaa"; // char array
1
char ca2[10]= "bbbb"; // char array
2
gopalshankar answered
11 ● 2 Apr 23 '15 at 17:38
Syntax:
#include<stdio.h>
int main(){
int a=10;
int *const ptr=&a;
*ptr=100;/* we can change the value
of object but we cannot point it to an
other variable.suppose another variabl
e int b=20; and ptr=&b; gives you erro
r*/
printf("%d",*ptr);
return 0;
}
Syntax:
#include<stdio.h>
int main(){
int a=10,b=20;
int const *ptr=&a;
printf("%d\n",*ptr);
/* *ptr=100 is not possible i.
e we cannot change the value of the ob
ject pointed by the pointer*/
ptr=&b;
printf("%d",*ptr);
/*we can point it to another ob
ject*/
return 0;
}
Ram edited
3,022 ● 10 ● 38 ● 56 Oct 23 '15 at 14:23
galois answered
747 ● 1 ● 10 ● 24 Jul 21 '16 at 4:31
Add a comment
Two rules
1
1. If const is between char and *, it
will affect the left one.
e.g.
Jishnu V S edited
7,705 ● 7 ● 25 ● 55 Nov 17 '16 at 7:11
For example:
SteliosKts answered
23 ● 1 ● 6 Jan 20 '18 at 15:49
edited
Jan 20 '18 at 23:00
char * const a;
char const * a;
Bonus:
Sany answered
83 ● 7 Nov 29 '19 at 14:46
edited
Nov 29 '19 at 14:52
meta chat tour help blog privacy policy legal contact us cookie
settings full site
2021 Stack Exchange, Inc. user contributions under cc by-sa