Difference between -> and . in a struct?

If I have a struct like

struct account {
   int account_number;
};

Then what's the difference between doing

myAccount.account_number;

and

myAccount->account_number;

or isn't there a difference?

If there's no difference, why wouldn't you just use the . notation rather than ->? -> seems so messy.

-> is a shorthand for (*x).field, where x is a pointer to a variable of type struct account, and field is a field in the struct, such as account_number.

If you have a pointer to a struct, then saying

accountp->account_number;

is much more concise than

(*accountp).account_number;

What's the difference between a pandemic, an epidemic, endemic , Learn the differences between colds and flu - CDC. have similar symptoms, it can be difficult to tell the difference between them based on symptoms alone. Difference Between Recession and Deflation (Comments Off on Difference Between Recession and Deflation). The state of economic activities is not constant. Changes in pricing, government policies, as well as consumer preference, can negatively or positively affect economic activities.

You use . when you're dealing with variables. You use -> when you are dealing with pointers.

For example:

struct account {
   int account_number;
};

Declare a new variable of type struct account:

struct account s;
...
// initializing the variable
s.account_number = 1;

Declare a as a pointer to struct account:

struct account *a;
...
// initializing the variable
a = &some_account;  // point the pointer to some_account
a->account_number = 1; // modifying the value of account_number

Using a->account_number = 1; is an alternate syntax for (*a).account_number = 1;

I hope this helps.

Cold Versus Flu, George Alba, MD, helps explain the difference between N95s, surgical masks and cloth masks as well as best practices according to CDC  Define vs. vs synonyms, vs pronunciation, vs translation, English dictionary definition of vs. abbr. veterinary surgeon abbreviation for versus abbreviation for

You use the different notation according to whether the left-hand side is a object or a pointer.

// correct:
struct account myAccount;
myAccount.account_number;

// also correct:
struct account* pMyAccount;
pMyAccount->account_number;

// also, also correct
(*pMyAccount).account_number;

// incorrect:
myAccount->account_number;
pMyAccount.account_number;

The Difference Between N95s, Surgical Masks and Cloth Masks , Well-planned online learning experiences are meaningfully different from courses offered online in response to a crisis or disaster. Colleges  45 synonyms of difference from the Merriam-Webster Thesaurus, plus 87 related words, definitions, and antonyms. Find another word for difference. Difference: the quality or state of being different.

-> is a pointer dereference and . accessor combined

The Difference Between Emergency Remote Teaching and Online , What's the difference? Bacteria and viruses can cause many common infections. But what are the differences between these two kinds of  The key difference between archenteron and blastocoel is that archenteron is the primary gut formed during gastrulation in the developing zygote, which later develops into the digestive tube, while blastocoel is an inner fluid-filled or yolk-filled cavity of the blastula formed during blastulation. Fertilization is the event of sexual

If myAccount is a pointer, use this syntax:

myAccount->account_number;

If it's not, use this one instead:

myAccount.account_number;

What's the Difference Between Bacterial and Viral , Learn about the differences between COVID-19 (Coronavirus) tests: Molecular (​Swab) Test, Antibody (Serology) Test, Antibody Tests for IgG and Antibody Test  Define difference. difference synonyms, difference pronunciation, difference translation, English dictionary definition of difference. disparity; unlikeness; distinction: made a difference Not to be confused with: deference – courteous respect for another’s opinion, wishes, or judgment:

The Difference between Tests for COVID-19 (Coronavirus), What is the difference between Alzheimer's and dementia? Get an overview of each and learn about early symptoms, risk factors, diagnosis and treatment. There are two different uses. If you are using double quotation marks for speech, as in a novel or other quotation, then, anything quoted within the quotation marks takes single quotes: "He yelled 'Stop talking' at me several times," said John. The BBC uses single quotation marks in headlines and its online site (inverted commas) but in novels

Dementia vs. Alzheimer's Disease: What Is the Difference?, What is the difference between isolation and quarantine? Isolation and quarantine are public health practices used to protect the public by preventing exposure  Which vs. That: What’s the Difference in Usage? To understand when to use that or which, it’s important to understand clauses.A defining clause (also called an essential clause or a restrictive clause) gives information essential to the meaning of the sentence.

What is the difference between isolation and quarantine?, This chart compares them side by side to help you understand the differences. IEP, 504 Plan. Basic Description. A blueprint or plan  PINK is a college girl’s must-shop destination for the cutest bras, panties, swim and loungewear!

Comments
  • You should explain your answer.