PCinvent.info

PCinvent Studio Template

Sep 22 2008
My Old C++ Pointer Homework Print E-mail
Monday, 22 September 2008


Read More Articles in: Technology>>>Programming

This Article is written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it


C++ pointer is so Disgusting but powerful. But I think I hate to be a lower level program to deal with physical memory, owning the concept and knowing how it work should be enough. Here is the Pointer usage example I demonstrate......


//Written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
#include <iostream>
using namespace std;

void main(void)
{
    int a=999;    //Init value to a
    cout<<"a=1, &a: "<<&a<<endl;    //0012ff60

    int *ptr; //define pinter to int
    ptr = &a;    //point ptr to memory address of a; ptr now is the memory address
    cout<<"ptr=&a, ptr= : "<<ptr<<endl;    //0012ff60

    int b;
    b = *&a;    //de-refernce of memory address of. Go Memory=>Get its value
    cout<<"b = *&a, b="<<b<<endl;        //999

    int c;
    c=*ptr; //C go to memory address which ptr holds=>de-reference and get its value
    cout<<"c=*ptr; c= : "<<c<<endl;        //999
    cout<<"*ptr= : "<<*ptr<<endl;        //999

    //array
    int array[] = {2, 4, 6};
    int t;
    cout<<"array: "<<array<<endl;    //Array name is a pointer
    cout<<"array[0]: "<<array[0]<<endl;
    array[0]=1;
    cout<<"array[0]=1; array[0]= : "<<array[0]<<endl;

    //copy array of char
    //char arrayA[3]="abc"; //overflow will cause error because it need a \o terminator
    char arrayA[4]="abc";
    char arrayB[4]="xyz";
    //array2[]=array[];//wrong string already defined in compile not runtime
    strcpy(arrayA, arrayB);
    cout<<"arrayA[4]=\"abc\", arrayB[4]=\"xyz\"; arrayA : "<<arrayA<<endl; //A will become B
    cout<<"strlen(arrayA) : "<<strlen(arrayA)<<endl;//string length of array (array name) = 3
}
Comments (0)Add Comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley

busy
Tags:  Technology Select Category My Old C++ Pointer Homework Memory Address Pointer C++
 
< Prev   Next >
Site by PCinvent.com