It declares a pointer to a char pointer.
The usage of such a pointer would be to do such things like:
void setCharPointerToX(char ** character) {
*character = "x"; //using the dereference operator (*) to get the value that character points to (in this case a char pointer
}
char *y;
setCharPointerToX(&y); //using the address-of (&) operator here
printf("%s", y); //x
Here's another example:
char *original = "awesomeness";
char **pointer_to_original = &original;
(*pointer_to_original) = "is awesome";
printf("%s", original); //is awesome
Use of ** with arrays:
char** array = malloc(sizeof(*array) * 2); //2 elements
(*array) = "Hey"; //equivalent to array[0]
*(array + 1) = "There"; //array[1]
printf("%s", array[1]); //outputs There
The [] operator on arrays does essentially pointer arithmetic on the front pointer, so, the way array[1] would be evaluated is as follows:
array[1] == *(array + 1);
This is one of the reasons why array indices start from 0, because:
array[0] == *(array + 0) == *(array);
Answer from Jacob Relkin on Stack OverflowIt declares a pointer to a char pointer.
The usage of such a pointer would be to do such things like:
void setCharPointerToX(char ** character) {
*character = "x"; //using the dereference operator (*) to get the value that character points to (in this case a char pointer
}
char *y;
setCharPointerToX(&y); //using the address-of (&) operator here
printf("%s", y); //x
Here's another example:
char *original = "awesomeness";
char **pointer_to_original = &original;
(*pointer_to_original) = "is awesome";
printf("%s", original); //is awesome
Use of ** with arrays:
char** array = malloc(sizeof(*array) * 2); //2 elements
(*array) = "Hey"; //equivalent to array[0]
*(array + 1) = "There"; //array[1]
printf("%s", array[1]); //outputs There
The [] operator on arrays does essentially pointer arithmetic on the front pointer, so, the way array[1] would be evaluated is as follows:
array[1] == *(array + 1);
This is one of the reasons why array indices start from 0, because:
array[0] == *(array + 0) == *(array);
C and C++ allows the use of pointers that point to pointers (say that five times fast). Take a look at the following code:
char a;
char *b;
char **c;
a = 'Z';
b = &a; // read as "address of a"
c = &b; // read as "address of b"
The variable a holds a character. The variable b points to a location in memory that contains a character. The variable c points to a location in memory that contains a pointer that points to a location in memory that contains a character.
Suppose that the variable a stores its data at address 1000 (BEWARE: example memory locations are totally made up). Suppose that the variable b stores its data at address 2000, and that the variable c stores its data at address 3000. Given all of this, we have the following memory layout:
MEMORY LOCATION 1000 (variable a): 'Z'
MEMORY LOCATION 2000 (variable b): 1000 <--- points to memory location 1000
MEMORY LOCATION 3000 (variable c): 2000 <--- points to memory location 2000
pointers - In C, what does double asterisk ** in parameters of a function do - Stack Overflow
Could someone explain the use of the asterisk * in C and how it is used?
history - Why does C use the asterisk for pointers? - Software Engineering Stack Exchange
Why do we need two asterisks when we wan - C++ Forum
Videos
If you have a variable like for example
int x = 10;
and want to change it in a function you need to pass it to the function by reference.
In C passing by reference means passing a variable indirectly through a pointer to it. Thus dereferencing the pointer within the function you have a direct access to the original variable and can change it.
Here is a demonstration program.
#include <stdio.h>
void f( int *px )
{
*px = 20;
}
int main( void )
{
int x = 10;
printf( "Before calling f x = %d\n", x );
f( &x );
printf( "After calling f x = %d\n", x );
}
The program output is
Before calling f x = 10
After calling f x = 20
If a variable has a pointer type like for example
node_t *head = NULL;
then again if you are going to change its value in a function you need to pass it to the function by reference as for example
push( &head, 10 );
So the corresponding function parameter will be declared like
void push(node_t ** head, int val);
And dereferencing the pointer to the original pointer head like
*head = new_node;
you can change the original pointer head defined in main.
If you will not pass the pointer by reference but pass it by value then the function will deal with a copy of the value of the original pointer. Changing the copy does not influence on the original pointer.
Consider the preceding demonstration program where the original variable is passed to the function by value
#include <stdio.h>
void f( int x )
{
x = 20;
}
int main( void )
{
int x = 10;
printf( "Before calling f x = %d\n", x );
f( x );
printf( "After calling f x = %d\n", x );
}
In this case the original variable x will not be changed. It is a copy of its value that was changed within the function. So the program output will be
Before calling f x = 10
After calling f x = 10
In this case, it declares a pointer to a node_t pointer, which is a function parameter.
Here is a simple example using character pointers:
char *charPointer = "This is a text.";
char **pointerToCharPointer = &charPointer;
printf("%s\n", charPointer);
(*pointerToCharPointer) = "This is something new.";
printf("%s\n", charPointer);
Output:
This is a text.
This is something new.
I am currently working on an assignment in C where we are required to make a stack by simply using pointers.
I know the line: int *ptr = &val; declares ptr to be a "pointer to"(which is my interpretation of what asterisk * means in C) the "address of" the integer variable val.
When I want to create a double pointer, or a pointer to a pointer, I do so like:
int **ptr_ptr = &ptr; By setting ptr_ptr to a "pointer to" the address of pointer ptr.
When we use the asterisk anywhere other than in a declaration, it is usually referred to as dereferencing that pointer (I think), and grabbing the value that the pointer actually points to. This goes against my intuition that an asterisk means "pointer to".
Could anybody explain the proper meaning of the asterisk in C? Is it just that it means different things depending on how it is used (i.e. in a declaration versus anywhere else)?
Thanks!
Why does C use the asterisk for pointers?
Simply - because B did.
Because memory is a linear array, it is possible to interpret the value in a cell as an index in this array, and BCPL supplies an operator for this purpose. In the original language it was spelled
rv, and later!, while B uses the unary*. Thus, ifpis a cell containing the index of (or address of), or pointer to) another cell,*prefers to the contents of the pointed-to cell, either as a value in an expression or as the target of an assignment.
From The Development of the C Language
Thats it. At this point, the question is as uninteresting as "why does python 3 use . to call a method? Why not ->?" Well... because Python 2 uses . to call a method.
Rarely does a language exist from nothing. It has influences and is based on something that came before.
So, why didn't B use ! for derefrencing a pointer like its predecessor BCPL did?
Well, BCPL was a bit wordy. Instead of && or || BCPL used logand and logor. This was because most keyboards din't have ∧ or ∨ keys and not equal was actually the word NEQV (see The BCPL Reference Manual).
B appears to have been partially inspired to tighten up the syntax rather than have long words for all these logical operators that programmers did fairly frequently. And thus ! for dereference became * so that ! could be used for logical negation. Note there's a difference between the unary * operator and the binary * operator (multiplication).
Well, what about other options, like
->?
The -> was taken for syntactic sugar around field derefrences struct_pointer->field which is (*struct_pointer).field
Other options like <- could create ambiguous parsings. For example:
foo <- bar
Is that to be read as:
(foo) <- (bar)
or
(foo) < (-bar)
Making a unary operator that is composed of a binary operator and another unary operator is quite likely to have problems as the second unary operator may be a prefix for another expression.
Furthermore, it is again important to try to keep the things being typed frequently to a minimum. I would hate to have to write:
int main(int argc, char->-> argv, char->-> envp)
This also becomes difficult to read.
Other characters might have been possible (the @ wasn't used until Objective C appropriated it). Though again, this goes to the core of 'C uses * because B did'. Why didn't B use @? Well, B didn't use all the characters. There was no bpp program (compare cpp) and other characters were available in B (such as # which was later used by cpp).
If I may hazard a guess as to why - its because of where the keys are. From a manual on B:
To facilitate manipulation of addresses when it seems advisable, B provides two unary address operators,
*and&.&is the address operator so&xis the address ofx, assuming it has one.*is the indirection operator;*xmeans "use the content of x as an address."
Note that & is shift-7 and * is shift-8. Their proximity to each other may have been a hint to the programmer as to what they do... but that's only a guess. One would have to ask Ken Thompson about why that choice was made.
So, there you have it. C is that way because B was. B is that way because it wanted to change from how BCPL was.
I was asked by a student if & and * were chosen because they were next to each other on the keyboard (something I had never noticed before). Much googling led me to B and BCPL documentation, and this thread. However, I couldn't find much at all. It seemed like there were lots of reasons for * in B, but I couldn't find anything for &.
So following @MichaelT's suggestion, I asked Ken Thompson:
From: Ken Thompson < [email protected] >
near on the keyboard: no.
c copied from b so & and * are same there.
b got * from earlier languages - some assembly,
bcpl and i think pl/1.
i think that i used & because the name (ampersand)
sounds like "address." b was designed to be run with
a teletype model 33 teletype. (5 bit baud-o code)
so the use of symbols was restricted.
So I understand pointers. An int * would point to the address of an integer. I understand how you could have a struct pointer and all that. I even sort of understand double pointers. An int ** would be a pointer, pointing to another pointer, which is pointing to an integer. I think that's right but I could be wrong. It's just when I see it in code my brain has a hard time grasping it. I'm looking at a past lab from a course to try to understand it, and I just don't really get it.
So in the lab we were given a struct student, which in itself has two pointer variables among others. We have to read in a file, with the first line containing three integers. The first is the number of courses, C. The next integer is N, which is the number of students per course. In the code, they do fscanf to take in the first few integers. I understand that. Then they allocate memory for courses using calloc.
The line is: student** courses = calloc(*C, sizeof(student*));
This is all inside a function which returns another student**. This is where I get lost. A struct double pointer still is hard for me to grasp. My friend said it's like an array of structs, but I still don't really get it.
Maybe if someone could explain them, or give me a resource that will explain them I would really appreciate it.