An Overview

All the necessary study material for b-tech students.

What we have to offer you.

Codes,Syllabus,Study material,Lab manuals,Assignments and many more . . .

Surfing becomes easy.

You can have any material from our website and post your valuable comments for further improvement.

Get the latest techno news

Get yourself updated with this fast changing dynamic world and all the changes in b-tech pattern.

All the best resources for you.

We have done our level best to provide you with the best of the degree and thus this site is devoted to b-tech exclusively.

Showing posts with label coding basics. Show all posts
Showing posts with label coding basics. Show all posts

10 January 2012

Storing the Address in a Pointer


Every variable has an address. Even without knowing the specific address of a given Variable, you can store that address in a pointer. For example, suppose that the variable howOld is an integer. To declare a pointer called pAge to hold its address, you write the following statement:

int *pAge = NULL;

This declares pAge to be a pointer to int. That is, pAge is declared to hold the address of an int. Pointers can have any name that is legal for other variables. Here we follow the convention of naming all pointers with an initial p and a second letter capitalized, as in pAge.

Note that pAge is a variable like any other variable. When you declare an integer variable, it is set up to hold an integer. When you declare a pointer variable like pAge, it is set up to hold an address. A pointer is just a special type of variable that holds the address of an object in memory; in this case, pAge is holding the address of an integer variable. You declare the type of variable you want the pointer to point to. This tells the compiler how to treat the memory at the location the pointer points to. The pointer itself contains an address.
What actually happens inside

In this example, pAge is initialized to NULL. A pointer whose value is NULL is called a null pointer. All pointers, when they are created, should be initialized to something. If you don’t know what you want to assign to the pointer, assign NULL. A pointer that is not initialized is called a wild pointer. Wild pointers are dangerous. If you initialize the pointer to 0 or NULL, you must specifically assign the address of howOld to pAge. Here’s code that shows how to do that:
The result should be the same as if you initialized it to NULL, but technically 0 is an integer constant, and NULL is an address constant of 0. The next version of C, C++, has a new nullptr constant that represents a null pointer. When your C++ compiler supports this new version, use nullptr instead of 0 or NULL.


int howOld = 50;
int *pAge = 0;
pAge = &howOld;

// make a variable
// make a pointer
// put howOld’s address in pAge

The first line creates a variable—howOld, whose type is unsigned short int—and initializes it with the value 50. The second line declares pAge to be a pointer to type unsigned short int and initializes the address to 0. You know that pAge is a pointer because of the asterisk (*) after the variable type and before the variable name. The third and final line assigns the address of howOld to the pointer pAge. You can tell that the address of howOld is being assigned to the pointer because of the address of operator &. If the address of operator was not used, the value of howOld would be assigned instead of its address. That value might be a valid address somewhere in memory, but that would be entirely a coincidence. Assigning a nonpointer to a pointer variable is a common error. Fortunately, the compiler will detect this and fail with an “invalid conversion” error. At this point, pAge has as its value the address of howOld. howOld, in turn, has the value 50. So, it represents the basic idea about storing the address in a pointer.

6 January 2012

Basic terms before learning coding


Compound Statements

Several statements can be grouped together as a compound statement, which begins
with an opening brace { and ends with a closing brace }. A compound statement can
appear anywhere a single statement could.

Although every statement in a compound statement must end with a semicolon, the
compound statement itself does not end with a semicolon. Here’s an example:
{
temp = a;
a = b;
b = temp;
}

This compound statement swaps the values in the variables a and b using a variable
named temp as a temporary holding place for one value.

Expressions

An expression is any part of a statement that returns a value, as in this simple example:
x = y + 13;

This statement makes the variable x equal to the variable y plus 13. So, if y equals
20, x equals 33. The entire statement also returns the final value of x, so it’s also an
expression. To understand this better, consider a more complex statement:
z = x = y + 13;

This statement consists of three expressions:
 The expression y 
+ 13 is stored in the variable x.

. The expression x
able z.
. The expression z



= y + 13 returns the value of x, which is stored in the vari-


= x = y + 13 returns the value of z, which is not stored.


The assignment operator = causes the operand on the left side of the operator to have
its value changed to the value on the right side of the operator.
Operand is a mathematical term referring to the part of an expression operated upon
by an operator.







The Full Text of Expression.cpp


#include <iostream>
int main()
{
int x = 0, y = 72, z = 0;
std::cout << “Before\n\nx: “ << x << “ y: “ << y;
std::cout << “ z: “ << z << “\n\n”;
z = x = y + 13;
std::cout << “After\n\nx: “ << x << “ y: “ << y;
std::cout << “ z: “ << z << “\n”;
return 0;
}

 This program produces the following output:
Before
x: 0 y: 72 z: 0

After
x: 85 y: 72 z: 85
Three variables are declared and given initial values, which are displayed on lines
5–6. In line 7, expressions assign values to x and z, in that order. The new values are
displayed in lines 8–9.


 Operators

An operator is a symbol that causes the compiler to take an action such as assigning
a value or performing multiplication, division, or another mathematical operation.

Assignment Operator
An expression consists of an assignment operator, an operand to its left called an
l-value, and an operand to its right called an r-value. In the expression grade = 95,
the l-value is grade, and the r-value is 95.

Constants are r-values but cannot be l-values. The expression 95 = grade is not per-
mitted in C++ because the constant 95 cannot be assigned a new value.

The primary reason to learn the terms l-value and r-value is because they may
appear in compiler error messages.


Mathematical Operators
There are five mathematical operators: addition (+), subtraction (-), multiplication
(*), division (/), and modulus (%). C++, like C, does not have an exponentiation oper-
ator to raise a value to a specified power. There is a function to perform the task.
Addition, subtraction, and multiplication act as you’d expect, but division is more
complex.

Integer division differs from ordinary division. When you divide 21 by 4, the result is
a real number that has a fraction or decimal value. By contrast, integer division pro-
duces only integers, so the remainder is dropped. The value returned by 21 / 4 is 5.

The modulus operator % returns the remainder value of integer division, so 21 % 4
equals 1. The integer division 21 / 4 is 5, leaving a remainder of 1.

Finding the modulus can be useful in programming. If you want to display a state-
ment every 10th time that a task is performed, the expression taskCount % 10 can
watch for this. The modulus ranges in value from 0 to 10. Every time it equals 0, the
count of tasks is a multiple of 10.

Floating-point division is comparable to ordinary division. The expression 21 / 4.0
equals 5.25.
 C++ decides which division to perform based on the type of the operands. If at least
one operand is a floating-point variable or literal, the division is floating point. Oth-
erwise, it is integer division.