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

Mahesh 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().

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

The answer is 64 because it will take will only first 8 bits from 16 bits of int.

Rashmi said:   1 decade ago
Here malloc (100) give the address and then by char*malloc (100) we got the value that is placed on that address.

And now we assign that value to char p* by that we get a pointer of char type.

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?

Shubham Arora said:   1 decade ago
I want to explain brief discription about the char that char represents a individual value during coding in c language i.e. p in above code,as we can also write as char p = malloc(100);

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)

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?.

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 ?

Nandan said:   1 decade ago
Pointer returns the address of the located position in the memory...and malloc fun too returns the pointer of the space allocated..
i.e

char *p = (*char) malloc(---);

Ananda venkatesh said:   9 years ago
Because words[] is stored in code section and p is pointing to the address of heap section containing memory of sizeof (words). So, both the addresses are different.


Post your comments here:

Your comments will be displayed after verification.