assignment of read-only parameter N

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaotianyu
    New Member
    • Feb 2018
    • 1

    assignment of read-only parameter N

    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

    Code:
    #include<stdio.h>
    #include<math.h>
    int number(const int N);
    int main()
    {
    	int n1,n2,i,cnt;
    	scanf("%d %d",&n1,&n2);
    	cnt=0;
    	for(i=n1;i<n2;i++)
    	{
    		if(number(i))
    		cnt++;
    	}
    	printf("cnt=%d\n",cnt);
    	return 0;
     } 
     
     int number(const int N)
     {
     	int x,y;
     	int num[50]={0};
     	int b=10,count=0;
     	for(int j=1;;j++)              //计算输入数值的总位数 
     	{
     		if(N%b!=0) count+=1;
     		else break;
     		b/=10;
    	}
     	for(int y=0;y<count;y++)      //将每一位 分别存储在数组中 
     	{
     		int c=N%b;
     		num[y]=c;
     		N/=10;
    	}
    	 for(y=0, x=count-1;y<count,x>=0;y++,x--)
    	 {
    	 	if((num[y]==num[x])&&(sqrt(N)==(int)sqrt(N))) return 1;
    	 	else return 0;
    	 }
     }
    Last edited by Frinavale; Feb 22 '18, 04:45 PM.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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:
    Code:
    N/=10;
    Or this:
    Code:
    N=N/10;
    Because you can never set N after it is initialized.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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.

      Comment

      Working...