User Tools

Site Tools


tech:tutorial:c_pointer_cheatsheet

This is an old revision of the document!


Basic C Pointer Cheat-Sheet

#include <stdio.h>
 
typedef struct _inf_ {
	int iMin;
	int iMax;
	char sName[41];
} Info;
 
int main( int argc, char *argv[]);
 
int main( int argc, char *argv[]) {
 
	int *pVal=0L;				// Pointer to an int, address is 0 (null)
	pVal = new int;			// pVal now holds address of allocated int
	*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 *sTmp=0L;				// Pointer to a char, address is 0 (null)
	char sTxt[41];				// Array of 41 chars (block of 41 bytes)
 
	sTxt[0] = cX;				// First char of sTxt set to value of cX, 'X'
	sTmp = sTxt;				// Pointer sTmp set to address of array sTxt, so
									// sTmp & sTxt are pointers now holding same address
	sTmp[1] = '\0';			// Second char of array set to null character
 
	sTmp = &cX;					// Pointer sTmp holds address of char cX
	sTxt[1] = *sTmp;			// Second char of sTxt changed to 'X'
	sTxt[2] = '\0';			// Third char of sTxt set to null character
 
	Info aData;					// Instance of variable, type is Info (a struct)
	Info *pData = &aData;	// Pointer to an Info, holds address of aData
	aData.iMin = 0;			// aData's iMin field is set to 0
	pData->iMax = 4;			// iMax of instance pData points to (aData) is now 4
 
}
tech/tutorial/c_pointer_cheatsheet.1481175618.txt.gz · Last modified: 2016/12/07 23:40 by rk4n3