Functions that return a pointer

  • Thread starter Thread starter woofr_87
  • Start date Start date
  • Tags Tags
    Functions
Click For Summary

Discussion Overview

The discussion revolves around creating a function in C that returns a pointer to the maximum value of an array of doubles. Participants explore the implementation details, address errors in the code, and clarify concepts related to pointers and variable storage classes.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The original poster's function is intended to return a pointer to the maximum value in an array, but it currently returns a double instead of a double pointer.
  • One participant suggests changing the function's return type to double to resolve the issue.
  • Another participant notes that if the function is to return a pointer, it cannot return a local variable due to its automatic storage class, which would lead to undefined behavior.
  • There is a suggestion to read about storage class specifiers such as auto, static, and extern to better understand the implications of variable lifetimes in C.

Areas of Agreement / Disagreement

Participants generally agree that the function should not return a local variable if a pointer is intended to be returned. However, there is no consensus on whether the function should be modified to return a double instead of a pointer, as some participants seek further clarification on the implications of returning pointers.

Contextual Notes

Unresolved issues include the handling of an empty array and the correct implementation of pointer logic in the function. There are also discussions about the necessity of function prototypes based on the order of function definitions.

Who May Find This Useful

Individuals learning C programming, particularly those interested in pointers, function return types, and variable storage classes.

woofr_87
Messages
4
Reaction score
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
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:
@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).
 
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 *.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K