I just learned pointers in my computer science class and i am having a hard time understanding what they are used for. Are they absolutely necessary to use them when passing arrays from functions ect. or is it just a more efficient way of doing so?

Dani AI

Generated

Good starter replies from and , and thanks to for the tutorial link. Below are practical, hands‑on notes and a small code example that address common beginner mistakes and safe patterns you can apply right away.

Bad vs safe patterns (don’t return addresses of local stack objects; document who owns memory):

/* BAD: returns pointer to local stack data -> dangling pointer */
char *bad_message(void) {
    char buf[16];
    strcpy(buf, "hello");
    return buf; /* UB when caller uses the pointer */
}

/* SAFE: caller provides the buffer */
char *fill_message(char *buf, size_t buf_size) {
    if (!buf || buf_size == 0) return NULL;
    snprintf(buf, buf_size, "hello");
    return buf;
}

/* ALTERNATIVE: allocate and document ownership (caller must free) */
char *alloc_message(void) {
    char *p = malloc(6);
    if (p) memcpy(p, "hello", 6);
    return p;
}

Quick practical checklist

  • Initialize pointers (use NULL) and check them before dereference.
  • Always pass a length with buffers; avoid implicit bounds assumptions.
  • Use const for read‑only parameters to clarify intent.
  • Document ownership: who must call free.
  • After freeing, set the pointer to NULL to reduce reuse bugs.

Tools and further reading

  • Run with compiler warnings (-Wall -Wextra) and sanitizers (-fsanitize=address,undefined) during development.
  • Use runtime tools like Valgrind for memory checks and AddressSanitizer docs to catch issues early.
  • For language details, see the pointer reference on cppreference.

These patterns make pointer code predictable and far easier to debug than chasing undefined behavior.

Recommended Answers

All 3 Replies

>Are they absolutely necessary to use them when passing arrays from functions ect. or is it just a more efficient way of doing so?
It is the only way you can pass by reference.
Further ahead in you education you'll learn that pointers shine when they are used in conjunction with allocation of dynamic memory and pointer to function.

I just learned pointers in my computer science class and i am having a hard time understanding what they are used for. Are they absolutely necessary to use them when passing arrays from functions ect. or is it just a more efficient way of doing so?

Don't look at the problem in hand,try to see through it ok.

Pointers are nothing but what they literally mean. They just point to things(Here variables arrays etc).
Here in C when we work we have this kind of layout:

Name
Memory
Address

Every memory block we use has an address( got by using &variable ) and we give it a name ( the variable name we use most of the time ).
We can easily pass these kind of (single unit) types to functions as arguments easily.But when it comes to an array it is not a single unit as such. It is a unit formed of similar units taken together and just think how difficult it would be to pass each unit of the array to function as array[0],array[1] ( If its a large number as array[1000] or something we are doomed :D )
So in case of arrays what we do is we send something which points to the memory unit where the array starts and access it.

Once you dive to the depths of "C" you wont find a topic more beautiful than pointers.Just keep your chill and learn what has been taught to you perfectly. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.