Advanced C Concepts: 2501ICT Nathan
Advanced C Concepts: 2501ICT Nathan
Advanced C Concepts
2501ICT Nathan
René Hexel
Semester 1, 2009
Outline
1 Preprocessor Directives
#include Reviewed
Preprocessor conditionals
#ifdef macro
only includes the subsequent code if macro was #defined
#ifndef macro
only includes the subsequent code if macro was not
#defined
#if expression
only includes the subsequent code if expression is true
#else
reverses the effects of the previous #if
#elif expression
combines #else with the effects of #if
#endif
ends the conditional block started by #if or #ifdef
each #if or #ifdef needs exactly one #endif
int main(void)
{
#ifdef DEBUG
printf("debugging is on, DEBUG is %d\n", DEBUG);
#else
printf("debugging is off, DEBUG is not defined\n");
#endif
return 0;
}
Answer
debugging is on, DEBUG is 1
int main(void)
{
#ifdef DEBUG
printf("debugging is on, DEBUG is %d\n", DEBUG);
#else
printf("debugging is off, DEBUG is not defined\n");
#endif
return 0;
}
Answer
debugging is on, DEBUG is 0
int main(void)
{
#if DEBUG_LEVEL < 1 // test the actual value of DEBUG_LEVEL
printf("debugging is off\n");
#elif DEBUG_LEVEL == 1
printf("debugging is on\n");
#else
printf("debugging is verbose, DEBUG_LEVEL is %d\n", DEBUG_LEVEL);
#endif
return 0;
}
Answer
debugging is verbose, DEBUG_LEVEL is 3
#include Protection
#endif // PROFIT_H
Copying Strings
return 0;
}
Explanation
String a gets copied to b character by character
Integer i counts up the current index into the array
’\0’ denotes the end of the string
needs to be copied before finishing the loop
return 0;
}
Answer
1 the string s needs memory space for 6 characters
2 printf() reads the string from the memory location of s
→ let’s do a little experiment
Pointer Variables
Example (A Character Pointer)
int main(void)
{
char s[6] = "Hello";
printf("%s\n", s);
printf("%s\n", p); // the same string as ’s’ (not a copy!)
return 0;
}
Explanation
char * is a character pointer type.
p is called a character pointer variable.
→ stores the memory address of a character
(the first character (’H’) of the string "Hello")
Example
int main(void)
{
char s[6] = "Hello"; // the same string as in the previous example
return 0;
}
char *p = s;
return 0;
}
Explanation
%p prints a memory address (in hexadecimal notation)
all three printf()’s are equivalent
⇒ print the same address!
return 0;
}
Explanation
Every variable occupies space in memory
⇒ pointers can be defined for any type!
Different variables are stored in different memory locations
⇒ all addresses printed in the example will be different!
René Hexel Advanced C Concepts
Preprocessor Directives
Working with Pointers
Pointers and Memory
Pointers to Pointers
Example
int main(void)
{
int x = 7; // normal integer variable
return 0;
}
Explanation
Like normal variables, pointers occupy memory space as
well!
⇒ &a will return the address of the pointer a
int **b is a pointer to a pointer
→ every additional * adds a level of indirection
return 0;
}
Answer
x = 7, y = 5
int main(void)
{
int x = 8;
return 0;
}
Answer
x = 4
Pointer Arithmetic
int main(void)
{
char s[10] = "fantastic"; // a string
char *p = s; // a pointer to the same string
return 0;
}
Answer
fan
int main(void)
{
char b[8], *a = "Hello"; // destination array and source string
return 0;
}
Explanation
in C each assignment has a value that can be tested
any non-zero result is treated as TRUE in C
the end-of-string character \0 is treated as FALSE
René Hexel Advanced C Concepts
Preprocessor Directives
Working with Pointers
Pointers and Memory
Arrays of Pointers
printf("%s\n", s[1]);
return 0;
}
Answer
two
return 0;
}
Points to remember
Command line parameters are passed as an array of
strings (argv)
The first argument (argc) contains the number of
elements in the array
argv[0] always contains the program name itself
René Hexel Advanced C Concepts
Preprocessor Directives
Working with Pointers
Pointers and Memory
Pointers to Structs
int main(void)
{
struct Student s; // a student variable s
struct Student *p = &s; // a pointer to that variable
return 0;
}
Answer
Peter’s ID is 1234567
Shortcut Notation
int main(void)
{
struct Student s; // a student variable s
struct Student *p = &s; // a pointer to that variable
return 0;
}
Explanation
p->x is a shortcut for (*p).x
Pointers to Remember
return 0;
}
Explanation
p does not point to a valid address!
typical errors are Bus Error and Segmentation Fault
return 0;
}
Explanation
0 (NULL) is not a valid memory address!
unlike Java, there are no NULL pointer exceptions!
typical errors are Bus Error and Segmentation Fault
int main(void)
{
int *p = function(); // assign the return value of function to p
return 0;
}
Explanation
x expires at end of function(), memory will be re-used!
will probably only crash sometimes!
→ one of the hardest errors to find and correct!
René Hexel Advanced C Concepts