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 2 of 9.

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.

Raviranjan prasad said:   9 years ago
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).

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)

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

Rishi said:   8 years ago
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).

Anishka said:   6 years ago
The pointer declaration and initialization can be done in two ways.

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

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

Himanshu Vishwakarma said:   9 months ago
1) The correct syntax to create a pointer of a specific type is:- <data type> *(pointer name);
2) malloc returns a "void" pointer to the allocated continuous memory size and it must be converted to the required type.
(1)

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

Veena Mitra said:   9 years ago
Hi @Sandeep,

Can you explain me. If my program is :-

#include<stdio.h>
int main(){
float i=320;
int *ptr=(int*)&i;
printf("%d",*ptr);
return 0;
}

Then the answer is : 1134559232.

Why ?

Srinu said:   1 decade ago
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().


Post your comments here:

Your comments will be displayed after verification.