Struct question

G

greg

Hi All.
I am wondering if the following is legal


strcuct P1
{
int i;
double *};

main()
{
struct P1 per;
per.P1=(double*)malloc(sizeof(double)*100);
}


--
Grigoris Lionis
PhD Student
Control Systems Lab, Mechanical Eng. Dept.
National Technical University of Athens
9 Heroon Polytechniou Str., Zografou
Athens 15700, Greece
 
A

Allan Bruce

greg said:
Hi All.
I am wondering if the following is legal


strcuct P1
{
int i;
double *};

main()
{
struct P1 per;
per.P1=(double*)malloc(sizeof(double)*100);
}

no, you need to specify the struct member to malloc. You have per.P1 which
wont compile!

if you had this, it would compile and be valid (untested):

strcuct P1
{
int i;
double *a;
};

int main(void)
{
struct P1 per;
per.a = malloc(sizeof(double)*100);

free(per.a)

return 0;
}
 
D

Darrell Grainger

Hi All.
I am wondering if the following is legal


strcuct P1

I assume you mean struct and not strcuct. As it stands this will not
compile.
{
int i;
double *};

Where is the variable name for the double*?

New to the newsgroup I see. There are two errors here. Find and read the
comp.lang.c FAQ to find out more.
{
struct P1 per;
per.P1=(double*)malloc(sizeof(double)*100);

If I asked why you are casting the result of malloc, would the answer be
to make the warning go away? If yes then you really need to 1) read the
FAQ and 2) scan the newsgroup for old messages relating to this.

Short answer. This code will not compile. Even if you tweaked it enough to
get it to compile it is not good. You'll have to define what 'legal' means
in this context.

I strongly recommend going to www.google.ca and searching for
comp.lang.c FAQ. Once you find it read it. There is a lot it can teach
you. Second, go to http://www.google.ca/grphp and search for
 
A

Alex Monjushko

greg said:
Hi All.
I am wondering if the following is legal


#include said:
strcuct P1

Surely you mean 'struct'.
{
int i;
double *};

You can't do that. You have to specify a variable name, such as
double * abc;

This is not a valid definition of main. Use either
int main(void)
or
int main(int argc, char *argv[])

{
struct P1 per;
per.P1=(double*)malloc(sizeof(double)*100);

There is no element P1 in 'struct P1'. With my fix, you can now
do this:
per.abc = malloc(sizeof(double) * 100);
or
per.abc = malloc(100 * sizeof *per.abc);

Notice that you do not need to cast the return value of malloc
if it has a prototype in scope (i.e. #include <stdlib.h>).

Finally, since main is supposed to return an int:

return 0;
 
E

Emmanuel Delahaye

greg said:
I am wondering if the following is legal

strcuct P1
Typo.

{
int i;
double *};

No. You need an identifier here.
main()
{
struct P1 per;
per.P1=(double*)malloc(sizeof(double)*100);

P1 is not a member of per.

Well, once 'strcuct' and the missing identifier is fixed, it's supposed to
compile, but :

- You forgot to supply a prototype for malloc().
- You forgot to free the allocated bloc.
- You forgot to return a valid value from main().
- The indentation is ugly
- The way you have done the allocation can be enhanced.

Try this:

#include <stdlib.h>

struct P1
{
int i;
double *array;
};

int main (void)
{
struct P1 per;
per.array = malloc (sizeof *per.array * 100);

/* ... */

free (per.arr);
return 0;
}
 
A

August Derleth

(untested):

strcuct P1

Obviously.

I wonder if `#define strcuct struct' would help the language, if it were
included in a standard header file.

Another bad proposal for C2099. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top