The C NULL Pointer
NULL is a #define statement that is used by functions in the standard C library (stdlib.h) such as malloc and fopen. It’s used to define an invalid pointer or an error condition.
Here is NULL definition statement inside stdlib.h:
#define NULL ((void *)0)
The above code means that NULL is the zero memory address.
Note that unlike PHP, the following is bad practice in C language:
if (!malloc(10))
You need to write the above statement as follows:
if (malloc(10) == NULL)
Below is the sample code for the NULL pointer:
/*
*
* null.c
*
* Program to demonstrate the use of a NULL pointer
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
static void goodbye() {
while (getchar() != 'n') {}
printf("nPress ENTER to exit: ");
fflush(stdin);
getchar();
}
/*
*
* memdup()
*
* A function that takes a pointer to any type of memory,
* then creates a duplicate amount of memory (using malloc)
* and copies the original memory into the new memory
*
*/
void *memdup(void *mem, int size) {
void *newptr;
newptr = malloc(size);
if (newptr == NULL) {
return NULL;
}
memcpy(newptr, mem, size);
return newptr;
}
main() {
float float_array1[] = {1.2, 2.3, 3.4};
int int_array1[] = {1,2,3,4,5,6};
char char_array1[] = "Hello Worldn";
float *float_array2;
int *int_array2;
char *char_array2;
atexit(goodbye);
float_array2 = memdup(float_array1, sizeof float_array1);
int_array2 = memdup(int_array1, sizeof int_array1);
char_array2 = memdup(char_array1, sizeof char_array1);
if (float_array2 == NULL) {
printf("The float failedn");
}
else {
free(float_array2);
}
int_array2 == NULL ? printf("The int failedn") : free(int_array2);
char_array2 == NULL && printf("The char failedn");
return 0;
}
C Character Functions
There are several types of character functions in the standard C library. One type of character functions is character classification functions. They are isuppper, islower, isdigit, isspace, and other boolean functions. For example, if (isupper(ch)) determines whether ch is upper case or not.
Another type of character functions is character conversion functions. They are toupper, tolower, and others. For example, ch = toupper(ch) converts ch to upper case whether the original is lower case or upper case.
Here is the same code for the character functions:
/*
*
* ctype.c
*
* Program to show examples of the use of isupper, toupper, etc.
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
#include <ctype.h>
main() {
char ch;
printf("Please type in a character (enter q to quite): ");
scanf("%c", &ch);
while (ch != 'q') {
if (isupper(ch)) {
printf("This is an uppercase charactern");
ch = tolower(ch);
printf("Lowercase equivalent is '%c'n", ch);
}
else if (islower(ch))
printf("This is an lowercase charactern");
else if (isdigit(ch))
printf("This is an digitn");
else if (isspace(ch))
printf("This is an space-type charn");
else
printf("This is an unknown charn");
printf("Please type a character (enter q to quite): ");
while (getchar() != 'n' && getchar() != EOF) {}
scanf("%c", &ch);
}
fflush(stdin);
}