You are on page 1of 3

C Programming Cheat Sheet Initialising a function

// Return t y p e can be d i f f e r e n t from parameter t y p e


Some General Guidelines f l o a t myIntFunction ( i n t myInt ) ;
i n t myFloatFunction ( f l o a t myFloat ) ;
Initialising variables i n t ∗ myIntPtrFunction ( i n t ∗ myIntPtr ) ;
void myArrayFunction ( i n t myArray [ ] ) ;
// F i r s t dimension o f t h e m a t r i x can be l e f t o u t
i n t myInt , myInt2 ; void myArrayFunction ( i n t myMatrix [ ] [ 1 0 0 ] [ 1 0 0 ] ) ;
f l o a t myFloat , myFloat2 ; struct myStructType myStruc tFunctio n (
i n t ∗ myIntPtr ; struct myStructType myStructVarName ) ;
i n t myArray [ 1 0 0 ] ;
i n t myArray2 [ ] = { 1 , 2 , 3 , 4 , 5 } ; Calling/Using a function
char myMessage [ ] = { ‘H ’ , ‘ e ’ , ‘ l ’ , ‘ l ’ , ‘ o ’ } ;
c h a r myMessage2 [ ] = ” H e l l o ” ;
// myMessage i s t h e same a s myMessage2 , e x c e p t t h a t myFloat = myIntFunction ( myInt2 ) ;
// myMessage d o e s not have a ‘ \ 0 ’ a t t h e end o f t h e s t r i n g myInt = myFloatFunction ( myFloat2 ) ;
struct myStructType myStructVarName ; myIntPtr = myIntPtrFunction ( myIntPtr ) ;
struct myStructType ∗ myStructPtr ; myArrayFunction ( myArray ) ;
myStructVarName = myStruc tFunction ( myStructVarName ) ;

Using variables Structure of a function


This is the default structure of a C function
myInt2 = 5 ; i n t myFunction ( i n t myInt , i n t myArray [ ] ) {
myFloat2 = 3 . 1 4 ; //INITIALISE ( any o t h e r v a r i a b l e s i f r e q u i r e d )
// A s s i g n i n g an a d d r e s s t o a p o i n t e r int a ;
myIntPtr = &myInt ; //USE VARIABLES
// A s s i g n i n g a v a l u e t o where a p o i n t e r p o i n t s a = 5;
∗ myIntPtr = 4 ; myInt = a ∗ 1 0 ;
myArray [ 5 ] = 3 ; myArray [ a ] = myInt ;
// This w i l l change ” H e l l o ” t o ” HelLo ” //RETURN
myMessage [ 4 ] = ‘ L ’ ; return myArray [ a ] − 5 0 ;
myStructVarName . structMember = 5 ; }
myStructPtr = &myStructVarName ;
myStructPtr−>structMember = 5 ;
The others follow a similar structure.

Printing variables Structure of a Program


This is the default structure of a C program:
p r i n t f ( ”%d” , myInt2 ) //INCLUDES :
p r i n t f ( ”%f ” , myFloat2 ) ; #include < s t d l i b . h>
// P r i n t i n g an a d d r e s s ( a normal %d works , and p r i n t #include <s t d i o . h>
// w i t h o u t c a s t i n g works f i n e )
p r i n t f ( ”%l d ” , ( long ) myIntPtr ) ; //GLOBAL VARIABLES
// P r i n t a v a l u e u s i n g a p o i n t e r i n t myGlobalInt ;
p r i n t f ( ”%d” , ∗ myIntPtr ) ;
p r i n t f ( ”%d” , myArray [ 5 ] ) ; //FUNCTION PROTOTYPES
p r i n t f ( ”%s ” , myMessage ) ; // P r i n t i n g u n t i l f i r s t ‘ \ 0 ’ i n t ∗ myIntPtrFunction ( i n t ∗ myIntPtr ) ;
p r i n t f ( ”%d” , myStructVarName . structMember ) ;
p r i n t f ( ”%l ” ’ , myStructPtr ) ; //MAIN FUNCTION
p r i n t f (”%d” myStructPtr−>structMember ) ; i n t main ( ) {
//INITIALISE
int a = 5 ;
int b ; Since a pointer is simply an address, it will contain a value representing the address
i n t ∗ myPtr ; which it is pointing to. In order to see what address the pointer is pointing to, one
s e t b u f ( s t d o u t , 0 ) ; // E c l i p s e r e q u i r e s t h i s l i n e o f code
would do something like,
p r i n t f ( ” Address o f v a r i a b l e t h a t p o i n t e r p o i n t s t o = %l d \ ” ,
//USE VARIABLES ( l o n g ) myIntPtr ) ;
b = a ∗5;
myPtr = &b ;
The Dereferencing Operator - *
//CALL SOME FUNCTIONS When you want to access the data or value in the memory that the pointer is
myIntPtr = myIntPtrFunction ( myIntPtr ) ; pointing to, then you need to dereference the pointer. In order to see what the
//RETURN
value or content is that the pointer is pointing to, one would do something like,
return 0 ; p r i n t f ( ” Value a t a d d r e s s which myIntPtr i s p o i n t i n g t o : %d\n” ,
} ∗ myIntPtr ) ;

//OTHER FUNCTIONS
i n t ∗ myIntPtrFunction ( i n t ∗ myIntPtr ) {
Don’t be fooled!
int a = 1 0 ; Sometimes the unary * is used to do a declaration and other times it is used as a
myNewIntPtr = &a ; dereference operator.
return myNewIntPtr ;
}
• If the unary * is used after a data type, it is a declaration

Using Pointers • If the unary * is used before a pointer variable, it dereferences


Declare the following variables: an integer and an integer pointer. Furthermore, To avoid confusion, you could place the unary * accordingly.
assign the address of the integer variable to the integer pointer.
Value Value * used to do a declaration
myInt
myIntPtr
= &myInt = *myIntPtr i n t ∗ myIntPtr ; // Recommended
i n t ∗ myIntPtr ;
int myInt; &myIntPtr &myInt

int∗ myIntPtr; ⇒ Address Address


Note: There is NO difference in the code snippet above, the result is the same.
myIntPtr = &myInt;

* used as a dereference operator


There are two general operators that are used when working with pointers. They
are explained next. ∗ myIntPtr = 1 0 ; // Recommended
∗ myIntPtr = 1 0 ;
The “Address Of ” Operator - &
The “Address Of” operator does exactly as it says - it finds the address of a Note: There is NO difference in the code snippet above, the result is the same.
variable. The “Address Of” operator is also sometimes called the “Reference”
operator. If you would like to know what the address is of a variable (ie: where Pointers and Arrays
the variable is stored in memory), you would do something like the following, Arrays and pointers are intimately related in C and often may be used inter-
changeably. An array name can be thought of as a constant pointer. Pointers can
p r i n t f ( ” Address o f my i n t v a r i a b l e myInt = %l d \n” , ( long)&myInt ) ;
be used to do any operation involving array subscripting.
The integer pointer that has been declared, is itself stored in memory somewhere.
If you would like to know what the address is of the pointer (ie: where the pointer If you have the following:
is stored in memory), you would do something like the following,
p r i n t f ( ” Address o f my i n t e g e r p o i n t e r myIntPtr = %l d \n” ,
( long)&myIntPtr ) ;
int b [ 5 ] ;
i n t ∗ bPtr ;
bPtr = b ; // OR
bPtr = &b [ 0 ] ; // OR
bPtr = &b ;

Then array element b[n] (where n = 0, 1, 2, 3 or 4) can alternatively be referenced


with the pointer expression in three ways,

∗ ( bPtr + n ) ;
bPtr [ n ] ;
∗ ( b+n ) ;

The following expression are thus equivalent,

b [ n ] = ∗ ( bPtr + n ) = bPtr [ n ] = ∗ ( b+n )

Declaring an array from a pointer using malloc


If you want to declare an array from a pointer using malloc, one way to do this
would be to do the following,

i n t ∗ myIntArray [ 1 0 ] ;
int i ;

f o r ( i = 0 ; i < s i z e o f ( myIntArray ) / s i z e o f ( i n t ∗ ) ; i ++){


myIntArray [ i ] = m a l l o c ( s i z e o f ( i n t ) ) ;
p r i n t f ( ”%l d \n” , ( long)&myIntArray [ i ] ) ;
}

The output for this would be something like this,

2293440
2293444
2293448
2293452
2293456
2293460
2293464
2293468
2293472
2293476

Notice that this method consumes 4 extra bytes (for the pointer).

You might also like