Thursday, September 8, 2011

Anonymous Objects

It is possible to construct anonymous objects for our own class types. This is done by creating objects like normal, but omitting the variable name.

1Cents cCents(5); // normal variable
2Cents(7); // anonymous variable
In the above code, Cents(7) will create an anonymous Cents object, initialize it with the value 7, and then destroy it.

Wild Pointer

Wild pointer:

A pointer in c which has not been initialized is known as wild pointer.

Example:

What will be output of following c program?


#include

int main(){

int *ptr;

printf("%u\n",ptr);

printf("%d",*ptr);


return 0;

}


Output:


Any address

Garbage value

Here ptr is wild pointer because it has not been initialized.

There is difference between the NULL pointer and wild pointer. Null pointer points the base address of segment while wild pointer doesn’t point any specific memory location.

Generic Pointer

Generic pointer:

void pointer in c is known as generic pointer. Literal meaning of generic pointer is a pointer which can point type of data.

Example:

void *ptr;
Here ptr is generic pointer.

Important points about generic pointer in c?

1. We cannot dereference generic pointer.


#include
#include <malloc.h>

int main(){

void *ptr;
printf("%d",*ptr);


return 0;
}

Output: Compiler error

2. We can find the size of generic pointer using sizeof operator.

#include <string.h>
#include

int main(){
void *ptr;
printf("%d",sizeof(ptr));


return 0;
}

Output: 2
Explanation: Size of any type of near pointer in c is two byte.

3. Generic pointer can hold any type of pointers like char pointer, struct pointer, array of pointer etc without any typecasting.

Example:


#include

int main(){

char c='A';
int i=4;
void *p;
char *q=&c;
int *r=&i;

p=q;
printf("%c",*(char *)p);

p=r;
printf("%d",*(int *)p);


return 0;
}

Output: A4

4. Any type of pointer can hold generic pointer without any typecasting.

5. Generic pointers are used when we want to return such pointer which is applicable to all types of pointers. For example return type of malloc function is generic pointer because it can dynamically allocate the memory space to stores integer, float, structure etc. hence we type cast its return type to appropriate pointer type.

Examples:

1.

char *c;
c=(char *)malloc(sizeof(char));

2.

double *d;
d=(double *)malloc(sizeof(double));

3.

Struct student{
char *name;
int roll;
};
Struct student *stu;
Stu=(struct student *)malloc(sizeof(struct student));

NULL Pointer

NULL pointer:

Literal meaning of NULL pointer is a pointer which is pointing to nothing. NULL pointer points the base address of segment.

Examples of NULL pointer:

1. int *ptr=(char *)0;
2. float *ptr=(float *)0;
3. char *ptr=(char *)0;
4. double *ptr=(double *)0;
5. char *ptr=’\0’;
6. int *ptr=NULL;

What is meaning of NULL?

NULL is macro constant which has been defined in the heard file stdio.h, alloc.h, mem.h, stddef.h and stdlib.h as
#define NULL 0

Examples:

What will be output of following c program?

#include <stdio.h>

int main(){
if(!NULL)
printf("I know preprocessor");
else
printf("I don't know preprocessor");

return 0;
}

Output: I know preprocessor
Explanation:
!NULL = !0 = 1

In if condition any non zero number mean true.

What will be output of following c program?

#include <stdio.h>
int main(){

int i;
static int count;

for(i=NULL;i<=5;){
count++;
i+=2;
}

printf("%d",count);


return 0;
}

Output: 3

What will be output of following c program?

#include <stdio.h>

int main(){

#ifndef NULL
#define NULL 5
#endif

printf("%d",NULL+sizeof(NULL));


return 0;
}

Output: 2
Explanation:

NULL+sizeof(NULL)
=0+sizeoof(0)
=0+2 //size of int data type is two byte.

We cannot copy any thing in the NULL pointer.

Example:

What will be output of following c program?

#include <string.h>
#include <stdio.h>

int main(){

char *str=NULL;

strcpy(str,"c-pointer.blogspot.com");
printf("%s",str);


return 0;
}

Output: (null)

Program to generate Random Numbers

#include
#include
#include
int main(){
int num;

//initialize the random number generator
randomize();

//Generating the random number
num = random(99999);

//Printing the random number
printf("%d",num);
getch();
return 0;
}

Note: random function random(val) generates any number between 1 to num -1 . randomize() function is used only to initialize the random number generator. It internally call the used the header file time.h

Types of Pointers

Dangling pointer:

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.
Initially:


Later:

For example:


(q)What will be output of following c program?

#include

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

int x=25;
++x;

return &x;
}


Output: Garbage value
Note: In some compiler you may get warning message returning address of local variable or temporary

Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.

Solution of this problem: Make the variable x is as static variable.
In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.

#include

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

static int x=25;
++x;

return &x;
}


Output: 26

Difference between NULL and NUL

NULL is a macro defined in for the null pointer.

NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. There?s no standard macro NUL in C, but some people like to define it.

The digit 0 corresponds to a value of 80, decimal. Dont confuse the digit 0 with the value of ?? (NUL)!
NULL can be defined as ((void*)0), NUL as ??.

basically NULL points the predefined value zero to the compiler !!

and NUL is the name of 1st character in the ASCII character set!!