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
Could someone explain the use of the asterisk * in C and how it is used?
what does "|=" operator mean in C? - Stack Overflow
What does the |= operator mean in C++? - Stack Overflow
syntax - What is the meaning of '==' in C? - Stack Overflow
Videos
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!
It works like the | + the = operator, in a similar way as += works.
It is equivalent as
a = a|b;
I suggest you to read this article about operators: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Bitwise_operators Ans this one about bitwise operation http://en.wikipedia.org/wiki/Bitwise_operation
Following the pattern of, for example, +=:
a |= b;
// Means the same thing as:
a = a | b;
That is, any bits that are set in either a or b shall be set in a, and those set in neither shall not be set in a.
Assuming you are using built-in operators on integers, or sanely overloaded operators for user-defined classes, these are the same:
a = a | b;
a |= b;
The '|=' symbol is the bitwise OR assignment operator. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.
The big advantage of the '|=' operator is when 'a' is itself a complex expression:
something[i].array[j]->bitfield |= 23;
vs:
something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23;
Was that difference intentional or accidental?
...
Answer: deliberate - to show the advantage of the shorthand expression...the first of the complex expressions is actually equivalent to:
something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23;
Similar comments apply to all of the compound assignment operators:
+= -= *= /= %=
&= |= ^=
<<= >>=
Any compound operator expression:
a XX= b
is equivalent to:
a = (a) XX (b);
except that a is evaluated just once. Note the parentheses here - it shows how the grouping works.
x |= y
same as
x = x | y
same as
x = x [BITWISE OR] y
== is a test for equality. = is an assignment.
Any good C book should cover this (fairly early on in the book I would imagine).
For example:
int i = 3; // sets i to 3.
if (i == 3) printf("i is 3\n"); // prints it.
Just watch out for the heinous:
if (i = 4) { }
which is valid C and frequently catches people out. This actually assigns 4 to the variable i and uses that as the truth value in the if statement. This leads a lot of people to use the uglier but safer:
if (4 == i) {}
which, if you accidentally use = instead of ==, is a compile-time error rather than something that will bite you on the backside while your program is running :-)
The logical-or operator is two vertical bar characters, one after the other, not a single character. Here it is lined up with a logical-and, and a variable called b4:
||
&&
b4
No magic there.
a == b is a test if a and b are equal.
a = b is called an assignment, which means to set the variable a to having the same value as b.
(You type | with Shift-\ in the US keyboard layout.)
These operators were borrowed from C into many other languages. C inherited & and | from B, where they’re used for bitwise operations by default, or for logical operations when a truth value is expected. For example, to quote “Users’ Reference to B on MH-TSS”, §12.0:
The use of the operator
&in conditional statements has been optimized to allow for conditional transfers and much more efficient object code; unfortunately, there is a conflict between this use and the use of this operator in its more usual sense as a bitwise logical operator. Thus, the statement:if( a&077 )goto label;is ambiguous; on the one hand, it could mean “if the last two octal digits of a are not 00, go to label” (
&used as a bitwise operator), while on the other hand it could mean “if bothaand077are nonzero, go to label” (&as a logical operator). This ambiguity is broken by always assuming the logical operator in a case where a truth value is expected (if,while, and conditional expression). To force the bitwise interpretation in the above example, one must writeif( (a&077) != O )goto label;which is in fact optimized well by the code generator.
B inherited this from BCPL, for example “The BCPL Cintsys and Cintpos User Guide” ch. 2:
Expressions of the form:
E1&E2andE1|E2return, respectively, the bitwise AND or OR of their operands unless the expression is being evaluated in a boolean context such as the condition in awhilecommand, in which case the operands are tested from from left to right until the value of the condition is known.
In C, the operators evaluate both their operands eagerly, and && and || were added to be explicit about the laziness in the second operand. From “The Development of the C Language”, under “Neonatal C”:
Rapid changes continued after the language had been named, for example the introduction of the
&&and||operators. In BCPL and B, the evaluation of expressions depends on context: withinifand other conditional statements that compare an expression’s value with zero, these languages place a special interpretation on the and (&) and or (|) operators. In ordinary contexts, they operate bitwise, but in the B statementif (e1 & e2) ...the compiler must evaluate
e1and if it is non-zero, evaluatee2, and if it too is non-zero, elaborate the statement dependent on theif. The requirement descends recursively on&and|operators withine1ande2. The short-circuit semantics of the Boolean operators in such ‘truth-value’ context seemed desirable, but the overloading of the operators was difficult to explain and use. At the suggestion of Alan Snyder, I introduced the&&and||operators to make the mechanism more explicit. Their tardy introduction explains an infelicity of C’s precedence rules. In B one writesif (a==b & c) ...to check whether
aequalsbandcis non-zero; in such a conditional expression it is better that&have lower precedence than==. In converting from B to C, one wants to replace&by&&in such a statement; to make the conversion less painful, we decided to keep the precedence of the&operator the same relative to==, and merely split the precedence of&&slightly from&. Today, it seems that it would have been preferable to move the relative precedences of&and==, and thereby simplify a common C idiom: to test a masked value against another value, one must writeif ((a&mask) == b) ...where the inner parentheses are required but easily forgotten.
Using a vertical bar for logical “OR” was already well established before BCPL (1967), notably in PL/I (1964); as a bit of trivia, this is why the ASCII character is styled as a solid line, rather than a broken line as it was originally.
An initial draft for a 7-bit character set that was published by the X3.2 subcommittee for Coded Character Sets and Data Format on June 8, 1961 […] received opposition from the IBM user group SHARE, with its chairman, H. W. Nelson, writing a letter to the American Standards Association titled “The Proposed revised American Standard Code for Information Interchange does NOT meet the needs of computer programmers!”; in this letter, he argues that no characters within the international subset designated at columns 2-5 of the character set would be able to adequately represent logical OR and logical NOT in languages such as IBM’s PL/I universally on all platforms.
I’m not sure if PL/I was the first mainstream language to use this character with this meaning, but in any case, many of its predecessors (Algol, COBOL, and Fortran) did not.
In a comment, Michael Homer mentioned that | was used for "or" as metalanguage in the Algol 60 report. The Algol 60 report is from 1960, earlier than PL/I (1964) mentioned in Jon Purdy's comprehensive answer. The | symbol for "or" in this report is part of Backus-Naur form (which was first used in the Algol 60 report). Backus originally used the word "or" with a bar over it, and Naur is the one to change this notation to |. So Naur appears to be the originator of | for "or."
Naur's choice may have been influenced by the Sheffer stroke which Sheffer in 1913 used as a symbol for NOR, and which was later (1917) misinterpreted and became widely used, as it is today, as a symbol for NAND. The second edition of Principia Mathematica (1927) helped popularize this usage of | for NAND.
str_var = "A string" count = 0 for c in str_var: count += 1 print(count)
can anyone explain what the "c" stands for in this? Does it stand for character? (Code came from a learning software I am using)
In a declaration, it means it's a pointer to a pointer:
int **x; // declare x as a pointer to a pointer to an int
When using it, it deferences it twice:
int x = 1;
int *y = &x; // declare y as a pointer to x
int **z = &y; // declare z as a pointer to y
**z = 2; // sets the thing pointed to (the thing pointed to by z) to 2
// i.e., sets x to 2
It is pointer to pointer.
For more details you can check: Pointer to pointer
It can be good, for example, for dynamically allocating multidimensional arrays:
Like:
#include <stdlib.h>
int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
exit or return
}
for(i = 0; i < nrows; i++)
{
array[i] = malloc(ncolumns * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr, "out of memory\n");
exit or return
}
}