Using const with pointers

Hi guys,

I have problem to with this code I couldn't pass the value and print it out.

this is Original code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# include <iostream>
using namespace std;
	// Attempting to modify data through a
	// nonconstant pointer to constant data.

 void f( const int * ); // prototype

 int main()
{
	int y;


 f( &y ); // f attempts illegal modification
 }//end main

 // xPtr cannot modify the value of constant variable to which it points
 void f(const int *xPtr)
 {

  *xPtr = 100; // error: cannot modify a const object
 } // end function f 



This what I did so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

void f( const int * ); // prototype

int main()
{
   int y;
   
   f( &y); // f attempts illegal modification
   cout << y << endl ;
   system ("pause");
} // end main

// xPtr cannot modify the value of constant variable to which it points
void f( const int *xPtr )
{
	const int y = 100;
    xPtr = &y; // error: cannot modify a const object
} // end function f


Please some help!!!!
I don't understand what you're trying to do.

const by definition means you cannot modify the object... so stop trying to modify it. If you need the function to modify it, then don't use the const keyword.
yes, you are right but I can change the value of the pointer is it?

our instructor said that think in a way to modify the code:

This the places that we need to think of:

f( &y ); // f attempts illegal modification

void f(const int *xPtr)

*xPtr = 100; // error: cannot modify a const object

like if we can create a variable and assign that pointer to the reference and pass it by reference to the main and print it.

closed account (S6k9GNh0)
const on a pointer means you cannot change the pointer or the data it points too. Also, understand that data itself cannot be constant in the way you're thinking of it.

It's a type, a pointer has no notion of what constness its data has unless you specify its constness through what type the pointer is.

1
2
const int * pConstInteger; /* The data must be constant! */
int * pInteger; /* The data must *not* best constant! */


Mind you, modifying const data is NOT to be done. In some cases, this can cause a SEGMENTATION FAULT which is a big no-no.
Last edited on
Do not assign pointers to local variables. They will be deallocated when function ends.

BTW Do you know thay you can make const either pointer or value it points to?
ok.
if it is like this

 
void f (int const *xPtr)


Can we modify it !!
I just figure this out.

Yes :)
This is what I'm talking about guys !!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void f( const int * ); // prototype
int y;
int main()
{
   
   
   f( &y); // f attempts illegal modification
   cout << y << endl ;
   system ("pause");
} // end main

// xPtr cannot modify the value of constant variable to which it points
void f( const int  *xPtr )
{
    y = 99;
    xPtr = &y; // error: cannot modify a const object
} // end function f 


I just want to print 99 in the main.
The out put would be 99
I just create a global variable. so it solve the problem.
closed account (z05DSL3A)
I think you need to explain what you are trying to do a little clearer. I think you have not solved any problem, just flatly ignored it.
Line 19 will not lead to a compiler error. You don't have a constant pointer, but a pointer to a constant object.
I.e. the pointer can be modified, but you cannot modify the object pointed to.

1
2
3
4
5
void f( const int  *xPtr )
{
    y = 99; // just sets the global variable
    xPtr = &y; // this is ok, it just has no effect outside of the function 
}
I think he just want's pass by reference. const has no place it what he's trying to do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

void f(/*const*/ int *); // f is filling in a value, the value pointed to isn't const

int y;

int main()
{
   f( &y);
   cout << y << endl ;
}

void f(/*const*/ int  *xPtr )
{
    y = 99;
    *xPtr = y;
}


And you're right, there's two things that can be const, the pointer itself and the thing pointed to. Here's the syntax:
1
2
3
4
const int* p; // the thing pointed to is const, you can change the value of p
int const *q; // same as p
int * const r; // the pointer is const, but the thing pointed to can change value
int const * const s; // the pointer is const and the thing pointed to is const 
Last edited on
closed account (z05DSL3A)
But his comments on line 3 and 4 are

1
2
	// Attempting to modify data through a
	// nonconstant pointer to constant data. 


I'm just wandering if it is part of his course work to investigate const, maybe given a set piece of code and asked to fill in the function.

--------========#========--------


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Attempting to modify data through a
// nonconstant pointer to constant data.
#include <iostream>

void foo( const int * value_ptr);

int main(void)
{
    int bar = 0;
    const int * bar_ptr = &bar;

    foo(bar_ptr);

    std::cout << bar << std::endl;

}

void foo( const int * value_ptr )
{
    int * evil_ptr = const_cast<int*>(value_ptr);
    *evil_ptr = 10;
} 


If bar in the above was const int instead of just int you would get undefined behavior:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main(void)
{
    const int bar = 3;

    const int * good_ptr = &bar;

    // This would give an error:
    // *good_ptr = 20;

    std::cout << "The address of bar is   : " << &bar     << " and holds the value     :" << bar       << std::endl;
    std::cout << "The value of good_ptr is: " << good_ptr << " and points to the value :" << *good_ptr << std::endl;
    std::cout << std::endl;

    int * evil_ptr = const_cast<int*>(&bar);
    // Undefined behavior
    *evil_ptr = 10; 

    std::cout << "The address of bar is   : " << &bar     << " and holds the value     :" << bar       << std::endl;
    std::cout << "The value of good_ptr is: " << good_ptr << " and points to the value :" << *good_ptr << std::endl;
    std::cout << "The value of evil_ptr is: " << evil_ptr << " and points to the value :" << *evil_ptr << std::endl;
    
    
    return 0;
}
The address of bar is   : 0036FD10 and holds the value     :3
The value of good_ptr is: 0036FD10 and points to the value :3

The address of bar is   : 0036FD10 and holds the value     :3
The value of good_ptr is: 0036FD10 and points to the value :10
The value of evil_ptr is: 0036FD10 and points to the value :10


So general, yes you can cast away const but it is not a very good idea.
Last edited on
gladi, by the time f() exits y is destroyed, so xptr is dangling, and thats why you can't print the value of y.
this should work:

1
2
3
4
5
void f( const int *xPtr )
{
    static const int y = 100;
    xPtr = &y;
}
Last edited on
closed account (z05DSL3A)
bandicoot360 wrote:
by the time f() exits y is destroyed, so xptr is dangling, and thats why you can't print the value of y.
xPtr is also destroyed.

:D yes, I didn't notice, sry
@gladi


I think that you want the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

void f( int * ); // prototype

int main()
{
   int y;
   
   f( &y);
 
   cout << y << endl ;

   system ("pause");
} // end main


void f( int *xPtr )
{
	const int y = 100;
	*xPtr = y; 
} // end function f 
Last edited on
Hi bandicoot360

I tried this but it didn't print the value of Y



gladi, by the time f() exits y is destroyed, so xptr is dangling, and thats why you can't print the value of y.
this should work:





12345
void f( const int *xPtr )
{
static const int y = 100;
xPtr = &y;
}


Hi vlad from moscow

there is no way other than this to print the value of y in the main. How I can pass the value?
You may pass an argument by reference. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

void f( int & ); // prototype

int main()
{
   int y;
   
   f( y);
 
   cout << y << endl ;

   system ("pause");
} // end main


void f( int &x )
{
	const int y = 100;
	x = y; 
} // end function f  
Topic archived. No new replies allowed.