Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const pointer versus const content pointed to by pointer

Reply
Thread Tools

const pointer versus const content pointed to by pointer

 
 
Disc Magnet
Guest
Posts: n/a
 
      05-06-2010
I wrote this code.

disc@magnet:~$ cat const.c
#include <stdio.h>

int main()
{
char s[] = "hello, world\n";
char r[] = "hello, pluto\n";
const char *p = s;
char const *q = r;

p[1] = 'a';
q[1] = 'a';

printf("p: %s\n", p);
printf("q: %s\n", q);
return 0;
}

Got these errors:

disc@magnet:~$ gcc const.c
const.c: In function 'main':
const.c:10: error: assignment of read-only location '*(p + 1u)'
const.c:11: error: assignment of read-only location '*(q + 1u)'

This shows that both syntaxes: const char *p as well as char const *p
makes the content of the array pointed to by p, is constant. However,
what I want is that p itself should be constant. That is, it should be
possible to assign to p only once.

I want a behavior like this:

disc@magnet:~$ cat int.c
#include <stdio.h>
int main()
{
const int p = 10;
p = 20;
return 0;
}

disc@magnet:~$ gcc int.c
int.c: In function 'main':
int.c:5: error: assignment of read-only variable 'p'

where p is a char *. What is the right syntax?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-06-2010
On 05/ 6/10 10:10 PM, Disc Magnet wrote:
> I wrote this code.
>
> disc@magnet:~$ cat const.c
> #include<stdio.h>
>
> int main()
> {
> char s[] = "hello, world\n";
> char r[] = "hello, pluto\n";
> const char *p = s;
> char const *q = r;
>
> p[1] = 'a';
> q[1] = 'a';
>
> printf("p: %s\n", p);
> printf("q: %s\n", q);
> return 0;
> }
>
> Got these errors:
>
> disc@magnet:~$ gcc const.c
> const.c: In function 'main':
> const.c:10: error: assignment of read-only location '*(p + 1u)'
> const.c:11: error: assignment of read-only location '*(q + 1u)'
>
> This shows that both syntaxes: const char *p as well as char const *p
> makes the content of the array pointed to by p, is constant.


No, it doesn't. p is a pointer to const char.

> However,
> what I want is that p itself should be constant. That is, it should be
> possible to assign to p only once.


You want char* const p = s;

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I think argv[i] should give us a pointer value. Why it is given thevalue pointed by pointer tahir rauf C Programming 12 04-29-2011 11:44 PM
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
What version did JFrame.add() get pointed to the content pane? Knute Johnson Java 2 11-10-2007 04:21 AM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57