<< Chapter < Page Chapter >> Page >
iPtr =&iVar; // Let iPtr point to the variable iVar.

In a pointer declaration, the asterisk (*) is part of an individual declarator. We can thus define and initialize the variables iVar and iPtr in one declaration, as follows:

int iVar = 77, *iPtr =&iVar; // Define an int variable and a // pointer to it.

The second of these two declarations initializes the pointer iPtr with the address of the variable iVar, so that iPtr points to iVar. Figure 4.1. illustrates one possible arrangement of the variables iVar and iPtr in memory. The addresses shown are purely fictitious examples. As Figure 4.1. shows, the value stored in the pointer iPtr is the address of the object iVar.

A pointer and another object in memory

It is often useful to output addresses for verification and debugging purposes. The printf() functions provide a format specifier for pointers: %p. The following statement prints the address and contents of the variable iPtr:

printf("Value of iPtr (i.e. the address of iVar): %p\n" "Address of iPtr: %p\n", iPtr,&iPtr);

The size of a pointer in memory given by the expression sizeof(iPtr)

&And * operators

The unary operator&gives the address of an object, so the statement

p =&c;

assigns the address of c to the variable p, and p is said to “point to” c. The&operator only applies to objects in memory: variables and array elements. It cannot be applied to expressions, constants, or register variables.

* is the operator that retrieves the value stored at the address held in the pointer. The indirection operator * yields the location in memory whose address is stored in a pointer. If ptr is a pointer, then *ptr designates the object that ptr points to. Using the indirection operator is sometimes called dereferencing a pointer. The type of the pointer determines the type of object that is assumed to be at that location in memory. For example, when you access a given location using an int pointer, you read or write an object of type int.

The indirection operator * is a unary operator; that is, it has only one operand.ptr points to the variable x. Hence the expression *ptr is equivalent to the variable x itself.

double x, y, *ptr; // Two double variables and a pointer to double. ptr =&x; // Let ptr point to x. *ptr = 7.8; // Assign the value 7.8 to the variable x.*ptr *= 2.5; // Multiply x by 2.5. y = *ptr + 0.5; // Assign y the result of the addition x + 0.5.
Got questions? Get instant answers now!

Do not confuse the asterisk (*) in a pointer declaration with the indirection operator. The syntax of the declaration can be seen as an illustration of how to use the pointer.

double *ptr;

As declared here, ptr has the type double * (read: "pointer to double"). Hence the expression *ptr would have the type double.

Of course, the indirection operator * must be used with only a pointer that contains a valid address. This usage requires careful programming! Without the assignment ptr =&x in the listing above, all of the statements containing *ptr would be senseless dereferencing an undefined pointer value and might well cause the program to crash.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask