# Using Pointers

Page 1 of 1

## 4 Replies - 3278 Views - Last Post: 23 March 2010 - 04:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=163647&amp;s=1668239f640435b4cef2dcdb61948829&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 marcelomg

Reputation: 0
• Posts: 21
• Joined: 10-January 09

# Using Pointers

Posted 22 March 2010 - 07:05 PM

I have this code that finds the smallest number in an array:
```#include <iostream>
using namespace std;

int main()
{
int i;
int smallest;
int nums[5] = {4, 20, 18, 7, 2};

smallest = nums[0];
for (i = 1; i < 5; i++) {
if (nums[i] < smallest)
smallest = nums[i];
}
cout << smallest;

return 0;
}

```

I'm trying to do the same code but with pointers instead, but something is wrong, I don't know what it is.
Can someone help?
Thanks,

Here it is:
```#include <iostream>
using namespace std;

int main()
{

int* pInt;
int* pSmallest;
int nums[5] = {4, 20, 18, 7, 2};

pSmallest = nums;
for (*pInt = 1; *pInt < 5; pInt++) {
if (nums[pInt] < *pSmallest)
pSmallest = pInt;
}
cout << pSmallest;

return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Using Pointers

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Using Pointers

Posted 22 March 2010 - 07:14 PM

There's several things amiss, all of which indicate a lack of pointer basics.

how much do you know about them?

```int main(){
int* pInt;
int* pSmallest;
int nums[5] = {4, 20, 18, 7, 2};

pSmallest = nums;
for (*pInt = 1; *pInt < 5; (*pInt)++) { //can't use a pointer like this, you didn't allocate any space
if (nums[*pInt] < *pSmallest) *pSmallest = nums[*pInt]; //if you did allocate space for it, need to derefernece to get integer value
}
cout << *pSmallest << endl; //dereference to get actual value pointed at

return 0;
}

```

A possible right way:

```int main(){
int* pSmallest;
int nums[5] = {4, 20, 18, 7, 2};

pSmallest = nums; //or &nums[0], same thing
for (int i = 0; i < 5; i++) { //no need for a pointer
if (nums[i] < *pSmallest) *pSmallest = nums[i]; //don't forget to derefence!
}
cout << *pSmallest << endl; //dereference to get actual value pointed at

return 0;
}

```

### #3 marcelomg

Reputation: 0
• Posts: 21
• Joined: 10-January 09

## Re: Using Pointers

Posted 22 March 2010 - 07:18 PM

Hello KYA,
I'm learning about this stuff now. This was actually an assignment. I had to come up with the first code, which is working fine.

The new code should not have i or smallest at all, just the array nums, and two pointers to int, pInt and pSmallest. No other variables. The code will therefore have no use of array notation...no brackets [], except in the declaration of nums itself. So instead of

smallest = nums[0];

...you’re going to have to use the pSmallest variable to keep track of the location (not value) of the smallest number so far:

### #4 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Using Pointers

Posted 22 March 2010 - 07:30 PM

well its just pointer arithmetic then, basically what KYA posted just changing the array notation to pointer notation
```#include <iostream>

using namespace std;

int main()
{
int nums[5] = {4, 1, 10, 7, 2};
int *index = nums;
int smallest = *index;

for (int i = 0; i < 5; i++)
if(*(index+i)<smallest) smallest = *(index+i);

cout <<smallest;

cin.ignore();
cin.get();
return 0;
}

```

This post has been edited by ImaSexy: 23 March 2010 - 03:33 PM

### #5 baavgai

• Dreaming Coder

Reputation: 7164
• Posts: 14,932
• Joined: 16-October 07

## Re: Using Pointers

Posted 23 March 2010 - 04:27 AM

marcelomg, on 22 March 2010 - 08:18 PM, said:

[i]The new code should not have i or smallest at all, just the array nums, and two pointers to int, pInt and pSmallest. No other variables. The code will therefore have no use of array notation...no brackets [], except in the declaration of nums itself. So instead of

This is too fun not to play. Restrictions are a great challenge.

Here's a working answer along the lines of what I believe is expected. Lots of comments to read.
```#include <iostream>

using namespace std;

int main() {
const int NUM_SIZE = 5; // never have mysery size arrays, const in good
int *pInt, *pSmallest, nums[NUM_SIZE] = {4, 20, 18, 7, 2};

// init both pointers to the beginning of our array
pInt = pSmallest = nums;

// watch carefully, we increment pInt first
// so it starts at the second element
// also, we use pointer arithmetic to check for the end of the array
// it's better to put this in a variable, but I'm playing by the rules
while(++pInt<(nums+NUM_SIZE)) {
// compare values
if (*pInt < *pSmallest) {
// save pointer value
pSmallest = pInt;
}
}

cout << *pSmallest << endl;

return 0;
}

```