C Programming - Pointers - Discussion

Discussion Forum : Pointers - General Questions (Q.No. 2)
Can you combine the following two statements into one?
char *p;
p = (char*) malloc(100);
char p = *malloc(100);
char *p = (char) malloc(100);
char *p = (char*)malloc(100);
char *p = (char *)(malloc*)(100);
Answer: Option
No answer description is available. Let's discuss.
82 comments Page 1 of 9.

Ravi said:   1 decade ago
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.

Veena said:   9 years ago
#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?

Vijay Sonone said:   1 decade ago
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.

Mukthahar said:   6 years ago
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.

Meera said:   1 decade ago
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);


Ankit said:   7 years ago
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.

Kavyashree said:   1 decade ago
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

Venkata surendra said:   1 decade ago
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.

Praphulla said:   1 decade ago
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.

Sabarno said:   1 decade ago
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.

Post your comments here:

Your comments will be displayed after verification.