Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the * and the & operators in c programming?

Tags:

I am just making sure I understand this concept correctly. With the * operator, I make a new variable, which is allocated a place in memory. So as to not unnecessarily duplicate variables and their values, the & operator is used in passing values to methods and such and it actually points to the original instance of the variable, as opposed to making new copies...Is that right? It is obviously a shallow understanding, but I just want to make sure I am not getting them mixed up. Thanks!

like image 591
Wesley Avatar asked Apr 23 '10 21:04

Wesley


People also ask

What is the difference between on the and in the?

'In' is a preposition, commonly used to show a situation when something is enclosed or surrounded by something else. 'On' refers to a preposition that expresses a situation when something is positioned above something else. Months, Years, Season, Decades and Century. Days, Dates and Special Occasions.

How do you use by the end?

"By the end" is used to say that something was happening throughout a story or event, and finished happening before the story or event ended. I hope this helps.

What is the difference between in the way and on the way?

"In the way" means that something is an obstacle. For example, "I can't move my car because that truck is in the way". "On the way" means that something, or someone, is in the process of reaching a goal, or a destination. For example, "I called her to let her know I was on my way to Madrid".


1 Answers

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.
}
like image 52
Steve Jessop Avatar answered Oct 28 '22 00:10

Steve Jessop