Q 1) In this small snippet

#include <stdio.h>   

void passingAddressOfConstants(const int* num1 , int* num2)
{
    *num1 = num2;

}

int main(){

    const int limit = 100;
    int result = 5;

    passingAddressOfConstants(&limit,&limit);

}

Error : error: invalid conversion from 'const int*' to 'int*'  

Here I defined "limit" is of type const int but why the error specifies conversion from const int* to int*.
Can someone tell me the reason behind this?

Q 2) Can someone help me to get an example for the below statement

We cannot pass the address of an integer constant to a pointer 
to a constant , as this would allow the a constant value to be 
modified 

Recommended Answers

All 5 Replies

likely because you are passing limit twice, once as a const int, and then trying to cast it as an int when it is clearly a const int.

My C++ is not the best, but Im pretty sure you can't do what you are doing :-/ You stronly type for a reason, and it's not so you can not use it.

Your Q2 will come when you fix your code in Q1.

Or in simpler terms, const means constant, it cannot be changed.

Also this:

num1 is a pointer-to-an-int, so *num1 is an int.

num2 is a pointer-to-an-int.

So *num1 = num2; has an int on the left, and a pointer-to-an-int on the right. It makes no sense. You're trying to assign a pointer (on the right) to an int (on the left).

The problem here is (a) the typo error with write *num1= num2; rather than *num1=*num2; (which doesn't actually affect the problem). And (b) what you are defining by const.

Let us start by looking at the definition of limit:

  const int limit=100;

That says limit is 100, and the compile [except in very special circumstances] will try to keep limit equal to 100 at all times, so you, the programmer have less difficulty in following your own (or someone elses program).

Then you write this:

 void passingAddressOfConstants(const int* num1 , int* num2)
   {
      // Stuff [doesn't matter what]
   }

This is ok BUT you have told the compiler that the address pointed
to by the variable num1 will be constant. Thus calling it with
passingAddressOfConstants(&limit,&limit) is 100% not ok, because you are allowed to change the second variable, which implies that limit will be changed.

However, the function is also illegal (simplified to):

 void passingAddressOfConstants(const int* num1 , int* num2)
   {
      *num1=20;   // compile error
   }

you promissed that the value pointed to by num1 will not change, and then you try to change it!

However, even if it is not possible to guess what is going to happen at run time this is legal:

 void passingAddressOfConstants(const int* num1 , int* num2)
   {
      int sum(0);
      std::cout<<"ADDRESS of num1 : "<<num1<<std::endl;
      for(int i=0;i<10;i++)
         sum+= *num1++
      std::cout<<"ADDRESS of num1 "<<num1<<std::endl;
      std::cout<<"Sum  : "<<sum<<std::endl;
   }

Don't expect a pretty number from sum.

Now you can have BOTH features [not ability to change the value, and no ability to change the address] e.g. you can write this :

void passingAddressOfConstants(const int* const num1 , int* num2)
   {
       *num1=1726;         // this is a compiler error
       int sum= *num1;     // this is ok
       num1++;             // this is a compiler error
       return;
   }

So your function is incorrect, it doesn't matter how you call it, or with what values, you promised the compiler that you would not change the value pointed to by num1 but you are trying to do so. Hence the compiler error.

The main reason for this to be in code is because it is SO easy to increment a pointer when you mean to increment the value, or to miss the * e.g.

     num1= 10;
     *num1=10;    

Simple typo-error , in on case num1 is set to point to memory location 10 and the other the memory location it points to is set to 10.

Please use const whenever you can, it will save masses of debugging time, even if it takes slightly longer to get a version compiled.

you cannot assign a value to a const variable except at the time of decleration

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.