CMPE-013/L Pointers Gabriel Hugh ElkaimSpring 2013Pointers A Variable’s Address

CMPE-013/L Pointers Gabriel Hugh ElkaimSpring 2013Pointers A Variable's Address www.phwiki.com

CMPE-013/L Pointers Gabriel Hugh ElkaimSpring 2013Pointers A Variable’s Address

Simpson, Leigh, Music Director;Program Director has reference to this Academic Journal, PHwiki organized this Journal CMPE-013/L Pointers Gabriel Hugh ElkaimSpring 2013Pointers A Variable’s Address versus A Variable’s ValueIn some situations, we will want to work with a variable’s address in memory, rather than the value it contains 005AAddress16-bit Data Memory (RAM)0x08020x08040x08060x08080x0800xVariable stored at Address0123DEADBEEFF00D04560x080AVariable name from C codeint x;Value of variable x= 0x0123Address of variable x= 0x0802Pointers What are pointersA pointer is a variable or constant that holds the address of another variable or functionFFFFAddress16-bit Data Memory (RAM)0x08020x08040x08060x08080x0800xpVariable at Address0123FFFF0802FFFFFFFF0x080AInteger Variable:Pointer Variable:

Warburg Institute, University of London GB www.phwiki.com

This Particular University is Related to this Particular Journal

Pointers What do they doA pointer allows us to indirectly access a variable (just like indirect addressing in assembly language)005AAddress16-bit Data Memory (RAM)0x08020x08040x08060x08080x0800x0123DEAD0802F00D04560x080Apx = 0x0123;p = 0x0123;Direct Accessvia xIndirect Accessvia pp points to xPointers Why would I want to do thatPointers make it possible to write a very short loop that per as long as ms the same task on a range of memory locations / variables.//Point to RAM buffer starting addresschar bufPtr = &buffer;while ((DataAvailable) && (ReceivedCharacter != ‘’)){ //Read byte from UART in addition to write it to RAM buffer ReadUART(bufPtr); //Point to next available byte in RAM buffer bufPtr++;}Pointers Why would I want to do thatAddress16-bit Data Memory (RAM)0x08BC0x08BE0x08C00x08C20x08C40x08C60x08BA0x08C80123456789ABCDEF13579BDF02468ACEPseudo-code:Point arrow to first address of bufferWrite data from UART to location pointed to by arrowMove arrow to point to next address in bufferRepeat until data from UART is 0, or buffer is full (arrow points to last address of buffer)RAM buffer allocated over a range of addresses(perhaps an array)

Pointers Where else are they usedUsed in conjunction with dynamic memory allocation (creating variables at runtime)Provide method to pass arguments by reference to functionsProvide method to pass more than one piece of in as long as mation into in addition to out of a functionA more efficient means of accessing arrays in addition to dealing with stringsPointers How to Create a Pointer Variableint iPtr; // Create a pointer to intfloat fPtr; // Create a pointer to floattype ptrName;In the context of a declaration, the merely indicates that the variable is a pointertype is the type of data the pointer may point toPointer usually described as “a pointer to type”Pointers How to Create a Pointer Type with typedeftypedef int intPtr; // Create pointer to int typeintPtr p; // Create pointer to int // Equivalent to: int p;typedef type typeName;A pointer variable can now be declared as type typeName which is a synonym as long as typeThe is no longer needed since typeName explicitly identifies the variable as a pointer to type

To set a pointer to point to another variable, we use the & operator (address of), in addition to the pointer variable is used without the dereference operator :This assigns the address of the variable x to the pointer p (p now points to x)Note: p must be declared to point to the type of x (e.g. int x; int p;)Pointers Initializationp = &x;Pointers UsageWhen accessing the variable pointed to by a pointer, we use the pointer with the dereference operator :This assigns to the variable y, the value of what p is pointing to (x from the last slide)Using p, is the same as using the variable it points to (e.g. x)y = p;Pointers Another Way To Look At The Syntax&x is a constant pointerIt represents the address of xThe address of x will never changep is a variable pointer to intIt can be assigned the address of any intIt may be assigned a new address any timeint x, p; //int in addition to a pointer to intp = &x; //Assign p the address of xp = 5; //Same as x = 5;

Pointers Another Way To Look At The Syntaxp represents the data pointed to by pp may be used anywhere you would use x is the dereference operator, also called the indirection operatorIn the pointer declaration, the only significance of is to indicate that the variable is a pointer rather than an ordinary variableint x, p; //1 int, 1 pointer to intp = &x; //Assign p the address of xp = 5; //Same as x = 5;Pointers Another Way To Look At The SyntaxBank of Mailboxes(memory locations)Contents of the Mailbox(variable x or p)Address ofMailbox(&x)Address on Envelope(pointer p)Contents of Letter(integer literal 5)p = &x;p = 5;Pointers How Pointers WorkAddress16-bit Data Memory (RAM)0x08BC0x08BE0x08C00x08C20x08C40x08C60x08BA{ int x, y; int p; x = 0xDEAD; y = 0xBEEF; p = &x; p = 0x0100; p = &y; p = 0x0200;}xypVariable at Address0x08C800000000000000000000000000000000

Pointers How Pointers WorkAddress16-bit Data Memory (RAM)0x08BC0x08BE0x08C00x08C20x08C40x08C60x08BAxypVariable at Address0x08C80000DEAD000000000000000000000000{ int x, y; int p; x = 0xDEAD; y = 0xBEEF; p = &x; p = 0x0100; p = &y; p = 0x0200;}Pointers How Pointers WorkAddress16-bit Data Memory (RAM)0x08BC0x08BE0x08C00x08C20x08C40x08C60x08BAxypVariable at Address0x08C80000DEADBEEF00000000000000000000{ int x, y; int p; x = 0xDEAD; y = 0xBEEF; p = &x; p = 0x0100; p = &y; p = 0x0200;}Pointers How Pointers WorkAddress16-bit Data Memory (RAM)0x08BC0x08BE0x08C00x08C20x08C40x08C60x08BAxypVariable at Address0x08C80000DEADBEEF08BC0000000000000000{ int x, y; int p; x = 0xDEAD; y = 0xBEEF; p = &x; p = 0x0100; p = &y; p = 0x0200;}

Pointers How Pointers WorkAddress16-bit Data Memory (RAM)0x08BC0x08BE0x08C00x08C20x08C40x08C60x08BAxypVariable at Address0x08C800000100BEEF08BC0000000000000000{ int x, y; int p; x = 0xDEAD; y = 0xBEEF; p = &x; p = 0x0100; p = &y; p = 0x0200;}Pointers How Pointers WorkAddress16-bit Data Memory (RAM)0x08BC0x08BE0x08C00x08C20x08C40x08C60x08BAxypVariable at Address0x08C800000100BEEF08BE0000000000000000{ int x, y; int p; x = 0xDEAD; y = 0xBEEF; p = &x; p = 0x0100; p = &y; p = 0x0200;}Pointers How Pointers WorkAddress16-bit Data Memory (RAM)0x08BC0x08BE0x08C00x08C20x08C40x08C60x08BAxypVariable at Address0x08C800000100020008BE0000000000000000{ int x, y; int p; x = 0xDEAD; y = 0xBEEF; p = &x; p = 0x0100; p = &y; p = 0x0200;}

Pointers in addition to Arrays A Quick Reminder Array elements occupy consecutive memory locationsPointers can provide an alternate method as long as accessing array elementsAddress16-bit Data Memory (RAM)0x08000x08020x08040x07FEx[0]0x0806x[1]x[2]int x[3] = {1,2,3};FFFF000100020003FFFFPointers in addition to Arrays Initializing a Pointer to an ArrayThe array name is the same thing as the address of its first (0th) elementIf we declare the following array in addition to pointer variable:We can initialize the pointer to point to the array using anyone of these three methods:int x[5] = {1,2,3,4,5};int p;p = x; //Works only as long as arrays!p = &x; //Works as long as arrays or variablesp = &x[0]; //This one is the most obviousIncrementing a pointer will move it to the next element of the arrayMore on this in just a bit Address16-bit Data Memory (RAM)0x08000x08020x08040x07FEx[0]0x0806x[1]x[2]FFFF000100020003FFFFPointers in addition to Arrays A Preview of Pointer Arithmeticint x[3] = {1,2,3};int p;p = &x;p++;p

Simpson, Leigh WOOF-AM Music Director;Program Director www.phwiki.com

Incrementing a pointer will move it to the next element of the arrayMore on this in just a bit Address16-bit Data Memory (RAM)0x08000x08020x08040x07FEx[0]0x0806x[1]x[2]FFFF0001000200030800Pointers in addition to Arrays A Preview of Pointer Arithmeticint x[3] = {1,2,3};int p;p = &x;p++;pIncrementing a pointer will move it to the next element of the arrayMore on this in just a bit Address16-bit Data Memory (RAM)0x08000x08020x08040x07FEx[0]0x0806x[1]x[2]FFFF0001000200030802Pointers in addition to Arrays A Preview of Pointer Arithmeticint x[3] = {1,2,3};int p;p = &x;p++;pPointer Arithmetic Incrementing PointersIncrementing or decrementing a pointer will add or subtract a multiple of the number of bytes of its typeIf we have: float x; float p = &x; p++; We will get p = &x + 4 since a float variable occupies 4 bytes of memory

float a[4]float a[5]float a[6]float a[7]float a[8]Pointer Arithmetic Incrementing Pointersfloat a[0]float a[1]float a[2]float a[3]ptr = &a;0x00500x00520x00540x00560x00580x005A0x005C0x005E0x00600x00620x00640x00660x00680x006A0x006C0x006E0x00700x00720x00740x0076ptr++;float ptr;Incrementing ptr moves it to the next sequential float array element16-bit Data Memory WordsPointer Arithmetic Larger JumpsAdding or subtracting any other number with the pointer will change it by a multiple of the number of bytes of its typeIf we have int x; int p = &x; p += 3; We will get p = &x + 6 since an int variable occupies 2 bytes of memoryfloat a[4]float a[5]float a[6]float a[7]float a[8]Pointer Arithmetic Larger Jumpsfloat a[0]float a[1]float a[2]float a[3]ptr = &a;0x00500x00520x00540x00560x00580x005A0x005C0x005E0x00600x00620x00640x00660x00680x006A0x006C0x006E0x00700x00720x00740x0076ptr += 6;float ptr;Adding 6 to ptr moves it 6 float array elements ahead (24 bytes ahead)16-bit Data Memory Words

Simpson, Leigh Music Director;Program Director

Simpson, Leigh is from United States and they belong to WOOF-AM and they are from  Dothan, United States got related to this Particular Journal. and Simpson, Leigh deal with the subjects like Music; Religious/Gospel

Journal Ratings by Warburg Institute, University of London

This Particular Journal got reviewed and rated by Warburg Institute, University of London and short form of this particular Institution is GB and gave this Journal an Excellent Rating.