ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
int main()
struct amp
{
string z;
char y;
float x;
int w;
};
cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"and it is located at"<<struct amp*s = malloc(sizeof(struct amp))<<endl;
it gives me an error---
In funtion 'int main()':
error: expected primary-expression before 'struct'
error: expected ';' before 'struct'
Please help me with this.. i tried looking it on net but could not find it..
int main()
struct amp
{
string z;
char y;
float x;
int w;
};
cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"and it is located at"<<struct amp*s = malloc(sizeof(struct amp))<<endl;
it gives me an error---
In funtion 'int main()':
error: expected primary-expression before 'struct'
error: expected ';' before 'struct'
Please help me with this.. i tried looking it on net but could not find it..
Why would you do something perverse like mix cout (C++) with "malloc()" ?
Anyway, the problem is mixing a declaration ("struct amp*s) in with an expression.
You have several alternatives:
Code:
struct amp
{
string z;
char y;
float x;
int w;
} s;
int
main(int argc, char *argv[])
{
cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
string z;
char y;
float x;
int w;
};
int
main(int argc, char *argv[])
{
// Allocate from stack ("automatic" variable)
struct amp s;
cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
string z;
char y;
float x;
int w;
};
int
main(int argc, char *argv[])
{
// Allocate from heap
// "new" is a C++ operator, "malloc()" is a C/C++ standard library function
struct amp *s = new struct amp;
cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"\n";
but my problem was on memory location, that is my second part struct amp*s = malloc(sizeof(struct amp)). please help me with this.
Thanks again.
Quote:
Originally Posted by paulsm4
Why would you do something perverse like mix cout (C++) with "malloc()" ?
Anyway, the problem is mixing a declaration ("struct amp*s) in with an expression.
You have several alternatives:
Code:
struct amp
{
string z;
char y;
float x;
int w;
} s;
int
main(int argc, char *argv[])
{
cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
string z;
char y;
float x;
int w;
};
int
main(int argc, char *argv[])
{
// Allocate from stack ("automatic" variable)
struct amp s;
cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
string z;
char y;
float x;
int w;
};
int
main(int argc, char *argv[])
{
// Allocate from heap
// "new" is a C++ operator, "malloc()" is a C/C++ standard library function
struct amp *s = new struct amp;
cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"\n";
Anyway, the problem is mixing a declaration ("struct amp*s) in with an expression.
You have several alternatives:
Code:
struct amp
{
string z;
char y;
float x;
int w;
} s;
int
main(int argc, char *argv[])
{
cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
string z;
char y;
float x;
int w;
};
int
main(int argc, char *argv[])
{
// Allocate from stack ("automatic" variable)
struct amp s;
cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
string z;
char y;
float x;
int w;
};
int
main(int argc, char *argv[])
{
// Allocate from heap
// "new" is a C++ operator, "malloc()" is a C/C++ standard library function
struct amp *s = new struct amp;
cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"\n";
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.