C Programming - Pointers - Discussion

Discussion Forum : Pointers - General Questions (Q.No. 2)
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
Explanation:
No answer description is available. Let's discuss.
Discussion:
82 comments Page 6 of 9.

Suma sahithi said:   1 decade ago
Thanks Mahesh and Kavyasri for the explanation.

Madhureddy said:   1 decade ago
Kavyashree explanation was excellent.

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

Arunpandiyan said:   1 decade ago
This about nothing but Initialization and declaration of varibale present on same line ,.
Well char c;
c='a';
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)

Mohan said:   1 decade ago
Actually we use pointer for p, then why we need another pointer for the location.

Vikrant said:   1 decade ago
Thanks Kavyashree. Yours explanation was good.

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

Nagarajan said:   2 decades ago
I want clear details from pointers with example.

Chandana said:   2 decades ago
Thanks mahesh.

Chinna said:   2 decades ago
If there is allocated space in char p.


Post your comments here:

Your comments will be displayed after verification.