C Programming - Pointers - Discussion


Can you combine the following two statements into one?

char *p;
p = (char*) malloc(100);

[A]. char p = *malloc(100);
[B]. char *p = (char) malloc(100);
[C]. char *p = (char*)malloc(100);
[D]. char *p = (char *)(malloc*)(100);

Answer: Option C


No answer description available for this question.

Lingarajathi said: (Jun 20, 2010)  
Hello Mr/Ms

I need clear details about pointers and pointers memory allocation, can you send me this me the details.

Mahesh said: (Jul 7, 2010)  
Here we have to allocate space for characters.so first we have to do typecasting i.e (char *).

If we give (char) it gives error because we are using pointer, then we allocate memory space by malloc().

Tj015 said: (Jul 7, 2010)  
Online complier great, can somebody tell me what are the prerequisite for making online complier.

Chinna said: (Jul 20, 2010)  
If there is allocated space in char p.

Chandana said: (Jul 29, 2010)  
Thanks mahesh.

Nagarajan said: (Aug 24, 2010)  
I want clear details from pointers with example.

Kavyashree said: (Oct 10, 2010)  
Prototype of malloc is

  ptr = (data type *)malloc(size);
  - where ptr is pointer of type datatype.

So in the above example as we need to allocate memory for char it can be done with the following statement:

  char *p = (char*)malloc(1000); // here p is a pointer of type char

Vikrant said: (Oct 11, 2010)  
Thanks Kavyashree. Yours explanation was good.

Mohan said: (Dec 26, 2010)  
Actually we use pointer for p, then why we need another pointer for the location.

Arunpandiyan said: (Jan 7, 2011)  
This about nothing but Initialization and declaration of varibale present on same line ,.
Well char c;
can be written as char c = 'a';
the same we applied here a pointer value can can be assigned only to pointer variable, char *p=(char *)malloc(1000)

Rashmi said: (Jan 13, 2011)  
Here malloc (100) give the address and then by char*malloc (100) we got the value that is placed on that address.

And now we assign that value to char p* by that we get a pointer of char type.

Madhureddy said: (Jan 30, 2011)  
Kavyashree explanation was excellent.

Suma Sahithi said: (Jan 31, 2011)  
Thanks Mahesh and Kavyasri for the explanation.

Saran said: (Feb 3, 2011)  
Here we have to allocate space for char.

Manish said: (Feb 4, 2011)  
I didnt understand about pointer,how it is work in c.

What is the difference between pointer of c and c++.?

Jadhi said: (Feb 10, 2011)  
I want to know about malloc clearly.

Jeevanantham said: (Feb 12, 2011)  
The malloc() function is a memory allocation type. It allocats memory in bytes and also return the pointer to allocated memory.

Minu Kutty said: (Feb 13, 2011)  
I need complete details about pointers and c++ is better than c or not.

Vinoth said: (Mar 18, 2011)  
Good example given by arunpandian

Rajesh said: (Mar 28, 2011)  
Thanks kavyashri and mahesh for good explanation.

Abhijit Kamune said: (May 2, 2011)  
Hey tell me where actually memory get allocate when we use malloc(), or calloc().

What is main diff between malloc() and calloc() ?

Prasath said: (May 19, 2011)  
1) All memories allocated by memory management API's like malloc and calloc will be allocated from the Heap partition of the program.

2) Both malloc and calloc will allocate memories but, malloc just allocate required memory leaving the garbage value as it is but calloc allocate memory required and clear it with 0.

Krishna said: (Jun 9, 2011)  
Can you combine two statements in pointers.

Krishna said: (Jun 9, 2011)  
Can you explain pointe def and array def.

ILYAS said: (Jun 29, 2011)  
Its very much simple many people have discussed in a lengthy way:

Understand 1st statement char*p, it means it stores the address of p let us its address is 1000. It means 1000---->p, clearly hear 1000 is pointing p, i.e char*p= p.

Replace p as char *p.

Fousiya said: (Jul 5, 2011)  
Thanks Kavyasri

Divya said: (Jul 5, 2011)  
Thanx kavyasree & mahesh........

Devi said: (Jul 16, 2011)  
I want difference for pointer in c and c++?

Ravi said: (Aug 19, 2011)  
The work of malloc function----->

> Malloc function allocate a fixed size block of memory from the heap and retuns a void type of pointer to it.
> Now we wont to store char type value in that memory so we have to convert the void type pointer to char type that's why (char *) is used to typecast the void pointer to char type.

The diff. between malloc and calloc------->
> syntaxt : data-type *p=(data-type *)malloc(size_t size);
> Malloc function allocates the a fixe size block of memory from the heap.
>syntaxt : data-type *p=(data-type *)calloc(size_t nsize,size_t size);
>Calloc function allocates the a size of (nsize *size) variable size block of memory from the heap.

Shailesh said: (Sep 12, 2011)  
Please explain why we use a pointer.

With real time example.


Prasanth said: (Sep 18, 2011)  
Thanks kavya

Rahul Roy said: (Sep 25, 2011)  
Because pointer indicate the address and point out that point. So the char*p indicate that point p.

Venkata Surendra said: (Sep 29, 2011)  
The difference between c and c++ pointer is only one thing
c pointer is suspicious pointer nothing but a pointer variable which holds address un related type...
simple example
int x=10;
float *p;
p=&x;// it leads to logical error but c complier doesnt give any error same program if you save.cpp extension error will occur.

Paramjeet Singh said: (Oct 17, 2011)  
First of all malloc is memory data type and p is character pointer data type. So as we know comparison is possible between two same entity so we will cast malloc as character pointer then equate at character pointer p.

Nandan said: (Nov 6, 2011)  
Pointer returns the address of the located position in the memory...and malloc fun too returns the pointer of the space allocated..

char *p = (*char) malloc(---);

Meera said: (Jan 27, 2012)  
malloc() function in c is used for allocating memory.

Following is the simple program using malloc function.

#include<stdio.h> /* For standard input output */
#include<malloc.h> /* You need this if on UNIX or LINUX */

int main()
char words[]={" this is a simple example for malloc\n"};
char *p; /* char pointer for use with malloc() */

p = (char *)malloc(sizeof(words)); /* Explained in the following text */
printf("words = %s\n",words);


Chandan said: (Feb 23, 2012)  
Thanks to all for discussion. But one think *P and pointer without star, both are different one store value and other store address then how you combine?

Rajendra said: (Feb 25, 2012)  
Thanks mahesh and kavaya.

Srinu said: (Jul 9, 2012)  
Here we have to allocate space for characters. So first we have to do typecasting i.e. (char *).

If we give (char) it gives error because we are using pointer, then we allocate memory space by malloc().

Priyanka Bedre said: (Jul 12, 2012)  
Can we write this program like.

char p;
p= (char *) malloc (1000) ;

Sanjay Tiwari said: (Aug 15, 2012)  
Since we know that decleration+initialization=can we write.

Shubham Arora said: (Oct 3, 2012)  
I want to explain brief discription about the char that char represents a individual value during coding in c language i.e. p in above code,as we can also write as char p = malloc(100);

Umashankar Singh said: (Jan 16, 2013)  
Please can an one explain malloc and calloc function ?

Praphulla said: (Mar 20, 2013)  
malloc & calloc both are memory allocation function & both returns void type where afterwords we type case it.


int *p;

where malloc depends upon byte which are fixed i.e 8-bit,again 16-bit.

calloc deals with no.of bytes where bytes is considered as block relevant to the memory issue.

Vij said: (Apr 19, 2013)  
1. char p;
p= (char *) malloc (1000) ;

2. char *p;
p= (char *) malloc (1000) ;

Is that 1 and 2 mean the same. explain?

Sivakumar said: (Jun 28, 2013)  
int main(){
int i=320;
char *ptr=(char*)&i;
return 0;

Ans: 64.

Could anyone explain this program and answer?.

Vijay Sonone said: (Jul 11, 2013)  
Here the memory is allocated by malloc function. P is local variable so the memory is allocated on stack for P. But the memory allocated by malloc function is 100 byte and which is allocated on HEAP. Here the type of pointer is character so for allocating the memory malloc is type casted with character, Because malloc always returns void*.

So void* is a pointer which pointing to particular address but can not access the element from that address. For allocating memory malloc internally call sbrk and brk algorithm. This algorithm internally called by growrage algorithm.

Sabarno said: (Jul 13, 2013)  
Say: int a=10;

// This means a is a variable where 10 is stored.

Now say the address/ memory location of a is 5000.

So, I say: int *a= 5000;

// 5000 refers to the memory location of a or pointed to a.


For storing a value you need a definition statement and for storing a memory address you need a pointer.

Rashmi said: (Aug 18, 2013)  
char *p;

Here *p means value of p.

And p= (char *) malloc (1000); which is saying that value of p is char*..


They can be combined as:

char *p = (char *) malloc (1000);

Subha said: (Aug 23, 2013)  
If p is a pointer.

i.e. p addressing some variable for eg x.

The p stores the address of x.

And *p gives the value of x. Is this correct or not?

Parsam Sai Bharath said: (Aug 25, 2013)  
Actually return type of malloc() or calloc() void pointer. So should typecast it.

Priya said: (Sep 18, 2013)  
Can anybody explain where is the pointer use, what is the purpose using of pointer?

Arunsudharsan said: (Jan 30, 2014)  
int i=320;
for(int j=1;j<=i-1;j++)
char *ptr=(char*)&j;

return 0;

You will get a clear idea @Sivakumar.

Yadhanna said: (Feb 8, 2014)  
In char *p ,
char p = (char*) malloc (100);

we are allocating 100bytes for a pointer variable "p". What is the need for that ? Are we storing 100 addresses ?

Vasu said: (Feb 28, 2014)  
In char *p ,

char p = (char*) malloc (100);
char p = (char*) malloc (100);

Wheteher we are allocating 100 bytes for p or *p?

Mahes said: (Mar 1, 2014)  
I want clear details in pointer?why we use this concept?

Shabana said: (Apr 9, 2014)  
int main(){
int i=320;
char *ptr=(char*)&i;
return 0;

Output is: 64;

How can any one explain these please I didn't understand?

Sudhagar said: (Jun 30, 2014)  
I need more explanation about pointers?

Sandeep said: (Jul 17, 2014)  
int main(){
int i=320;
char *ptr=(char*)&i;
return 0;

The answer is 64 because it will take will only first 8 bits from 16 bits of int.

Appala Prathyusha said: (Jul 20, 2014)  
How do we know the address of i?

Rafael Bluhm said: (Jan 28, 2015)  
Is not correct to cast*void types in C. Malloc returns a void pointer, and compiles cast it automatically.

The correct is char*p = malloc(100);

Katty said: (May 25, 2015)  
The space is allotted for pointers.

Veena Mitra said: (Jun 22, 2015)  
Hi @Sandeep,

Can you explain me. If my program is :-

int main(){
float i=320;
int *ptr=(int*)&i;
return 0;

Then the answer is : 1134559232.

Why ?

Veena said: (Jun 22, 2015)  
#include /* For standard input output */
#include /* You need this if on UNIX or LINUX */

int main()
char words[]={" this is a simple example for malloc\n"};
char *p; /* char pointer for use with malloc() */
int n=sizeof(words);
int i;
p = (char *)malloc(sizeof(words)); /* Explained in the following text */

printf("Base add of array word= %u",&words);
printf("\n address of pointer p= %u",p);


The output is:

Base add of array word = 4294811566.

Address of pointer p = 139501688.

Here, why not p containing the base address of array words?

Ananda Venkatesh said: (Nov 27, 2015)  
Because words[] is stored in code section and p is pointing to the address of heap section containing memory of sizeof (words). So, both the addresses are different.

Ananda Venkatesh said: (Nov 27, 2015)  
@Veena mitra.

int and float storage patterns are different. So, we get different outputs.

Raviranjan Prasad said: (Nov 28, 2015)  
Because the char is a data type who declare a pointer type variable *p.

So it will used char *p and we will write any memorial allocation by (ptr=malloc (size in byte code).

When we change this type then write malloc by this syntax:

ptr = (data type *) malloc (size in byte code).

Rishi said: (May 31, 2016)  
Explanations are good.

But one think is missing that since *p points to the beginning address, thus p=char* malloc(100), can be assigned with a char*p.

The type should be same is the necessary point but why so works is because of p(base address).

Pratik Dutta said: (Aug 4, 2016)  
We can variable declare as int i;


Int i=10;

At first we declare the variable then assign value, but together we can write the last line.

Ganathantrapureddy said: (Aug 7, 2016)  
Thanks for all given solutions.

Siva said: (Aug 28, 2016)  
Why are we using return 0 if we give the main function as int main? can anyone tell me?

LOIS said: (Sep 12, 2016)  
In C, 0 = true and others values represent false; then when one return 0 is to say that no errors occur.

Shehryar said: (May 16, 2017)  
Explain it in detail.

Pooja said: (Jul 24, 2017)  
Not getting it, Explain it in detail.

Vijay said: (Nov 22, 2017)  
step 1: char p // It is normal.
step 2: here we are using pointer so p = p* and p = 1000(size).
step 3 : P* = (char*)malloc(1000).

Ankit said: (Nov 26, 2017)  
Malloc is a function to allocate memory dynamically(i.e. at the runtime of a program). So, when the memory being declared by the compiler at runtime it doesn't know the type of memory being allocated because we can't specify the data type of memory at runtime like static variables. hence we have to typecast that memory according to our need. (In this case as Character type).

Hope it helps.

Anishka said: (Feb 3, 2018)  
The pointer declaration and initialization can be done in two ways.

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

char *p;
p = (char*) malloc(100);
It can also be written as;
char *p = (char*)malloc(100);

Mukthahar said: (May 30, 2018)  
The malloc function creates a memory in heap memory and returns void pointer. which can be converted into any pointer type. now so char *p=(char *)malloc(sizeof(int)*10);


size(int)*10 means want to create 20 bytes of memory.
malloc(sizeof(int)*10) means 20 bytes of memory created by malloc function in heap memory.
now malloc function returns void pointer. A void pointer can convert into any other pointer type.
(char *)malloc(sizeof(int)*n)

Here we had converted the void pointer into char pointer.

That's it.

Rama said: (Jul 12, 2018)  
Thank you all for your good explanations.

Can anyone explain what is the relation between arrays & pointers?

Rishikesh said: (Aug 19, 2020)  
Prototype of malloc = ptr = (data type *)malloc(size);

Parth Lathiya said: (Jul 30, 2022)  

Both are not the same.

1) not a proper variable pointer declaration.
So, 2) proper define the pointer variable.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.