Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a = &b vs *a = &b — pointer assignment

I have a pointer and a variable:

int *a;
int b;

Is there any difference between assignments

a = &b;

and

*a = &b;

and what are they called (like pointer declaration or something) ?

like image 914
Sumit Ramteke Avatar asked Jan 02 '17 09:01

Sumit Ramteke


People also ask

How old is the letter A?

The letter A is derived from the Phoenician letter aleph—a western Semitic word referring to the aforementioned beast of burden. Aleph can be traced back to the Middle Bronze Age and the Proto-Sinaitic script found in parts of Egypt and Canaan from around 1850 BCE (Before the Common Era).

Why are there 2 types of A?

“The TL;DR is that it's basically a historical accident: There were loads of variations of the letter 'a' and one became standard in printing while a less fancy one became standard in handwriting, presumably because people are lazy when they have to do things by hand,” writes Reddit user F0sh.

What is the symbol for letter A?

Ɑ ɑ : Latin letter alpha / script A, which represents an open back unrounded vowel in the IPA.

Is A letter A character?

Examples of characters include letters, numerical digits, common punctuation marks (such as "." or "-"), and whitespace. The concept also includes control characters, which do not correspond to visible symbols but rather to instructions to format or process the text.


2 Answers

Types matter.

  • In case of a=&b, the assignment is valid. You're assigning the address of an integer (type: int *), to another variable of type int *, so this is legit.

  • In case of *a=&b, this is a constraint violation (for assignement operator, see chapter §6.5.16.1/p1, Constraints, for Simple assignment) and thus not a valid C syntax, thus not required to be compiled by any conforming compiler. To make it a valid C syntax, we need to enforce a typecast, something like

    *a= (int) &b;
    

    would make it a syntactically valid C statement which meets the required constraint.

    Even, after that, the result is implementation defined.#note Here, you're basically trying to assign the the address of an integer (type: int *) to another variable of type int (*a is of type int). Conversion from a pointer to integer is implementation defined behaviour.

    Quoting C11, chapter §6.3.2.3, Pointers

    Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. [....]

[....] And what are they called?

They both are assignment statements.


Note:

Considering a points to a valid memory location already. Otherewise, dererefencing an invalid pointer invokes undefined behavior on it's own.

like image 82
Sourav Ghosh Avatar answered Oct 22 '22 13:10

Sourav Ghosh


Pay attention to types on the left and on the right of =.

&b is int *, a is also int * but *a is int.


It's a bit confusing that * has different meanings:

int *a; — here * means that a will be a pointer;

*a = ...; — here * means that we change not address stored in a but value which is located at the address.


So a = &b means "write the address of b to a",

but *a = &b means "write the address of b to *a, i.e. to the address which is stored in a".


Let's suggest that we have this situation:

   Address  Value
 a 0x0001   0x0004
 b 0x0002   70
   0x0003   80
   0x0004   90

At the moment a is 0x0004 and *a is 90.

If you do a = &b, a will be 0x0002 and *a will be 70.

But if you do *a = &b, a will not change, but *a, i.e. value at the address 0x0004, will change to 0x0002.

like image 34
Pavel Avatar answered Oct 22 '22 15:10

Pavel