How big should a structure be before you should start using a pointer to it, rather than making a copy? [C] [closed] - c

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is a slightly subjective answer, but how large do you think a structure should be before you start using pointers to it in other structures or function calls rather than the structure by-value?

Depends on the compiler and architecture.
C and C++ define the size of the types they use, and how functions are written, but they don't define how they are implemented.
This is means the standard itself doesn't define how the structurs are passed, just that they are in essence copied.
The compiler might decide to do something else entirely, like not copying the struct at all if there's a default copy constructor , and the variable isn't used.
But after saying all that, most common-sense compiler implementations would store the struct in a register if it fits. So depends on the architecture, make sure the structure fits in a register.

Related

Should structs be typedef'd in a C library? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
First off, I am well aware of the concepts behind using typedef on both names and anonymous structs. My question is how a C library should expose its types.
Typically, I would simply typedef an anonymous struct, or a named struct when I need forward declaration. However, this is an issue if a library with only typedefs is included in a project that prefers using the struct qualifier, which is no longer possible. Similarly, having both a typedef and a named struct may still pollute the general namespace for such projects.
Should a library, then, only provide named structures and leave typedefing to the user?

Why does C use the asterisk to reference the value of a pointer? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I find that I refer to the value at the memory location pointed to by a pointer far more often then I want to refer to the actual value of the pointer. As a result I wonder why C does not use the asterisk in the to refer to the actual value of the pointer since it is more typing.
I have already read a post on another website about how B did it this way. This does not answer my question; it just changes it to why did B do it this way.
I doubt anyone can tell you why it was designed the way it was. But a pointer is a variable that holds an address. Therefore, it's value is the address. I would find it strange and unexpected if it required special syntax to get the value of any variable.
Note that C++ lets you use references to access the value pointed to without special syntax. But you are dereferencing the pointer, so for me it makes perfect sense to require a different syntax.

What is the advantage of using pointers in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why do we use pointers in C Programming?
In general pointers are able to access the address where the int/float/char etc... is stored.
Are there any other uses?
It depends on what you try to achieve:
you can change the value of a variable inside a function
you can pass a struct to a function without having to copy all its fields - think of a function that receives a struct.
you can point to a specific variable/struct and point to it from other structs
and many other advantages (advantages is purpose dependant and it depends on whats your program is doing).
Pointers are quite basic C and there is a lot of material online you should get yourself familiar with them and the advantages will pop up themselves.
The reason is that pointers are used to bodge into C some vital features which are missing from the original language: arrays, strings, & writeable function parameters. They can also be used to optimize a program to run faster or use less memory that it would otherwise. A few tasks these days, such as programming microcontrollers, still need this.

How can I convert an array name to an array pointer in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a list of arrays, each with its own descriptive name.
How can I create a pointer to a particular array just using the name (ie string) of the array?
If I understand correctly, what you mean and what you want, then you want something as Map, Dictionary etc. For example, look at this question: Quick Way to Implement Dictionary in C
The C programming language is somewhat of a "low-level" language. It doesn't natively have any introspection constructs.
The names you see/use are just labels that are converted to addresses by the compiler when building a static executable, so the running code has no way to look up things by variable name.

A byte has 8-bits. Can it be larger in another system? give an example [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
A byte has 8-bits. Can it be larger in another system? Give an example.
Also, how many different types of calls such as writeint,writedec,writestring,writechar are there in assembly language.
Thanks.
Not any more. There was a time, yes, when there were systems without a fundamental 8-bit byte.
System where 1 byte != 8 bit?
how many different types of call such as writeint,writedec,writestring,writechar are there in assembly language
This question makes no sense. Assembly language is just a means of writing code that translates directly to machine instructions. call is just one of these instructions - it jumps to some other section of code, with the intent of returning to the place where the call was made.
The things you're referring to sound more like library routines - in which case there are any number of them, depending on the programming environment.

Resources