By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,967 Members | 2,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,967 IT Pros & Developers. It's quick & easy.

assignment of read-only parameter N

P: 1
Here is all my code but I get a error with N/=10
it says:assignment of read-only parameter N
I don't know how to deal with it

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<math.h>
  3. int number(const int N);
  4. int main()
  5. {
  6.     int n1,n2,i,cnt;
  7.     scanf("%d %d",&n1,&n2);
  8.     cnt=0;
  9.     for(i=n1;i<n2;i++)
  10.     {
  11.         if(number(i))
  12.         cnt++;
  13.     }
  14.     printf("cnt=%d\n",cnt);
  15.     return 0;
  16.  } 
  17.  
  18.  int number(const int N)
  19.  {
  20.      int x,y;
  21.      int num[50]={0};
  22.      int b=10,count=0;
  23.      for(int j=1;;j++)              //计算输入数值的总位数 
  24.      {
  25.          if(N%b!=0) count+=1;
  26.          else break;
  27.          b/=10;
  28.     }
  29.      for(int y=0;y<count;y++)      //将每一位 分别存储在数组中 
  30.      {
  31.          int c=N%b;
  32.          num[y]=c;
  33.          N/=10;
  34.     }
  35.      for(y=0, x=count-1;y<count,x>=0;y++,x--)
  36.      {
  37.          if((num[y]==num[x])&&(sqrt(N)==(int)sqrt(N))) return 1;
  38.          else return 0;
  39.      }
  40.  }
Feb 22 '18 #1
Share this Question
Share on Google+
2 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Your variable N is a const.

A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value.

Therefore, you cannot do this:
Expand|Select|Wrap|Line Numbers
  1. N/=10;
Or this:
Expand|Select|Wrap|Line Numbers
  1. N=N/10;
Because you can never set N after it is initialized.
Feb 22 '18 #2

Expert 100+
P: 2,398
What do you hope to accomplish by declaring int number(const int N)?
C is a call-by-value language. From the callerís point-of-view, its arguments are safe from alteration by number regardless of const in the declaration.

const can be meaningful with pointer arguments but that isnít the case here.
Feb 23 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.