const pointer versus const content pointed to by pointer

Discussion in 'C Programming' started by Disc Magnet, May 6, 2010.

  1. Disc Magnet

    Disc Magnet Guest

    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?
     
    Disc Magnet, May 6, 2010
    #1
    1. Advertising

  2. Disc Magnet

    Ian Collins Guest

    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
     
    Ian Collins, May 6, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhijit Bhadra
    Replies:
    2
    Views:
    7,479
    Ron Natalie
    Dec 1, 2004
  2. Knute Johnson
    Replies:
    2
    Views:
    445
    Knute Johnson
    Nov 10, 2007
  3. tahir rauf
    Replies:
    12
    Views:
    745
    Keith Thompson
    Apr 30, 2011
  4. Paul Butcher
    Replies:
    12
    Views:
    753
    Gary Wright
    Nov 28, 2007
  5. Victor Reyes
    Replies:
    2
    Views:
    106
    Victor Reyes
    Apr 7, 2008
Loading...

Share This Page