Need some clarification about pointers...

Hello! I'm new here.

Anyway, I just want some clarification about pointers. I've watched videos, but I'm still a bit confused. My class had a homework problem regarding it.
Basically, I just want to make sure that the side notes I put are correct.

The code works though.
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
/*
Use pointers to write a function that finds the smallest element in an array of integers.

Use [1,2,4,5,10,100,2,-22] to test the function.
*/

#include "stdafx.h"
#include <iostream>

using namespace std;

int *search(int *list, int size) /*If I don't put the * next to the "list", I won't be able to access the values 
								 listed on the "main". That's what I think*/
{
	int x = 0;
	for (int i = 0; i < size ; i++) //just to find the smallest element from the array
	{
		if (x > list[i]) //If condition probably works here
		{
			x = list[i];
		}
	}
	cout << "Smallest element is: " << x << endl;
	return 0; /*not really sure what to put here, I keep getting error unless I put a return value so I guess
			  it's just zero... UGH!!! It works though.*/
}

int main()
{
	int list[] = { 1,2,4,5,10,100,2,-22 };
	search(list, 8);
	system("pause");
    return 0;
}

/*not really sure what to put here, I keep getting error unless I put a return value so I guess
it's just zero... UGH!!! It works though.*/


If you don't want the function to return anything, why have you specified that the function returns an int-pointer? int *search(int *list, int size)

What happens if there is no negative number in your list? For example, if the list was { 5, 6} ?Your answer will be zero, which is wrong.
Last edited on
finds the smallest element in an array

Element. Not value.

* I don't think that your function should print anything. It has exactly one task: find element.
* Returning pointer makes sense though; the returned pointer should point to the smallest element.
* The input array could be empty. It could even be invalid. How do you represent a pointer that points to exactly nowhere?

There are two approaches to setting the first "smallest":
* Initialize with first element in the array
* Initialize with a value that cannot be smaller than any possible input.
See http://www.cplusplus.com/reference/limits/numeric_limits/

For extra fun, do use pointers in the loop, not the int i.


A program to test your function with:
1
2
3
4
5
6
7
8
9
10
int main()
{
  int list[] = { 1,2,4,5,10,100,2,-22 };
  int * result = search( list, 8 );
  if ( /*result is valid*/ )
  {
    std::cout << "Smallest element is: " << *result << '\n';
  }
  return 0;
}
Last edited on
search:
if you don't put the *, it wont compile if you call it in main with list. It will say you are trying to put an int * into an int, which is not allowed without explicit casting for type safety reasons.

the name of an array is treated like a pointer. That is, int *p = &(list[0]) is the same as int *p = list and you can pass list to a function, as you did, as if list were an int pointer.


Think of the memory of your computer as a giant array of bytes.
An index into that array is an integer value representing where exactly the starting point of your data is located. That index is the pointer... most pointer work is exactly the same as handling indices into arrays, with a little bit of funky syntax, and handling allocation and deallocation when necessary (it rarely is these days).

Topic archived. No new replies allowed.