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

Priya said:   1 decade ago
Can anybody explain where is the pointer use, what is the purpose using of pointer?

Parsam sai bharath said:   1 decade ago
Actually return type of malloc() or calloc() void pointer. So should typecast it.

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

Rashmi said:   1 decade ago
char *p;

Here *p means value of p.

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

Therefore,

They can be combined as:

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

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.

Hence

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

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.

Sivakumar said:   1 decade ago
#include<stdio.h>
int main(){
int i=320;
char *ptr=(char*)&i;
printf("%d",*ptr);
return 0;
}

Ans: 64.

Could anyone explain this program and answer?.

Vij said:   1 decade ago
1. char p;
p= (char *) malloc (1000) ;

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

Is that 1 and 2 mean the same. explain?

Praphulla said:   1 decade ago
malloc & calloc both are memory allocation function & both returns void type where afterwords we type case it.

ex:

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

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.

Umashankar Singh said:   1 decade ago
Please can an one explain malloc and calloc function ?


Post your comments here:

Your comments will be displayed after verification.