C Programming - Complicated Declarations - Discussion

Discussion Forum : Complicated Declarations - Point Out Errors (Q.No. 2)
2.
Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>

int main()
{
    static char *p = (char *)malloc(10);
    return 0;
}
Error: Lvalue required
Error: Rvalue required
Error: invalid *p declaration
No error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Ayushmann said:   1 decade ago
static char *p; // declaring.

p = (char *)malloc(10); // initializing.

But if you go for declaring + initializing at same time.

static char *p = (char *)malloc(10); is absolutely right.

Casper said:   9 years ago
Here the answer "D. No error", is absolutely wrong; anyone can check it out on your compiler?

I tried it, I think Option C is the right answer.

Sunny Setia said:   8 years ago
The malloc() is only used to allocate memory at runtime. Static variables are initialized at compile time.

So, Answer is Option C and not D.

Reetu said:   1 decade ago
This error is there on compiling the code in Linux (GCC) compiler:

In function 'main':

Line 6: Error: Initialize element is not constant.

Vikki said:   1 decade ago
initializer element is not constant
for statics in C, the initializers must be compile time constants.

Payal said:   9 years ago
In GCC, it is showing error with a keyword static which shouldn't be there. What can I do?

Nitin said:   1 decade ago
*p = (char *)malloc(10);

and

p=(char *)malloc(10);

How come they are equal?

Naresh said:   6 years ago
The static variable can not be initialized with run time entity.

Rima said:   1 decade ago
static char *p = (char *)malloc(10);

How is this possible?

Priya said:   8 years ago
Anyone, please explain me the program.

Post your comments here:

Your comments will be displayed after verification.