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.

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

Output is: 64;

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

Mahes said:   1 decade ago
I want clear details in pointer?why we use this concept?

Vasu said:   1 decade ago
In char *p ,

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


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

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

Arunsudharsan said:   1 decade ago
main(){
int i=320;
for(int j=1;j<=i-1;j++)
{
char *ptr=(char*)&j;
printf("%d\n",*ptr);
}

return 0;
}

You will get a clear idea @Sivakumar.

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?

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.


Post your comments here:

Your comments will be displayed after verification.