Understanding functions and pointers in C -
This is a very simple question, but what does the following function prototype mean?
int square (int y, size_t * x)
What size does _T * mean? I know size_t is a data type (int> = 0) but how do I attach it? Is this an indicator on the memory space for x? Normally I am having problems with this stuff, and if someone can provide easy reference, I would appreciate it.
Thanks everyone, I understand what an indicator is, but I think it is a difficult time to understand the relationship between pointers and functions. When I define a function prototype as int sq (int x, int y)
, then it is very clear to me what is going on. However, when I see something like int sq (int x, int * y)
I can not do - for my life - understand what the second parameter really means. At some level, I understand that it means "pass an indicator" but I can not understand enough things to manipulate it on my own.
How about tutorials?
However in this case, the pointer is probably used to modify / return the value. In C, there are two basic mechanisms in which the function can return a value (please forgive the dumb example):
This can return the value directly:
Float square_root (float x) {if (x> = 0) return sqrt (x); Return 0; }
or it can be returned by a pointer:
int square_root (float x, float * result) {if (x> gt = =) {* Result = sqrt (result); Return 1; } Return 0; }
The first name is given:
float a = squared_root (12.0);
... while later:
float b; Class_source (12.00, and b); Note that the example of the latter will also allow you to check whether the returned value was real - this system is widely used in c libraries, where the return value of the function Usually demonstrates success (or lack thereof), while prices are returned through parameters themselves. So you can later write:
float sqresult; If (! SquareRoot (Myvar, & amp; sqresult)) {// signal error} other {// value is good, continue using sqresult! }
Comments
Post a Comment