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);
Discussion:
82 comments Page 7 of 9.
Tj015 said:
2 decades ago
Online complier great, can somebody tell me what are the prerequisite for making online complier.
Mahesh said:
2 decades 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().
If we give (char) it gives error because we are using pointer, then we allocate memory space by malloc().
Rahul roy said:
1 decade ago
Because pointer indicate the address and point out that point. So the char*p indicate that point p.
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) ;
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().
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);
}
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(---);
i.e
char *p = (*char) malloc(---);
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers