Pointers

I am starting to learn about pointers and I have to call a function that resets a negative int to zero. I can do it when I pass the pointer as an argument but the instructions say to use the address of an int. Number 11 and 12 is where I am stuck. The instructions are what is in the comments. Some insight in the right direction would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  void noNegatives(int *x);


#include <iostream>
using namespace std;

int main()
{
    // 1. Create two integer variables named x and y.
    int x, y;

    // 2. Create an int pointer named p1.
    int* p1;

    // 3. Store the address of x in p1.
    p1 = &x;

    // 4. Use only p1 (not x) to set the value of x to 99.
    *p1 = 99;

    // 5. Using cout and x (not p1), display the value of x.
    cout << x << endl;

    // 6. Using cout and the pointer p1 (not x), display the value of x.
    cout << *p1 << endl;

    // 7. Store the address of y into p1.
    p1 = &y;

    // 8. Use only p1 (not y) to set the value of y to -300.
    *p1 = -300;

    // 9. Create two new variables: an int named temp, and an int pointer named p2. Make p2
    //    point to x.
    int temp, *p2;

    *p2 = x;
    cout << "\np2 = " << *p2 << ", which is x "<< endl;
    cout << "p1 = " << *p1  << ", which is y " << endl;


    // 10. Use only temp, p1, and p2 (not x or y) to swap the values in x and y. (This will take
    //     a few statements.   Don't use a swap function.)

    temp = *p1;
    *p1 = *p2;
    *p2 = temp;

    cout << "\np2 is now " << *p2 << endl;
    cout << "p1 is now " << *p1 << endl;

    // 11. Write a function with the following signature: void noNegatives(int *x). The function
    //     should accept the address of an int variable. If the value of this integer is
    //     negative then it should set it to zero.
    //     Place the prototype for this function above the main function, and the definition
    //     below main().
    // 12. Invoke the function twice: once with the address of x as the argument, and once with
    //     the address of y.  Use x or y for the argument (not p1 or p2).
    noNegatives(&x);
   // noNegatives(p2);
   // cout << *x << endl;
  //  noNegatives(&y);

// 13. Use p2 to display the values in x and y (this will require both assignment statements
//     and cout statements).  You can use x and y in assignment statements, but not in your
//     cout statement. This should produce the output
//
//     x is: 0
//     y is: 99



// 14. Create an int array named 'a' with two elements. Make p2 point to the first element
//     of a.


// 15. Use only p2 and x (not a) to initialize the first element of a with the value of x.


// 16. Use only p2 and y (not a) to initialize the second element of a with the value of y.
//     Leave p2 pointing to the first element of a. Don't use pointer arithmetic.
//     Hint: don't forget that pointers and arrays are the same thing.


// 17. Using cout and p2 only, display the address of the first element in a.


// 18. Using cout and p2 only, display the address of the second element in a. Leave p2
//     pointing to the first element of a.   Don't use pointer arithmetic.


// 19. Use p1, p2, and temp to swap the values in the two elements of array 'a'.
//     (first point p1 at a[0], then point p2 at a[1], then do not use "a" again. After this
//     the swapping steps should look very similar to step 10. Don't use a swap function.)


// 20. Display the values of the two elements.
//     (The first element should be 99, the second 0).


// 21. Write a function named 'swap' that accepts two pointers to integers as arguments, and
//     then swaps the contents of the two integers. Do not use any reference parameters.
//     Place the function prototype for swap() above the main() function, and place the
//     definition of swap() below main().
// 22. Invoke your swap() function with the addresses of x and y (using the address-of
//     operator in the arguments), then print their values.  (x should be 99, y should be 0).


// 23. Invoke your swap function with the address of the two elements in array 'a', then
//     print their values.  (a[0] should be 0, a[1] should be 99).





} /* end of function main() */




//***********************************************************************************************************
void noNegatives(int *x)
{
    if(*x < 0)
        *x = 0;

    cout << *x << endl;
}
Last edited on
What exactly don't you understand?

Remember in main() x is not a pointer, it is a "normal" instance.

I got it to work by using x = *p2; right before line 59. I don't 100 % confident that this is what the instructions want though.
What do you mean by "got it to work"? What do you expect the value of x to be at that point? Why?
If I cout x right before the function call it's value is 99, and that's because in the beginning I have p1 assigned the address of x and then *p1 = 99 which stores the value of 99 in x by pointing to x's address.

Since the functions job is to to reset any negatives to zero I assigned x the value of *p2 which is -300. Then, if I cout x after this it is zero. This is all new so I am not even entirely sure if I am following the instructions correctly.

Since the functions job is to to reset any negatives to zero I assigned x the value of *p2 which is -300.

Okay but what is the value of y? Isn't part of this section going to call the function with y as well?

Perhaps you just want to print the value of x after the function to demonstrate that the function didn't alter the positive value?

p1 got assigned the value of y's address and pointed to y so it will have -300. Then after the temp swap y became 99.

The function call with y was not active in the post above because I was currently trying to understand the first function call with x.

If I cout x after the function call it has the value of zero. Are you saying that the function should only reset negative values inside the function, and the value of x out side the function should be 99, which was the original value?
Last edited on
Nice job putting the assignment in the comments of the code. This is a very easy and powerful trick to ensure that you do everything that's required.

I think the problem you're having now is line 37:
*p2 = x; // Make p2 point to x.
should be:
p2 = &x; // Make p2 point to x.
Thank you, I came to that realization as well. Now I am on steps 17 and 18 and I am not sure if I am going about it correctly. The instructions say to use cout and p2 only but I am not sure how to do this without using p2 = &x; on #17 and p2 = &x; on #18. Here is my updated code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
int temp, *p2;

	p2 = &x;
	cout << "p2 now has the value of x: " << *p2 << endl;


	// 10. Use only temp, p1, and p2 (not x or y) to swap the values in x and y. (This will take
	//     a few statements.   Don't use a swap function.)

	cout << "Before swap:" << endl;
	cout << "p1 = " << *p1 << " x." << endl;
	cout << "p2 = " << *p2 << " ." << endl;

	temp = *p1;
	*p1 = *p2;
	*p2 = temp;

	cout << "After swap:" << endl;
	cout << "p1 = " << *p1 << " ." << endl;
	cout << "p2 = " << *p2 << " ." << endl;

	// 11. Write a function with the following signature: void noNegatives(int *x). The function
	//     should accept the address of an int variable. If the value of this integer is
	//     negative then it should set it to zero.
	//     Place the prototype for this function above the main function, and the definition
	//     below main().

	// 12. Invoke the function twice: once with the address of x as the argument, and once with
	//     the address of y.  Use x or y for the argument (not p1 or p2).
	noNegatives(&x);
	cout << x << endl;
	noNegatives(&y);
	cout << y << endl;

	// 13. Use p2 to display the values in x and y (this will require both assignment statements
	//     and cout statements).  You can use x and y in assignment statements, but not in your
	//     cout statement. This should produce the output
	//
	//     x is: 0
	//     y is: 99
	p2 = &x;
	cout << " x is: " << *p2 << endl;
	p2 = &y;
	cout << " y is: " << *p2 << endl;


	// 14. Create an int array named 'a' with two elements. Make p2 point to the first element
	//     of a.
	int a[2];

	a[0] = *p2;
	cout << "*p2 @ a[0] is " << a[0] << endl;

	// 15. Use only p2 and x (not a) to initialize the first element of a with the value of x.
	p2 = &x;
	a[0] = *p2;
	cout << a[0] << endl;


        // 16. Use only p2 and y (not a) to initialize the second element of a with the value of y.
	//     Leave p2 pointing to the first element of a. Don't use pointer arithmetic.
	//     Hint: don't forget that pointers and arrays are the same thing.
	p2 = &y;
	a[1] = *p2;


	// 17. Using cout and p2 only, display the address of the first element in a.
	p2 = &x;
	cout << "The address of the first element is " << p2 << endl;

	// 18. Using cout and p2 only, display the address of the second element in a. Leave p2
	//     pointing to the first element of a.   Don't use pointer arithmetic.
	p2 = &y;
	cout << "The address of the second element is " << p2 << endl;

you need to back up a little.

// 15. Use only p2 and x (not a) to initialize the first element of a with the value of x.
p2 = &x;
a[0] = *p2;
cout << a[0] << endl;


try p2 = a;
p2[0] = x;

16 is similar. redo 16
and 17 makes more sense at this point, I hope?
It says not to use "a" though.
Last edited on
Oh I see, that is the initialization part.
This seems to be better. Here is my updated code. I chose to use &a[0] because it helps me to read it's meaning rather than just a.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

// 15. Use only p2 and x (not a) to initialize the first element of a with the value of x.

	//p2 = &x;
	//a[0] = *p2;
	//cout << a[0] << endl;

	p2 = &a[0];
	a[0] = x;
	cout << a[0] << endl;

	// 16. Use only p2 and y (not a) to initialize the second element of a with the value of y.
	//     Leave p2 pointing to the first element of a. Don't use pointer arithmetic.
	//     Hint: don't forget that pointers and arrays are the same thing.

	//p2 = &y;
	//a[1] = *p2;

	p2 = &a[1];
	a[1] = *p2;

	// 17. Using cout and p2 only, display the address of the first element in a.
	//p2 = &x;
	cout << "The address of the first element is " << p2 << endl;

	// 18. Using cout and p2 only, display the address of the second element in a. Leave p2
	//     pointing to the first element of a.   Don't use pointer arithmetic.
	p2 = &a[0];
	cout << "The address of the second element is " << p2 << endl;
Thanks for the help! I was able to complete the rest of the assignment.
I think you still don't quite have it. Starting at step 14:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 14. Create an int array named 'a' with two elements. Make p2 point to the first element
	//     of a.
	int a[2];

	p2 = &a[0];   // or just p2=a;
	cout << "*p2 @ a[0] is " << a[0] << endl;

	// 15. Use only p2 and x (not a) to initialize the first element of a with the value of x.
	p2[0] = x;   // or *p2=x;
	cout << a[0] << endl;


        // 16. Use only p2 and y (not a) to initialize the second element of a with the value of y.
	//     Leave p2 pointing to the first element of a. Don't use pointer arithmetic.
	//     Hint: don't forget that pointers and arrays are the same thing.
	p2[1] = y;

	// 17. Using cout and p2 only, display the address of the first element in a.
	cout << "The address of the first element is " << p2 << endl;
Topic archived. No new replies allowed.