Functions that return a pointer

  • Thread starter woofr_87
  • Start date
  • Tags
    Functions
In summary: NULL || a[i]==NULL) max = a[i]; else { for(i=0;i<size;i++) {
  • #1
woofr_87
4
0
hye everyone...

i want to build a program where the program with a function that returns a pointer to the maximum value of an array of double. if the array is empty, the program should return NULL.

here is my code, but still have an error... every week i try to solve this problem but...hmmm.. I am new in programming...

can anyone assist me to solve my problem...

PHP:
#include <stdio.h>

double* maximum(double* a, int size);

double* maximum(double* a, int size)
{
	double max = 0;

	int i;



if(a==NULL || a[i]==NULL)
{
	printf("NULL");
}

else
{

	for(i=0;i<size;i++)
	{
		
		if(a[i]>max)
		{
			max=a[i];


		//ptr_max = &max;
		//result2 = *ptr_max;

		//result2 = max;


		return max;
		}
		}
	}

	




}


void main()
{
	double array[4]={30.00,20.00,70.00,10.00};
	//maximum(array,4);
//	double result;

	//result = maximum(array,4);
	printf("The largest number is : %0.2f",maximum(array,4));






}
 
Physics news on Phys.org
  • #2
Your function is actually returning a double, not a pointer to a double (double*). Here are a couple of lines in your maximum function.
Code:
    double max = 0; 
    .
    .
    // Calculate the maximum value of the passed array.
    .
    return max;
The quickest fix is to change your function prototype and definition so that it returns a double, like so:
Code:
double maximum(double* a, int size); 
double maximum(double* a, int size) 
{
    .
    .
    .
}
BTW, since your maximum function is defined above main(), you don't need the prototype. The compiler treats your function definition as also a declaration. If you had the definition of maximum below main(), then you would need the prototype.

If for some reason you really want to return a pointer to a double, then you won't be able to have a variable that is local to maximum as the thing you return, at least if that variable's storage class is automatic (allocated on the stack). The reason for this is that variables that are local to maximum spring to life when the function is entered, and die off when the function exits. Having a pointer to a variable that no longer exists does you no good.
 
Last edited:
  • #3
@Mark44...Thanks for your respond...but I am still unclear...can you explain more detail about this...
Mark44 said:
then you won't be able to have a variable that is local to maximum as the thing you return, at least if that variable's storage class is automatic (allocated on the stack).
 
  • #4
Read in your text or reference book about storage class specifiers - auto, static, extern. To fix your problem, just have the function return a double, not a double *.
 
  • #5


Hello! It looks like you are having trouble with your program. I would suggest starting by breaking down the problem into smaller parts and testing each part individually. This can help you identify where the error is coming from.

First, make sure your function is correctly declared and defined. In your code, the function is declared as returning a double pointer, but it looks like you are trying to return a double value. Make sure the return type matches what you want to return.

Next, check your logic for finding the maximum value. Your current code will only return the first value in the array that is larger than 0. Instead, you should initialize the max variable to the first element in the array, and then compare it to the rest of the elements in the array to find the true maximum value.

Additionally, your code will not work if the array is empty, as you are trying to access a NULL value. You should add a condition to check if the array is empty and return NULL in that case.

Finally, make sure you are properly using pointers in your code. You should be returning a pointer to the maximum value, not the value itself. You can use the & operator to get the address of the maximum value and return that.

I hope this helps you in solving your problem. Good luck!
 

1. What is a function that returns a pointer?

A function that returns a pointer is a type of function that, instead of returning a value of a specific data type, returns the memory address of a variable or object in the program. This allows the programmer to access and manipulate the data stored at that memory address.

2. Why would I use a function that returns a pointer?

Functions that return pointers are especially useful when working with large or complex data structures, such as arrays or linked lists. They allow for efficient memory management and can improve the performance of the program.

3. How do I declare a function that returns a pointer?

To declare a function that returns a pointer, you would use the asterisk (*) symbol in the function declaration, right before the name of the function. For example: int* getArray(int size); This function would return a pointer to an integer array of the specified size.

4. Can I modify the value of the pointer returned by the function?

Yes, you can modify the value of the pointer returned by the function. Since the pointer is just a memory address, you can use it to access and manipulate the data at that address. However, you should be careful when doing so to avoid any potential errors or memory leaks.

5. Is it possible for a function to return multiple pointers?

Yes, it is possible for a function to return multiple pointers. This can be achieved by using arrays or structures as the return type of the function, and then returning multiple pointers to different elements of the array or structure.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
669
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
927
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
Back
Top