Sunday 20 March, 2011

Using Pointers (continues)


int  int1     = 1036;   /* some data to point to  */
int  int2     = 8;
int *int_ptr1 = &int1;  /* get addresses of data  */
int *int_ptr2 = &int2;
int_ptr1 = *int_ptr2;
int_ptr1 = int_ptr2;

Changes happen:
1.Type check warning:  *int_ptr2 is not an int *
2.Changes int_ptr1 – doesn’t change int1

Using Pointers (cont.)


int  int1     = 1036;   /* some data to point to  */
int  int2     = 8;
int *int_ptr1 = &int1;  /* get addresses of data  */
int *int_ptr2 = &int2;
*int_ptr1 = int_ptr2;
*int_ptr1 = int2;

int1 becomes 8
Type check warning:  int_ptr2 is not an int

Using Pointers



int  i1;
int  i2;
int *ptr1;

int *ptr2;
i1 = 1;
i2 = 2;

ptr1 = &i1;//ptr1 points to address 0x1000

ptr2 = ptr1;//ptr2 takes address of ptr1 and so ptr2 points to   address 0x1000

*ptr1 = 3;//value of i1(to which ptr1 pointed becomes 3

i2 = *ptr2;//as i1=3,*ptr2=*ptr1=3,so i2 becomes 3


Pointer Operations in C


Creation
& variable  Returns variable’s memory address
Dereference
* pointer  Returns contents stored at address
Indirect assignment
* pointer = val  Stores value at address
Assignment
pointer = ptr  Stores pointer in another variable

eg. 
      if a variable  is a=24 and it is stored at memory address 2000.Consider we want to point a pointer named p at that variable,do following things.
int *p,a=24,b,c;//initializing the pointers
*p=&a;//stores 2000 address to the pointer's address
b=*p;//outputs as '24' as it returns content of pointer
*p=55;//directly stores the value at address 2000
c=p;//stores the pointer
  

Pointers

Special case of bounded-size natural numbers
Maximum memory limited by processor word-size
232 bytes = 4GB, 264 bytes = 16 exabytes
A pointer is just another kind of value
A basic type in C
 
eg. int *ptr
The variable “ptr” is a pointer to an “int”,the datatype.

Memory Addresses


1.Storage cells are typically viewed as being byte-sized
 
Usually the smallest addressable unit of memory
 
Few machines can directly access bits individually
 
Such addresses are sometimes called byte-addresses
 
2.Memory is often accessed as words
 
Usually a word is the largest unit of memory access by a single machine instruction
 
CLEAR’s word size is 8 bytes (= sizeof(long))
 
 A word-address is simply the byte-address of the word’s first byte

Expressions

•Constant
–25+5

•Integral
–M*x assume M and x are both integer

•Float
–Sum/9

•Pointer
–Ptr++

•Relational
–X<=y

•Logical
–A>b && b<c

Data types and operators


•Built-in type
–Integer,float,charetc.
•Derived data types
–Array,pointeretc.
•User defined data type
–Structure, union, class, enumeration.
•Operators
–Arithmetic
–Assignment
–Increment/decrement
–Unary
–Relational
–Ternary
–Logical
–bitwise

Basic Commands

C statement
Printf(“%d”,a); //to print variable "a"

Equivalent C++ statement
Cout<<a;

C statement
Scanf(“%f”,&average); //to get the value and store in vaiable "average"

Equivalent C++ statement
Cin>>average;

C statement
Printf(“total is %d this ”,sum);//to print the variable 'sum'

Equivalent C++ statement
Cout<<“totalis“<<sum<<“ this”;

C statement
Scanf(“%d %f”,&a,&x);//to get and store the value of vaeiables 'a' and 'x'

Equivalent C++ statement
Cin>>a>>x;

GTU syllabus for OOCP

Dear friends,if you are a GTU 4th sem student and OOCP is one of your subject,here is a link of your subject syllabus.Click here to download it.

Introduction of C++

C++ was developed by Bjarne Stroustrup of AT&T to add objectoriented
constructs to the C language. Because object-oriented
methodology was relatively new at the time, and all existing
implementations of object-oriented languages were rather slow and
inefficient (most were interpreters), one goal of C++ was to maintain
the efficiency of C.