What are Pointer Members Inside Structures- Best C Programming Course

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

PREV_LEC  |  NEXT_LEC

Free C Programming Course · Chapter 10 · Lecture 14

Pointer Members Inside Structures

Understand a pointer inside a structure in C: how to read the pointer, how to reach the value it points to, how precedence works, and the shallow-copy trap. Free embedded C course.

Topics in This Free C Tutorial

pointer inside a structure in C pointer member shallow copy NULL pointer free embedded C course embedded C programming

So far our structures held plain data. Now we put a pointer inside a structure in C. A pointer member lets a structure refer to data that lives elsewhere: a buffer, a counter, a result variable or another structure. This is how embedded code connects a channel descriptor to its destination variable or a driver object to its buffer. In this lecture of our free C programming course you will learn the two things a pointer member can give you (the address it stores and the value it points to), the precedence rules that decide how *cp->sink is read, and a copy trap that catches many beginners.

What You Will Learn

  • How to declare a pointer member inside a structure
  • How to read the stored address and the value it points to
  • How precedence decides the meaning of *cp->sink
  • What happens when you copy a structure that contains a pointer
  • Why a pointer member should start as NULL or a valid address

Prerequisites

You should have read the previous lecture on pointers to structures and know the dot and arrow operators. Understanding what *p does for an ordinary int *p is enough pointer theory for this lesson.

Declaring a Pointer Inside a Structure in C

A pointer member is declared exactly like any pointer variable, only inside the braces of the structure. The following structure describes one ADC input channel together with a pointer to the integer variable where its samples should be deposited:

struct adc_channel {
    int  id;
    int *sink;               /* pointer member: where to deposit the sample */
};

This structure holds two things: a plain integer id and an address, sink. The structure does not hold the sampled value itself. The value lives in some other variable, and sink merely points to it.

A pointer member refers to data outside the structure

1

struct adc_channel ch

Contains id (an int) and sink (an address).

2

ch.sink

The stored address of the target variable.

3

int result

The target, stored separately. *ch.sink reaches it.

Reading a Pointer Inside a Structure in C

There are two different things you can ask for, and each has two syntaxes depending on whether you have the structure or a pointer to it.

You wantWith structure variable chWith pointer cp
The stored addressch.sinkcp->sink
The value it points to*ch.sink*cp->sink

Precedence: What Does *cp->sink Mean?

The dot and arrow operators have higher precedence than the dereference star. So *ch.sink is read as *(ch.sink) and *cp->sink as *(cp->sink): first select the member, then follow it. That is exactly what we want, so no extra parentheses are required, although writing them can make code clearer for readers.

Complete Example With a Pointer Member

Save as p2_ptr_member.c:

#include <stdio.h>

struct adc_channel {
    int  id;
    int *sink;
};

int main(void)
{
    int result = 11;
    struct adc_channel ch = { 3, &result };
    struct adc_channel *cp = &ch;

    printf("ch.sink      = %p\n", (void *)ch.sink);
    printf("cp->sink     = %p\n", (void *)cp->sink);

    printf("*ch.sink     = %d\n", *ch.sink);
    printf("*cp->sink    = %d\n", *cp->sink);

    *cp->sink = 99;
    printf("result now   = %d\n", result);

    printf("*(cp->sink)  = %d\n", *(cp->sink));
    return 0;
}

Build and Run

$ gcc -Wall -Wextra -std=c11 p2_ptr_member.c -o p2
$ ./p2
ch.sink      = 0x7fff35b09a54
cp->sink     = 0x7fff35b09a54
*ch.sink     = 11
*cp->sink    = 11
result now   = 99
*(cp->sink)  = 99

Your address values will be different, but the first two lines will always match each other, because they are two routes to the same member. The assignment *cp->sink = 99 changed the variable result, which is not even part of the structure, showing that the pointer member really does reach outside it.

The Shallow-Copy Trap

When you assign one structure to another, every member is copied byte for byte. For a pointer member, that means the address is copied, not the data it points to. Both structures end up pointing at the same variable. Save as p4_shallow_copy.c:

#include <stdio.h>

struct holder {
    int  id;
    int *data;
};

int main(void)
{
    int sample = 10;
    struct holder a = { 1, &sample };
    struct holder b = a;     /* copies id and the pointer, not the int */

    *b.data = 77;            /* change through the copy */

    printf("a.data = %p, b.data = %p\n", (void *)a.data, (void *)b.data);
    printf("*a.data = %d  (a never touched)\n", *a.data);
    printf("same target? %s\n", a.data == b.data ? "yes" : "no");

    struct holder c = { 3, NULL };
    if (c.data == NULL)
        printf("c.data is NULL: safe to test before use\n");
    return 0;
}
$ ./p4
a.data = 0x7ffef02543cc, b.data = 0x7ffef02543cc
*a.data = 77  (a never touched)
same target? yes
c.data is NULL: safe to test before use

After struct holder b = a

Variableiddata (address)Points to
a1address of samplesample
b1same addresssample (shared)

This is called a shallow copy. It is often what you want, such as when many structures should share one buffer. When you actually need independent data, you must copy the pointed-to data yourself.

Initialising a Pointer Inside a Structure in C

A pointer member inside a local structure holds garbage until you set it. Dereferencing garbage is undefined behaviour and on a microcontroller it may hit a peripheral address and cause a fault. Set every pointer member either to a valid address or to NULL when the structure is created, and test for NULL before using it, as the last lines of the example did for c.data.

Real-World Uses of a Pointer Inside a Structure in C

ApplicationPointer member holds
ADC or sensor driverAddress of the variable that receives results
UART driver objectAddress of the transmit or receive buffer
Linked list or queueAddress of the next node of the same structure type
Text labelPointer to a constant string stored in flash

Common Mistakes

  • Confusing the address with the value. cp->sink is an address; *cp->sink is the value.
  • Assuming a copy duplicates the pointed-to data. It only duplicates the address.
  • Leaving the pointer uninitialised. Start with a valid address or NULL.
  • Pointing at a local variable that goes out of scope. The pointer then dangles, so make sure the target outlives the structure.

Best Practices and Safety

Checklist

  • Initialise pointer members at declaration.
  • Check against NULL before every dereference that could fail.
  • Use const on pointer members that must only be read.
  • Write a comment stating who owns the pointed-to data, so nobody frees or overwrites it by mistake.
  • On resource-limited targets, a pointer member costs only the pointer size (typically 4 bytes on 32-bit microcontrollers) rather than a full copy of the data.

Summary and Key Takeaways

  • A pointer member stores an address; the data it points to lives outside the structure.
  • s.p or sp->p gives the address, while *s.p or *sp->p gives the value.
  • Dot and arrow bind tighter than star, so *sp->p means *(sp->p).
  • Copying a structure copies pointer members as addresses, which is a shallow copy.
  • Always initialise pointer members.

Conclusion

A pointer inside a structure in C lets small structures describe large or distant data cheaply, and it is the foundation of buffers, queues and driver objects. In the next lecture we finish the chapter section by looking at how structure members are passed to functions, and apply everything in a UART configuration lab with a quiz and interview questions.

Suggested Images for This Lecture

Image ideaALT text
A structure box with an arrow leaving to a separate variablePointer inside a structure in C pointing to a variable outside the structure
Two structures with arrows to the same variableShallow copy of a pointer member in C, both structures sharing one target
Terminal output of the pointer member programPointer member output in a free embedded C course program

Frequently Asked Questions

How do I access the value a structure’s pointer member points to?

Dereference the member: *s.p with a structure variable, or *sp->p with a pointer to the structure.

Do I need parentheses in *sp->p?

No. The arrow binds tighter than the star. Parentheses are optional and can help readability.

What does copying a structure with a pointer member do?

It copies the address only. Both structures then point to the same data.

Can a pointer member be NULL?

Yes, and that is a good default. Test it before dereferencing.

Where does the pointed-to data live?

Outside the structure, in another variable, a buffer, flash or dynamic memory.

Is a pointer member the same as a nested structure?

No. A nested structure is stored inside the outer one, while a pointer member stores only an address.

Finish Chapter 10 Part One

Next: passing structure members to functions, a UART lab, a quiz and interview questions.

PREV_LEC  |  NEXT_LEC

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →