473,499 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct question

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


Nov 14 '05 #1
6 1604

"greg" <gl*********@mail.ntua.gr> wrote in message
news:c8***********@ulysses.noc.ntua.gr...
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;
}

Nov 14 '05 #2
greg wrote:
Hi All.
I am wondering if the following is legal

strcuct P1
Nope: "struct".
{
int i;
double *};
Nope: need element name.
main()
{
struct P1 per;
per.P1
Nope: "per" has no "P1" field.
=(double*)malloc(
Discouraged (casting of malloc result in C), see FAQ.

sizeof(double)*100); }


Not legal. Not nice. Not time to go home yet ...

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #3
On Fri, 21 May 2004, greg wrote:
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*?
main()
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
groups:comp.lang.c <some topic>. You will often find answers to your
questions just be searching old messages.

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



--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #4
greg <gl*********@mail.ntua.gr> wrote:
Hi All.
I am wondering if the following is legal

#include <stdlib.h> /* for malloc */
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;
main()
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; }


--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #5
In 'comp.lang.c', "greg" <gl*********@mail.ntua.gr> wrote:
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;
}

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #6
On Fri, 21 May 2004 14:05:48 +0100, Allan Bruce wrote:
(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. ;)

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
9027
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
19
9199
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
3
2153
by: Emanuele Blanco | last post by:
Hi there, I just compiled a program that uses linked lists (needed it as an homework for my Programming course at University). It works flawlessly, even if I notice a thing. Here's my linked...
67
10649
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
7
4080
by: Allan Adler | last post by:
I think this is a C question rather than a Linux question. I have a copy of Linux Core Kernel Commentary, Scott Maxwell, published in 1999. On p.110, the source for include/asm-i386/current.h...
7
1859
by: Urs Wigger | last post by:
In a C++ project, I have the following struct definition: struct GridModeDataT { double dVal1; double dVal2; double dVal3; long ...
20
2067
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
14
2762
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
21
1976
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first...
4
9779
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.