What are Struct Arguments Scope Traps- C Programming Course in Telugu

🚀 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 →

Free Embedded C Course · Structure and Union

Struct Arguments Scope Traps

Pass structures by value, by pointer and by const pointer, and learn why a struct tag declared in the wrong place makes the compiler reject your function call.

Passing a structure to a function looks identical to passing an integer, and for that reason it hides several traps. The structure arguments in C rules decide what the function sees, whether the original changes, and, less obviously, whether the compiler even agrees that the type you pass is the type the function expects. In this lecture of our free embedded C course we compile deliberately broken programs, read the exact errors, and then fix them. It follows the same practical style as the rest of our free C programming course.

structure arguments in Cstruct scopepass by valuepass by pointerfree embedded C courseembedded C programming

What You Will Learn

  • How structure arguments in C are copied and what that means for the caller
  • How a struct tag declared inside a function breaks a later function definition
  • Why an anonymous struct in a parameter is never compatible with your variable
  • How to pass by pointer and by const pointer, and when each fits
  • How to judge the stack cost of passing large structures by value
  • How to read three real compiler diagnostics

Prerequisites

You should know how to define a structure and how functions pass arguments by value. The earlier lectures of this chapter on pointers to structures and on the arrow operator are directly useful.

Structure Arguments in C Are Copies

When you call bump_by_value(t) the compiler copies the whole structure into the parameter. The function then works on its private copy. When the function returns, the copy is discarded. The variable in the caller was never touched.

When you want the function to change the caller’s structure, pass an address instead, and use the arrow operator inside the function. When the function only needs to look, pass a pointer to const. The following program shows all three styles in one place.

#include <stdio.h>

struct counter { int ticks; char unit; };   /* file scope: visible everywhere */

static void bump_by_value(struct counter c)
{
    c.ticks++;
    printf("inside by value : %d\n", c.ticks);
}

static void bump_by_pointer(struct counter *c)
{
    c->ticks++;
    printf("inside by pointer: %d\n", c->ticks);
}

static void show_counter(const struct counter *c)
{
    printf("read-only view  : %d %c\n", c->ticks, c->unit);
}

int main(void)
{
    struct counter t = { 12, 'm' };

    bump_by_value(t);
    printf("after by value  : %d\n", t.ticks);

    bump_by_pointer(&t);
    printf("after by pointer: %d\n", t.ticks);

    show_counter(&t);
    printf("sizeof(struct counter)=%zu\n", sizeof t);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra -o arg_ok arg_ok.c
$ ./arg_ok
inside by value : 13
after by value  : 12
inside by pointer: 13
after by pointer: 13
read-only view  : 13 m
sizeof(struct counter)=8

Read the first four lines carefully. Inside the by-value function the counter reached 13, but back in main it was still 12. After the pointer call it was 13 in both places. The structure here is eight bytes, which includes padding after the single-character member.

What the callee receives
By value
a full copy of the structure on the stack
By pointer
only an address, the original is edited
By const pointer
an address, but writes are rejected

Trap 1: Struct Tag Declared Inside a Function

Tags have scope, just like variables. If you define struct reading inside main, the tag exists only inside main. A function defined outside main cannot see it. Here is the broken program.

#include <stdio.h>

void show(struct reading r);        /* tag unknown at file scope */

int main(void)
{
    struct reading { int raw; char unit; };   /* tag lives only inside main */
    struct reading v = { 2, 's' };
    show(v);
    return 0;
}

void show(struct reading r)
{
    printf("%d %c\n", r.raw, r.unit);
}
$ gcc -std=c11 -Wall -Wextra arg_scope_bad.c
arg_scope_bad.c:3:18: warning: 'struct reading' declared inside parameter list will not be visible outside of this definition or declaration
arg_scope_bad.c:9:10: error: type of formal parameter 1 is incomplete
arg_scope_bad.c:13:26: error: parameter 1 ('r') has incomplete type

The prototype on line 3 mentions struct reading before any declaration of that tag exists at file scope. The compiler responds by creating a new, incomplete type that lives only inside the parameter list. Passing a variable of the real type from main can never match it, and the function body cannot access members of an incomplete type.

How to Fix It

Move the structure definition to file scope, above every function that uses it. In a real project that means putting it in a header and including that header wherever the type is needed. The fixed program is the one you already ran above: the type struct counter is defined at the top of the file.

Trap 2: Anonymous Struct in the Parameter

A second way to end up with a mismatched type is to define the structure right inside the parameter list.

#include <stdio.h>

struct reading { int raw; char unit; };

/* a parameter declared with its own anonymous struct is a different type */
void show(struct { int raw; char unit; } r)
{
    printf("%d %c\n", r.raw, r.unit);
}

int main(void)
{
    struct reading v = { 2, 's' };
    show(v);
    return 0;
}
$ gcc -std=c11 -Wall -Wextra arg_anon_bad.c
arg_anon_bad.c:6:11: warning: anonymous struct declared inside parameter list will not be visible outside of this definition or declaration
arg_anon_bad.c:14:10: error: incompatible type for argument 1 of 'show'

Both structures have identical members, yet C considers them different types. Two separate struct definitions create two separate types, even when the members match exactly. The anonymous struct in the parameter also cannot be named anywhere else, so no caller could ever construct a matching value. The cure is the same: define the type once, name it, and use the name in the function.

Compatible and incompatible struct types
struct reading at file scope
one type, usable everywhere
anonymous struct in a parameter
a second type, usable nowhere else
Result
arguments never match

Passing Structure Arguments in C: Value or Pointer?

StylePrototypeChanges visible to caller?CostUse when
By valuevoid f(struct counter c)NoCopies sizeof(struct) bytesSmall structures and pure functions
By pointervoid f(struct counter *c)YesCopies one addressFunction must modify the object
By const pointervoid f(const struct counter *c)No (compiler enforced)Copies one addressLarge structures that are only read

A good rule for embedded C programming: if the structure is larger than a few words, or if the function is called often from a task with a small stack, pass a const pointer. If the structure is tiny and you want clean value semantics, passing by value is perfectly fine.

Common Mistakes

MistakeDiagnostic or symptomFix
Defining the struct inside main and calling other functions with itdeclared inside parameter listDefine at file scope or in a header
Defining a struct inline in a parameterincompatible type for argumentUse a named type
Expecting a by-value function to modify the callerCaller’s value never changesPass a pointer
Passing a large struct by value in a deep call chainStack overflow on a small MCUPass a const pointer
Omitting the function prototypeImplicit declaration errorDeclare every function before use

Best Practices

  • Declare structure types in headers, never inside functions that other functions must share.
  • Name every struct. An unnamed struct in a parameter is a bug waiting to be compiled.
  • Use const pointer parameters for read-only access to large structures.
  • Check the stack cost: on small targets a by-value parameter of a hundred bytes is repeated at every level of recursion or call depth.
  • Keep -Wall -Wextra on; the scope warning appears before the hard error and tells you the cause.

Performance and security. Passing by value copies bytes each call and can overflow a small stack when structures are large. Passing pointers avoids the copy, but then the callee can reach the caller’s memory, so use const when the function should not change anything and validate any pointer that comes from outside the module.

Real-World Use Cases

  • A sensor driver returns a small reading structure by value, while a logger takes a pointer to a large frame and only reads it
  • A shared header defines the packet header structure so that the parser, the logger and the test code all agree on the same type
  • A configuration function takes a const pointer to a settings block so firmware code cannot accidentally change defaults

Summary and Key Takeaways

  • Structure arguments in C are passed by value: the callee works on a copy.
  • Pass a pointer when the function must modify the original, and a const pointer when it only reads.
  • Struct tags have scope; define types at file scope or in headers.
  • An anonymous struct in a parameter list creates an unusable type.
  • Two struct definitions with identical members are still different types.

Conclusion

Most trouble with structure arguments in C is not about the call itself. It is about the type: where it is defined, how long its tag stays visible, and whether the function and caller really agree on it. Define each structure once, in a header or at the top of a file, pass a pointer when the callee must modify or when the structure is large, and read compiler warnings about parameter lists as they come. The next lecture closes this chapter with union exercises, a quiz and interview questions.

structure arguments in C FAQ

How are structure arguments in C passed to a function?

By value, like every argument in C. The function receives a complete copy of the structure, so changes to the parameter never reach the caller’s variable. To let the function modify the original you pass its address.

Why does GCC say ‘declared inside parameter list will not be visible outside’?

The compiler met a struct tag in a parameter declaration and could not find an earlier declaration of that tag in scope. It therefore invented a brand-new type that exists only inside that parameter list, and that type can never match anything you pass in.

Where should a struct definition live so that functions can use it?

At file scope, normally in a header included by every source file that needs it. A struct tag declared inside a function is visible only inside that function.

Can a function parameter be declared with an inline struct definition?

Syntactically yes, but the parameter gets its own unnamed type that is incompatible with every other struct, even one with identical members. The call then fails to compile.

Is it bad to pass a large structure by value in embedded C programming?

It can be. The whole structure is copied onto the stack on each call. On small microcontrollers with limited stack this is wasteful, so pass a pointer, preferably a pointer to const when the function only reads.

What is the difference between a pointer and a const pointer parameter?

A const struct counter * parameter lets the function read the structure but the compiler rejects any attempt to modify it through that pointer. It documents intent and protects the caller.

Do old programs without function prototypes still compile?

Calling a function that has not been declared was removed from the language in C99, and GCC 14 reports it as an error by default. Always declare functions before calling them.

Finish the Chapter in the Free C Programming Course

The final lecture of this chapter has union exercises, a quiz and interview questions.

{ “@context”: “https://schema.org”, “@type”: “FAQPage”, “mainEntity”: [ { “@type”: “Question”, “name”: “How are structure arguments in C passed to a function?”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “By value, like every argument in C. The function receives a complete copy of the structure, so changes to the parameter never reach the caller’s variable. To let the function modify the original you pass its address.” } }, { “@type”: “Question”, “name”: “Why does GCC say ‘declared inside parameter list will not be visible outside’?”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “The compiler met a struct tag in a parameter declaration and could not find an earlier declaration of that tag in scope. It therefore invented a brand-new type that exists only inside that parameter list, and that type can never match anything you pass in.” } }, { “@type”: “Question”, “name”: “Where should a struct definition live so that functions can use it?”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “At file scope, normally in a header included by every source file that needs it. A struct tag declared inside a function is visible only inside that function.” } }, { “@type”: “Question”, “name”: “Can a function parameter be declared with an inline struct definition?”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “Syntactically yes, but the parameter gets its own unnamed type that is incompatible with every other struct, even one with identical members. The call then fails to compile.” } }, { “@type”: “Question”, “name”: “Is it bad to pass a large structure by value in embedded C programming?”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “It can be. The whole structure is copied onto the stack on each call. On small microcontrollers with limited stack this is wasteful, so pass a pointer, preferably a pointer to const when the function only reads.” } }, { “@type”: “Question”, “name”: “What is the difference between a pointer and a const pointer parameter?”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “A const struct counter * parameter lets the function read the structure but the compiler rejects any attempt to modify it through that pointer. It documents intent and protects the caller.” } }, { “@type”: “Question”, “name”: “Do old programs without function prototypes still compile?”, “acceptedAnswer”: { “@type”: “Answer”, “text”: “Calling a function that has not been declared was removed from the language in C99, and GCC 14 reports it as an error by default. Always declare functions before calling them.” } } ] }
💬 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 →