This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tech:tutorial:c_pointer_cheatsheet [2016/12/07 23:40] rk4n3 created |
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 |
- | sTmp = &cX; // Pointer sTmp holds address of char cX | + | cX='d'; // Regular char (1 byte) set to the character 'd' |
- | sTxt[1] = *sTmp; // Second char of sTxt changed to 'X' | + | sTmp = &cX; // Pointer sTmp holds address of char cX |
- | sTxt[2] = '\0'; // Third char of sTxt set to null character | + | sTxt[1] = *sTmp; // Second char of sTxt changed to 'd' |
+ | sTxt[2] = '\0'; // Third char of sTxt set to null character | ||
- | Info aData; // Instance of variable, type is Info (a struct) | + | Info aData; // Instance of variable, type is Info (a struct) |
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> | ||