Reputation:
I am learning C language and doing a lot of practice. I understand that *
and **
are pointers in .c
function.
Please consider the following function (another .c
function)
void pcc(int* n, int* d, int* family, int* type, double* par, double* nu, double* out)
{
int i, j, in=1, k, **fam;
double *w, **v, t, **theta, **x, **ny;
…some works
}
My question is, why do we use the pointer in the argument of the function? Because as I understand we use the pointer to point the previous identifed elements.
Also, why do we use pointer for some elements that are not already defined in the arguments of the function. For example, in the last function we define a new element after writing the argument of the function:
int i, j, in=1, k, **fam;
double *w, **v, t, **theta, **x, **ny;
For instance, double *w
was not in the argument of the function! also **v
and other elements. How do we know they must get **
or *
.
Any help please?
Upvotes: 0
Views: 89
Reputation: 823
In C you traditionally use pointers to modify variables that were declared at a different scope than the one of your function.
Pointers to pointers (**
) though, are usually used when you want to manipulate those addresses in some way.
Now as for you question:
How do we know they must get
**
or*
?
Well it depends on what you want to do with the data. If you want to manipulate the value you only need a pointer; if you want to manipulate the address you may need a pointer to a pointer.
Upvotes: 0
Reputation: 46
Pointers are used when we want the location of a value, as opposed to just the value. Now why would we want this? One simple scenario is actually illustrated in your question. In c, it is often inconvenient (if not impossible) to return more than one value from a function. An easy way to get around that is with pointers! When you pass arguments into a function normally, for example
int myFunction(int some_parameter);
you are actually passing a copy of some_parameter into your function. When the function returns, the original value of some_parameter is unchanged. But what if we want to change some_parameter in the function? We pass a pointer to the value instead. This allows us to directly change the value of some_parameter at its original memory location, instead of a copy. So as you can see, if we instead had
void myFunction(int* parameter1, int* parameter2) {
*parameter1 = 1;
*parameter2 = 2;
}
When you exit the function, your values parameter1 and parameter 2 will be 1 and 2 respectively, even though you never actually returned a value! Pointers are one of the most useful and difficult parts about c, and I recommend you consult the other sources that others have selected in order to learn more about them.
Upvotes: 0
Reputation: 307
The reasons for declaring a variable a * or ** are countless, it's better to try and get a grip of what they actually mean instead of getting hung up on why they're used in this specific instance.
A pointer * is a reference to a memory location where a value is stored. In the case of a char it is a reference to a single memorylocation holding a byte, and in the case of an int it's usually 4 bytes on a 32bit system.
You can pass a pointer as an argument to indicate this is where you want the result to be stored. Another reason might be efficiency. If you have a very large struct it's better to pass a 4byte (on a 32bit system) reference to the area of memory where the struct lies instead of loading the entire struct on the stack.
A ** is a double pointer. It is pointing to a specific memory location that itself is storing the memory location of something else.
char a[5];
Here, the variable a is actually a char * pointing at the first element of a[], namely a[0].
char a[5][5];
Here a is a char **, pointing to the first element of a[][], namely a[0], which will return a char * pointing at a[0][0].
The second example is better when you want to fiddle with the pointers. Maybe you have a char** (an 'Array of strings'), and you want to replace one of the 'strings' (a string is just a char[] in c), you simply change the char* pointed to by the char**).
I hope this makes it a little clearer for you.
Upvotes: 1