what type of function is this ?

Thread Starter

@vajra

Joined May 2, 2018
154
I understand this function doesn't pass any argument but it will get return data
Code:
<return-type> function( void )
{
   statements; 
}
Code:
<return-type>* function( void )
{
 statements ;
}
could anyone help me to understand what type of function is this ?
 

BobaMosfet

Joined Jul 1, 2009
2,211
This function returns a pointer, or in other words an address to a memory location sized & shaped according to the type.

Code:
int myIntVar;                                     // global variable

int *myfunction(void);                      // function prototype

int *myfunction(void)                      // function declaration
   {
   return(&myIntVar);                      // return the address of this variable to the caller
   }
If you want to learn C, get a book. You need a reference that explains not just functions, but operators. Operators are extremely powerful. Any serious C developer should have these in their library at a minimum:

Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6

Title: Algorithms in C, 3rd Ed. [Parts 1-4, Fundamentals, Data Structures, Sorting, Searching]
Author: Robert Sedgewick
ISBN: 0-201-31452-5
 

Ian Rogers

Joined Dec 12, 2012
1,136
Years ago when I started in C I was given TurboC 1.0.. ( made by Wizard C, later bought by Borland )… I had the Borland version.. These were my earliest IDE's, before that I used command line on Unix.. I digress... I was given two books that accompanied the C, A reference guide and an advance user guide.... They are still with me..
Here is the reference guide
https://archive.org/details/bitsavers_borlandturuide1987_12272000
Point is... This is how we learned the language.... I would "play" make games, silly projects... My first game was hangman working with strings and dynamic allocation.... My first crane simulator was written on the TurboC++ 3.1 IDE... Borland still give Windows 4.5 c/c++ for free.... A great learning tool …

Funny though.... Since I started embedded programming I never pass references back.... I either work globally or rarely use passed parameters forward..

The stack on some micro's is a bit tiny, so whole different structure needed...
 

Thread Starter

@vajra

Joined May 2, 2018
154
This function returns a pointer, or in other words an address to a memory location sized & shaped according to the type.
I have a book and I am trying to understand the example written in book

function pointer pass address and integer and return address

Code:
#include<stdio.h>

int *return_pointer(int *, int); // this function returns a pointer of type int

int *return_pointer(int *p, int n) // pass address and integer
{
    p = p + n;   
    
    return p;  // return address
}
 
int main()
{
    int i, *ptr;   //declare int variable i and pointer *ptr
    
    int arr[5] = {11, 22, 33, 44, 55}; /* array to hold five integer  */
    
    i = 4; //set i to 4
 
    printf("element of arr = %d \n", arr[i]);  // arry[4] = 55
 
    ptr = return_pointer(arr, i);  // calling to fuction that return address
 
    printf("\nAfter incrementing arr by 4 \n\n");
 
    printf("Address of ptr = %d\n\n" , ptr);
    printf("Value at %d is %d\n", ptr, *ptr);
 
    return 0;
}
program wasn't commented I have added comment line in program. I might be wrong somewhere
 
Top