C Pointers and Functions

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Pointers and Functions?

Pointer parameters let a function modify caller data or avoid copying large objects. A function can also return a pointer, but never to one of its own local variables. Returning a pointer into the caller's array is a safe and common pattern.

Remember
void  reset(int *p)           { *p = 0; }
int  *find_max(int *a, int n);       /* returns pointer into a */
const char *name_of(int code);       /* returns static text    */

Cheat Sheet

Keep these facts handy while you work with Pointers and Functions in C.

TopicDetail
Modifyvoid f(T *out)
Read-onlyconst T *in
ReturnPointer to static, heap, or caller data
NeverReturn the address of a local

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Array of Pointers.

Example 1 — Return a pointer to the maximum element

The caller can read or modify the element through the result.

C
#include <stdio.h>

int *find_max(int *a, int n) {
    int *best = &a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] > *best) best = &a[i];
    }
    return best;
}

int main(void) {
    int data[] = {3, 9, 4};
    int *m = find_max(data, 3);

    printf("max = %d\n", *m);
    *m = 0;
    printf("%d %d %d\n", data[0], data[1], data[2]);
    return 0;
}

How It Works

  1. find_max returns the address of the largest element.
  2. *m = 0 edits the original array element.
  3. The returned pointer is valid because data outlives the call.

Edge Cases & Pitfalls

Check these before relying on Pointers and Functions in real programs.

local

Returning a local address

The local variable is destroyed on return, so the pointer dangles.

const

Dropping const

Passing a const pointer into a function expecting T * discards protection and warns.

Key Takeaways

  • Modify: pass the address.
  • Return: only valid lifetimes.
  • const: document read-only access.

One line: Pointer parameters let a function modify caller data or avoid copying large objects.

Frequently Asked Questions

Yes. Return a pointer to heap memory, static storage, or data owned by the caller, but never to a local automatic variable.
A pointer copies only an address, which is cheaper than copying the whole struct, and lets the function modify the original.
Continue with "C Array of Pointers" next. Store several pointers in an array, such as a list of strings. The C Pointers hub lists every topic in order.

Next: Array of Pointers

Store several pointers in an array, such as a list of strings.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful