If this is something your code does frequently, it can cause a noticeable performance difference. Second, no matter how many times you copy a pointer around, it's still pointing to the same structure in memory. All modifications to it will be reflected on the same structure.
But if you copy the structure itself, and then make a modification, the change only shows up on that copy. Any code that holds a different copy won't see the change. Sometimes, very rarely, this is what you want, but most of the time it's not, and it can cause bugs if you get it wrong. In addition to other answers, sometimes returning a small struct by value is worthwhile. For example, one could return a pair of one data, and some error or success code related to it.
For historical reasons it is not the case and errors are reported thru the errno global, which today is a macro. Notice that the Go language has a nice notation to return two or a few values. So in new C code, returning a small C struct can be more readable, more thread-friendly, and more efficient. If you have a texture for example somewhere in memory, and you want to reference that texture in several places in your program; it wouldn't be wise to make a copy every time you wanted to reference it.
Instead, if you simply pass around a pointer to reference the texture, your program will run much faster. The biggest reason though is dynamic memory allocation. When this happens, the amount of memory you need to use will be determined at runtime. A good example of this is reading from a file that is specified by the user.
In this case, you have no idea how large the file may be when you compile the program. You can only figure out how much memory you need when the program is actually running. Both malloc and free return pointers to locations in memory. So functions that make use of dynamic memory allocation will return pointers to where they have created their structures in memory.
You can indeed do this. The following should work:. When a program calls fopen , it generally won't care about any of the contents of the returned structure--all it will care about is that other functions like fread will do whatever they need to do with it. Having fread receive a pointer to the FILE makes that easy. The most common one is information hiding. C doesn't have, say, the ability to make fields of a struct private, let alone provide methods to access them. So if you want to forcefully prevent developers from being able to see and tamper with the contents of a pointee, like FILE , then the one and only way is to prevent them from being exposed to its definition by treating the pointer as opaque whose pointee size and definition are unknown to the outside world.
The definition of FILE will then only be visible to those implementing the operations that require its definition, like fopen , while only the structure declaration will be visible to the public header. Hiding the structure definition can also help provide breathing room to preserve binary compatibility in dylib APIs.
It allows the library implementers to change the fields in the opaque structure without breaking binary compatibility with those who use the library, since the nature of their code only needs to know what they can do with the structure, not how large it is or what fields it has. As an example, I can actually run some ancient programs built during the Windows 95 era today not always perfectly, but surprisingly many still work.
Chances are that some of the code for those ancient binaries used opaque pointers to structures whose size and contents have changed from the Windows 95 era. Yet the programs continue to work in new versions of windows since they weren't exposed to the contents of those structures. When working on a library where binary compatibility is important, what the client isn't exposed to is generally allowed to change without breaking backwards compatibility.
It is typically less efficient assuming the type can practically fit and be allocated on the stack unless there's typically a much less generalized memory allocator being used behind the scenes than malloc , like a fixed-sized rather than variable-sized allocator pooling memory already allocated.
However, if don't want functions to modify original structure use the const keyword. Recall that const keyword when applied to a variable makes it read-only. Try commenting out code in line 32 and see it yourself. We have already seen how to pass an array of integers to a function. Similarly, we can pass an array of structures to a function.
In lines , a structure company is declared with four members namely name , ceo , revenue , pps. In lines , an array of structure called companies of type struct company is declared and initialized. If you don't want to call a function to modify the original structure use the keyword const. In lines , a for loop is used to loops through the array of structure and prints the details of each company. Recall that name of the array i.
In this case, 0th element is of type struct company. Just as we can return fundamental types and arrays, we can also return a structure from a function. To return a structure from a function we must specify the appropriate return type in the function definition and declaration.
Consider the following example:. Store Information of Students Using Structure. Add Two Distances in inch-feet system using Structures. C Structure and Function In this tutorial, you'll learn to pass struct variables as arguments to a function.
Similar to variables of built-in types, you can also pass structure variables to a function. Passing structs to functions We recommended you to learn these tutorials before you learn how to pass structs to functions.
Return struct from a function. Table of Contents Passing structure to a function Return struct from a function Passing struct by reference. Previous Tutorial:. Next Tutorial:. Share on:. Did you find this article helpful? Asked 9 years, 8 months ago. Active 10 months ago.
Viewed k times. Improve this question. Neuron 4, 4 4 gold badges 24 24 silver badges 47 47 bronze badges. GregHewgill: You are absolutely right. Add a comment. Active Oldest Votes. Improve this answer. Carl Norum Carl Norum k 29 29 gold badges silver badges bronze badges. That's quite interesting. I was always under the impression you need pointers for these. I was wrong : — mmirzadeh. You certainly don't need pointers.
That said, most of the time you would want to use them - the implicit memory copies that take place flinging structures around by value can be a real waste of CPU cycles, not to mention memory bandwidth. Probably huge. The thing is, normally if you're passing structures around by value you're copying them a lot. Anyway it's not really as simple as that. You could be passing around local or global structures, in which case thir allocation cost is pretty much free. You need pointers and allocation of memory for the returned value outside the function body as soon as the amount of memory allocated for a value isn't known at compile time.
It is for structs, so C functions have no problem returning them. Show 10 more comments. Greg Hewgill Greg Hewgill k gold badges silver badges bronze badges. That's totally implementation dependent. For example, armcc will pass small enough structures in the regular parameter passing or return value registers. Wouldn't that be returning a pointer to a local variable? The memory for the returned structure can't be part of foo stack frame. It has to be in a place that survives past the return of foo.
0コメント