#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0

    Pointers / error: assignment of read-only location


    Hi
    Im new to programming in c,
    Im trying to scroll array

    void scroll(const char *M);
    void main()
    {
    const char *a= "Message";
    scroll(a);
    }

    void scroll(const char *M)
    {
    int n=7,i,j;
    char temp;
    for(j=0;j<7;j++)
    {
    temp=M[0];
    for(i=0;i<n-1;i++)
    M[i] = M[i+1];
    M[n-1]=temp;
    printf("%s\n",M);
    }
    }



    I get an error at line M[i]=M[i+1];
    error: assignment of read-only location


    I had no problem when programmed as

    void scroll(const char M[]);
    void main()
    {
    const char a[]= "Message";
    scroll(a);
    }

    void scroll(const char M[])
    {
    int n=7,i,j;
    char temp;
    for(j=0;j<7;j++)
    {
    temp=M[0];
    for(i=0;i<n-1;i++)
    M[i] = M[i+1];
    M[n-1]=temp;
    printf("%s\n",M);
    }
    }


    Please suggest me where I had gone wrong
    as in my project
    my declarations are void scroll(const char *M);
    and input is given as pointer array *a

    suggest what changes i need to make in the program above
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Please use code tags when posting code!

    Please suggest me where I had gone wrong
    Look at this snippet:

    Code:
    void scroll(const char M[])
    {
    ...
       M[i] = M[i+1];
    You told the compiler that M[] can't be changed by using the const qualifier, so why are you trying to change a value, that you told the compiler can't be changed?

    Jim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Thanks Jim,

    I modified removing const

    and it works.

IMN logo majestic logo threadwatch logo seochat tools logo