Difference between - and . in C
Basic difference is this:
. is the member of a structure
-> is the member of a POINTED TO structure
So the .
is used when there is a direct access to a variable in the structure. But the ->
is used when you are accessing a variable of a structure through a pointer to that structure.
Say you have struct a
:
struct a{
int b;
}
However say c
is a pointer to a
then you know that: c = (*a)
. As a result ->
is being used as a alternative for a certain case of the .
. So instead of doing (*a).b
, you can do c->b
, which are the exact same.
What is the difference between = and |= in C?
Do the two lines have same effect of writing one to data register B ?
No.
DDRB = 0b00000001;
writes one to bit-0 and zero to bits 1 to 7, while:
DDRB |= 0b00000001;
is a read-modify-write operation equivalent to:
DDRB = DDRB | 0b00000001;
so it writes a one to bit-0 as before and leaves all other bits unchanged.
So for example if DDRB
's current value were 0b11110000
after:
DDRB = 0b00000001;
it will be 0b00000001
while after:
DDRB |= 0b00000001;
it will be 0b11110001
.
So one sets DDRB to 1, while the other sets DDRB:Bit-0 to 1.
Difference between using and ' in C
The symbol '
is used to specify an integer character constant. For example 'A'
is an integer character constant that has the type int
.
The symbol "
is used to specify a string literal. For example "A"
is a string literal that has the type char[2]
. That is the string literal is a character array that is stored like
{ 'A', '\0' }
String literals contain sequences of characters terminated by the zero terminating character '\0'
that also is stored in string literals.
Character arrays designators used in expressions are implicitly (with rare exceptions) converted to pointers to their first elements of the type char *
or const char *
.
This means that in this assignment statement
grade="A";
the string literal "A"
that has the array type char[2]
is converted to a pointer to its first element 'A'
. As a result the compiler issues a message that you are trying to assign a pointer to an object of an integer type.
You can write for example
grade = "A"[0];
or
grade = *"A";
But this will only confuse readers of the code because it will be more simpler and clear to write
grade = 'A';
That is there is no need to use a whole array like "A"
(remember that it internally represents an array of two elements { 'A', '\0' }
) to use only its one element 'A'
.
What is the difference between the * and the & operators in c programming?
Not quite. You're confusing a *
appearing in a type-name (used to define a variable), with the *
operator.
int main() {
int i; // i is an int
int *p; // this is a * in a type-name. It means p is a pointer-to-int
p = &i; // use & operator to get a pointer to i, assign that to p.
*p = 3; // use * operator to "dereference" p, meaning 3 is assigned to i.
}
Difference between - and . in C++
In this case you can avoid the problem by setting the values directly in the initialiser list.
class SessionController {
private: SessionView view;
private: Session model;
public: SessionController (SessionView view, Session model) : view(view), model(model) {
}
};
Or if you really need to do it in the body.
class SessionController {
private: SessionView view;
private: Session model;
public: SessionController (SessionView pview, Session pmodel) {
view = pview;
model = pmodel;
}
};
Related Topics
Does Std::Cout Have a Return Value
Find Two Elements in an Array That Sum to K
Difference Between | and || , or & and &&
Output Redirection Using Fork() and Execl()
Std::Remove_If - Lambda, Not Removing Anything from the Collection
Why Can't We Declare Object of a Class Inside the Same Class
How to Avoid Errors While Using Crtp
When Is C++ Covariance the Best Solution
Is There a Standard Date/Time Class in C++
Two Classes That Refer to Each Other
How to Run the Preprocessor on Local Headers Only
Is Using Unsigned Integer Overflow Good Practice
Piping (Or Command Chaining) with Qprocess
Array of Pointers as Function Parameter
Ways to Detect Whether a C++ Virtual Function Has Been Redefined in a Derived Class
Why Do We Need to Tie Std::Cin and Std::Cout
How to Legally Reinterpret_Cast Between Layout-Compatible Standard-Layout Types