User Tools

Site Tools


tech:tutorial:c_pointer_cheatsheet

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 iVal=2;		// Regular int, set to 2
	int *pVal = &iVal;	// Pointer to an int, holds address of iVal
 
	char cX='E';		// Regular char (1 byte) set to the character 'E'
	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, 'E'
	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
 
	cX='d';			// Regular char (1 byte) set to the character 'd'
	sTmp = &cX;		// Pointer sTmp holds address of char cX
	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 *pData = &aData;	// Pointer to an Info, holds address of aData
	aData.iMin = 0;		// aData's iMin field is set to 0
	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);
}
tech/tutorial/c_pointer_cheatsheet.txt · Last modified: 2016/12/07 23:52 by rk4n3