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;
}
Discussion:
10 comments Page 1 of 1.
Vikki said:
1 decade ago
initializer element is not constant
for statics in C, the initializers must be compile time constants.
for statics in C, the initializers must be compile time constants.
Nitin said:
1 decade ago
*p = (char *)malloc(10);
and
p=(char *)malloc(10);
How come they are equal?
and
p=(char *)malloc(10);
How come they are equal?
Rima said:
1 decade ago
static char *p = (char *)malloc(10);
How is this possible?
How is this possible?
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.
p = (char *)malloc(10); // initializing.
But if you go for declaring + initializing at same time.
static char *p = (char *)malloc(10); is absolutely right.
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.
In function 'main':
Line 6: Error: Initialize element is not constant.
Payal said:
9 years ago
In GCC, it is showing error with a keyword static which shouldn't be there. What can I do?
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.
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.
So, Answer is Option C and not D.
Priya said:
8 years ago
Anyone, please explain me the program.
Naresh said:
6 years ago
The static variable can not be initialized with run time entity.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers