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

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);

Sanjay Tiwari said:   1 decade ago
Since we know that decleration+initialization=can we write.

Priyanka Bedre said:   1 decade ago
Can we write this program like.

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

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

Rajendra said:   1 decade ago
Thanks mahesh and kavaya.

Chandan said:   1 decade ago
Thanks to all for discussion. But one think *P and pointer without star, both are different one store value and other store address then how you combine?

Meera said:   1 decade ago
malloc() function in c is used for allocating memory.

Following is the simple program using malloc function.

#include<stdio.h> /* For standard input output */
#include<malloc.h> /* You need this if on UNIX or LINUX */


int main()
{
char words[]={" this is a simple example for malloc\n"};
char *p; /* char pointer for use with malloc() */

p = (char *)malloc(sizeof(words)); /* Explained in the following text */
printf("words = %s\n",words);

return(0);
}

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(---);

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.

Venkata surendra said:   1 decade ago
The difference between c and c++ pointer is only one thing
c pointer is suspicious pointer nothing but a pointer variable which holds address un related type...
simple example
int x=10;
float *p;
p=&x;// it leads to logical error but c complier doesnt give any error same program if you save.cpp extension error will occur.


Post your comments here:

Your comments will be displayed after verification.