Do something to improve things. Anything. Start small or go big. Just do something. Today. Now. The world will be better for it. ----Just because no one else believes in your dreams doesn't mean you shouldn't believe in your dreams. ----The world is the great gymnasium where we come to make ourselves strong. "Swami Vivekananda"

Government Jobs In India Headline Animator

Showing posts with label POINTER IN C LANGUAGE. Show all posts
Showing posts with label POINTER IN C LANGUAGE. Show all posts

21 July 2011

5. 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 "malloc.h"
void main(){
void *ptr;
printf("%d",*ptr);
}


Output: Compiler error


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


#include "string.h"
void main(){
void *ptr;
printf("%d",sizeof(ptr));
}


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:

void 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);
}


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.

Size of Pinter


What is the size of void pointer in c?

Answer: 
Size of any type of pointer in c is independent of data type which is pointer is pointing i.e. size of all type of pointer (near) in c is two byte either it is char pointer, double pointer, function pointer or null pointer.  Void pointer is not exception of this rule and size of void pointer is also two byte. 
What is size of pointer variable in c?
Answer: 
Size of any type of pointer variables in c is independent of data type which is pointer is pointing i.e. size of all type of pointers variables(near) in c is two byte either it is char pointer variable, double pointer variable, function pointer or null pointer variable. 

What is pointer in c programming?


Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like charintfloatdouble or user defined data type like function, pointer etc. or derived data type like array, structure, unionenum.
Examples:

int *ptr;
int (*ptr)();
int (*ptr)[2];

In c programming every variable keeps two type of value.
1. Contain of variable or value of variable.
2. Address of variable where it has stored in the memory.

(1) Meaning of following simple pointer declaration and definition:

int a=5;
int * ptr;
ptr=&a;

Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory : 1025 (assume)
About variable ptr:
4. Name of variable : ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory : 5000 (assume)
Pictorial representation:


Note: A variable where it will be stored in memory is decided by operating system. We cannot guess at which location a particular variable will be stored in memory.

Subscribe via email

Enter your email address:

Delivered by FeedBurner

website-hit-counters.com
Provided by website-hit-counters.com site.