bg287

csc 201 20101117 Notes

Posted in csc201 by bnmng on 2010 11/17

Read 14.2 and 14.3
Today: Going over Test 3
Pointers
Pointer variables
Pointer

Wed: No attendance. Mr. Sterling will be available.

A pointr is a variable whose vaue is the address of a location in memory.
int* ptr; //ptr will wild the address of an int.

int x;
x=12;

int* ptr;
ptr = &x;

x = 12, ptr = the address of x (maybe 2000 or something),

cout << *ptr; //prints 12, the value of the contents at the memory of x.

*ptr = 5; // changes the value at the address ptr to 5;

char msg[] = "Hello";

char* ptr;
ptr=msg; //Don't need an & because an array is already an address

*ptr='M'; Changes the H in Hello to M.
ptr++; //increments the address
*ptr='a'; Changes the e in Mello to an a.

int StringLength(/* in */ const char str[]);
//Precondition: str is a null nterminated string
//postcondition: Return value==length of str
// (not counting '')
{ char *p;
int count =0;
p = str;
while (*p!='')
{
count++;
p++;
//Increments the address p by sizeof char
}
return count;
}

int * p1, p2 //Doesn’t declare two pointers. p1 is a pointer and p2 is an int.

int *p1 *p2 //Declares two pointers.

typedef int * inttype
inttype p1, p2 //Declares two pointers.

//A struct
patient
idnum: 1737
height: 65
weight: 137.

say patptr is a pointer to the struct named patient.
(*patptr).height //Two ways to access patients height.
patptr->height

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: