Saturday, January 3, 2009

C reference 3

Union - A derived data type, whose members share the same storage space. The members of a union can be of any type and the number of bytes used to store a union must be at least enough to hold the largest member.

Only a value with same type of the first union member can be used to initialize union in declaration part. For example:

union
sample
{

int p;

float q;
};

...

...

union
sample content = {234};
union sample content = {23.44}; // is invalid.

String reverse function using recursion:

#include/stdio.h/
void reverse(char *);


int main() {
char c[20];
char *x;
x= gets(c);
reverse(x);
printf("\n");
return 0;
}

void reverse(char *s)
{
if(s[0] == '\0')
return;
else
{
reverse(&s[1]);
putchar(s[0]);
}
}


Function prototype

Function description

int getchar(void)

Input the next character from the standard input (keyboard) and return it as an integer.

char *gets(char *s)

Input characters from the standard input (keyboard) into the array s until a newline or end-of-file character is encountered. A terminating NULL character is appended to the array.

int putchar(int c)

Print the character stored in c.

int puts(const char *s)

Print the string s followed by a newline character.

int sprintf(char *s, const char *format, …)

Equivalent to printf() except the output is stored in the array s instead of printing on the screen.

int sscanf(char *s, const char *format, …)

Equivalent to scanf() except the input is read from the array s instead of reading from the keyboard.



strtok() -

char *strtok(char *s1, const char *s2)

A sequence of calls to strtok() breaks string s1 into “tokens”, logical pieces such as words in a line of text, separated by characters contained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called, NULL is returned.


Memory Functions:

Function prototype

Function description

void *memcpy(void *s1, const void *s2, size_t n)

Copies n characters from the object pointed to by s2 into the object pointed to by s1. A pointer to the resulting object is returned.

void *memmove(void *s1, const void *s2, size_t n)

Copies n characters from the object pointed to by s2 into the object pointed to by s1. The copy is performed as if the characters are first copied from the object pointed to by s2 into temporary array, then from the temporary array into the object pointed to by s1. A pointer to the resulting object is returned.

int memcmp(const void *s1, const void *s2, size_t n)

Compares the first n characters of the objects pointed to by s1 and s2. The function return 0, less than 0, or greater than 0 if s1 is equal to, less than, or greater than s2.

void *memchr(const void *s, int c, size_t n)

Locates the first occurrence of c (converted to unsigned char) in the first n characters of the object pointed to by s. If c is found, a pointer to c in the object is returned. Otherwise NULL is returned.

void *memset(void *s, int c, size_t n)

Copies c (converted to unsigned char) into the first n characters of the object pointed to by s. A pointer to the result is returned.



Dynamic Memory Allocation:


- Dynamic memory is allocated on the heap.


Disk file segments



For more details: http://www.tenouk.com/ModuleZ.html



Heap


- The heap segment provides more stable storage of data for a program; memory allocated in the heap remains in existence for the duration of a program.


- global variables (external storage class), and static variables are allocated on the heap.The memory allocated in the heap area, if initialized to zero at program start, remains zero until the program makes use of it. Thus, the heap area need not contain garbage.


malloc() :

char * test;

test = (char *) malloc(10); // allocates 10 bytes of memory


calloc():

int * test;

test = (int *) calloc(5, sizeof(int)); // allocates 5* 4 (size of int) bytes


- Initializes the allocated memory elements to zero.


realloc():

reallocates the already allocated memory

void * realloc (void * pointer, size_t elemsize);




No comments: