User Tools

Site Tools


tech:tutorial:c_pointer_cheatsheet

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tech:tutorial:c_pointer_cheatsheet [2016/12/07 23:41]
rk4n3
tech:tutorial:c_pointer_cheatsheet [2016/12/07 23:52] (current)
rk4n3
Line 13: Line 13:
 int main( int argc, char *argv[]) { int main( int argc, char *argv[]) {
  
- int *pVal=0L; // Pointer to an int, address is 0 (null) + int iVal=2; // Regular ​int, set to 2 
- pVal = new int; // pVal now holds address of allocated int + int *pVal = &iVal; // Pointer to an int, holds address of iVal
- *pVal = 2; // The memory pVal points (an int) now has value 2 +
- int iVal = *pVal; // iVal set to value of int pVal points to+
  
- char cX='X'; // Regular char (1 byte) set to the character 'X'+ char cX='E'; // Regular char (1 byte) set to the character 'E'
  char *sTmp=0L;​ //​ Pointer to a char, address is 0 (null)  char *sTmp=0L;​ //​ Pointer to a char, address is 0 (null)
  char sTxt[41];​ //​ Array of 41 chars (block of 41 bytes)  char sTxt[41];​ //​ Array of 41 chars (block of 41 bytes)
  
- sTxt[0] = cX; // First char of sTxt set to value of cX, 'X'+ sTxt[0] = cX; // First char of sTxt set to value of cX, 'E'
  sTmp = sTxt; // Pointer sTmp set to address of array sTxt, so  sTmp = sTxt; // Pointer sTmp set to address of array sTxt, so
  // sTmp & sTxt are pointers now holding same address  // sTmp & sTxt are pointers now holding same address
  sTmp[1] = '​\0';​ //​ Second char of array set to null character  sTmp[1] = '​\0';​ //​ Second char of array set to null character
  
 + cX='​d';​ //​ Regular char (1 byte) set to the character '​d'​
  sTmp = &​cX;​ //​ Pointer sTmp holds address of char cX  sTmp = &​cX;​ //​ Pointer sTmp holds address of char cX
- sTxt[1] = *sTmp; // Second char of sTxt changed to 'X'+ sTxt[1] = *sTmp; // Second char of sTxt changed to 'd'
  sTxt[2] = '​\0';​ //​ Third char of sTxt set to null character  sTxt[2] = '​\0';​ //​ Third char of sTxt set to null character
  
Line 34: Line 33:
  Info *pData = &​aData;​ //​ Pointer to an Info, holds address of aData  Info *pData = &​aData;​ //​ Pointer to an Info, holds address of aData
  aData.iMin = 0; // aData'​s iMin field is set to 0  aData.iMin = 0; // aData'​s iMin field is set to 0
- pData->​iMax = 4; // iMax of instance pData points to (aData) is now 4 + pData->​iMax = *pVal; // iMax of instance pData points to (aData) is now 2 
 + // String in sTxt ("​Ed"​) copied into aData'​s sName 
 + strcpy( pData->​sName,​ sTxt);
 } }
 </​code>​ </​code>​
  
tech/tutorial/c_pointer_cheatsheet.1481175678.txt.gz · Last modified: 2016/12/07 23:41 by rk4n3