Sponsored Content
Full Discussion: Struct Initialization
Top Forums Programming Struct Initialization Post 33224 by Perderabo on Friday 20th of December 2002 09:25:08 AM
Old 12-20-2002
I think that I now mostly understand the code that you posted. You're right about gcc. I suggest that you do most of your testing with a C compiler. There is a lot of code out there that happily states "gcc required", a situation simliar to those "IE required" web pages.

You are doing a lot of "typedef struct X { } X". That makes X both a typedef and name of a stucture. Unless you have a very good reason for that, just use "typedef struct { } X".

When you do "#define X_NS_C (X) {...}", what function is the (X) supposed to serve? That is the one part that I can't figure out. I finally just assumed it was a comment and dropped it.

You need a \n in your printf. I never could get my version of gcc to compile your program. When you run your program after a gcc compile, do you get a trailing \n somehow?

The exit(1) is interesting, but I couldn't resist the urge to switch it to exit(0).
Code:
#include <stdlib.h> 
#include <stdio.h> 

typedef struct { 
        int E; 
        int F; 
        } TEST3; 

typedef struct { 
        int C; 
        TEST3 D; 
        } TEST2; 

typedef struct { 
        int A; 
        TEST2 B; 
        } TEST; 

#define TEST_NS_C  { 1, TEST2_NS_C } 
#define TEST2_NS_C { 2, TEST3_NS_C } 
#define TEST3_NS_C { 6, 8 } 

main(int argc, char *argv[]) 
{ 
        TEST T = TEST_NS_C;
        printf("%d %d %d %d \n", T.A, T.B.C, T.B.D.E, T.B.D.F); 
        exit(0); 
}

Of course, in real life, I would lose those #defines and just go with:
TEST T = { 1, { 2, { 6, 8 } } };
but I guess that you need those defines for some reason. Do you really need to split them to several lines? Even machine generated C code should be readable.

I have tried my code on several c compilers. HP's c++ compiler issued a warning because main() is not typed, but the resulting program ran fine. Other than that, it compiled fine everywhere, including with gcc.

You lost me with that NO_STATEMENT stuff...I hope that wasn't too important.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell initialization files

As you know, when a user logs in, the shell reads the initialization files in an order something like below... ################### Bourne Shell /etc/profile > $HOME/.profile Bash Shell /etc/profile > $HOME/.bash_profile > $HOME/.bash_login > $HOME/.profile > $HOME/.bashrc C Shell... (3 Replies)
Discussion started by: SeanWuzHere
3 Replies

2. Programming

Char initialization

Hi All, char a="\0"; a) a contains \0 a contains garbage value b) a contains \ a contains 0 a contains garbage value Pls, let me know correct result is a or b. I guess a. Thanks, Naga:cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

3. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

4. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

5. Shell Programming and Scripting

Declaring variables without initialization

I get an error in my shell script that line 1: )unexpected. Line 1 in my script (using sh by the way) is the variable I declared but did not initialize it. result= Is this wrong? How can I fix it? I am using the variable later in the program, so I figured I could just declare it first... (4 Replies)
Discussion started by: itech4814
4 Replies

6. Shell Programming and Scripting

Initialization error

new to shell scripting. below line is showing error in script. ${parameter:=word} in the o/p first it shows the below error. word: not found. and then in next line print "word" ---------------- p2: word: not found. word --------------------------- OS is AIX and shell is... (12 Replies)
Discussion started by: scriptor
12 Replies

7. UNIX for Advanced & Expert Users

openmpi - initialization failed

Hi everybody, my problem is the following: I'm trying to run an openmpi program on a cluster (atlasz.elte.hu, it's in hungarian, but you can try google translate), but I always got this error: Fatal error in MPI_Init: Other MPI error, error stack: MPIR_Init_thread(394)...........:... (0 Replies)
Discussion started by: jkobori
0 Replies

8. Programming

C- static initialization of structures

Situation: I have an array of structures: struct my_struct_type { char tags_sn; char group_c; char is_err_c; }; struct my_struct_type stuff = { "abcd", 'A', 'E', "efgh", 'B', 'E', "ijkl", 'C', 'E' NULL, '\0', '\0' ... (14 Replies)
Discussion started by: garysk
14 Replies

9. Shell Programming and Scripting

Variable initialization

Hallo Team, I have a simple request and i would appreciate your help. I would like to use two dates in my script lets: A=$(date +"%d %B %Y") echo $A 23 June 2014 That's awesome now we cooking. Now i want B to be on the previous month eg: echo $B Should give me 23 May 2014 I would... (9 Replies)
Discussion started by: kekanap
9 Replies

10. Shell Programming and Scripting

Error occurred during initialization of VM

Hi , I was invoking a sh file using the nohup command. But while invoking, I received a below error. Error occurred during initialization of VM Unable to load native library: /u01/libjava.so: cannot open shared object file: No such file or directory . Could you please help out. Regards,... (2 Replies)
Discussion started by: Kamal1108
2 Replies
Struct(3)						User Contributed Perl Documentation						 Struct(3)

NAME
Inline::Struct -- Manipulate C structures directly from Perl. SYNOPSIS
use Inline C => Config => Structs => ['Foo']; my $obj = Inline::Struct::Foo->new; $obj->num(10); $obj->str("Hello"); myfunc($obj); __END__ __C__ struct Foo { int num; char *str; }; void myfunc(Foo *f) { printf("myfunc: num=%i, str='%s' ", f->num, f->str); } This complete program prints: myfunc: num=10, str='Hello' DESCRIPTION
Inline::Struct is not a new language. It's a language extension designed to be used by Inline::C. It parses struct definitions and creates typemaps and XS code which bind each struct into a Perl class. This code is passed to Inline::C, which compiles it in the normal way. NOTE: Inline::Struct parses only C-style structs. It doesn't know about any C++ extensions to structs like scopes, constructors or methods. If you want such functionality you should use Inline::CPP to parse your structs. Using Inline::Struct Inline::Struct has a Parse::RecDescent grammar to parse C structs. If a struct is recognized, it can be bound to Perl. If the struct's definition is not recognized (usually because it has a member with no typemap), it will not be bound to Perl, but will be available from other functions in C or C++. The following example shows how a simple struct might look to a Perl programmer. Example 1: use Inline C => <<'END', ENABLE => 'STRUCTS'; struct Fraction { long numer; long denom; }; END my $o = Inline::Struct::Fraction->new(4, 3); print $o->numer, $o->denom, " "; $o->numer(4)->denom(7); After the code above has been compiled, Perl's namespace looks a lot like the following: package Inline::Struct::Fraction; sub new { } sub DESTROY { } sub _KEYS { } sub _VALUES { } sub _HASH { } sub numer { } sub denom { } Note that these are actually XS subs written in C, not Perl subs. But that's what it looks like. The Struct Interface The following sections define the interface of each subroutine. Note: this interface is likely to change in future versions of Inline::Struct. Please don't rely on Inline::Struct in production code quite yet. When a struct is bound by Inline::Struct, a new namespace is created underneath Inline::Struct. So if you have a struct named 'Foo', the package of the Perl class will be 'Inline::Struct::Foo'. new If no arguments are provided, all fields are zeroed out. If you provide values, they should be appropriate for the field type, and in the same order as they are defined in the struct. DESTROY The destructor. Should never be called by the programmer -- this is called automatically when the Perl variable holding the struct is destroyed. Frees the memory associated with the struct. If the struct holds pointers to malloc'd memory, they will not be freed. If you run into such a situation, consider using C++ and Inline::CPP instead. _KEYS A read-only method, this returns a reference to an array containing the names of the fields in the struct. The fields are in the order they appear in the C source code. _VALUES A read-only method, this returns a reference to an array containing the values of the fields in the struct. The values are returned in the same order as the fields. _HASH A read-only method, this returns a reference to a hash, mapping field names to field values. Accessors For each field in the struct, an accessor sub will be created which lets you get or set the value in the struct. If no arguments are provided, the sub returns the value of that field. If any arguments are provided, the field is set to the first argument, and the modified structure is returned. This makes setting multiple fields easy: $o->field1(something)->field2(somethingelse); C and C++ Configuration Options Inline::Struct has no configuration options of its own, but it does provide a new configuration option for C or C++. STRUCTS Specifies that structs are to be bound to Perl. There are several meanings to this option, so I'll explain with an example: use Inline C => Config => STRUCTS => 'Foo'; Adds 'Foo' to the list of structs to bind to Perl. use Inline C => Config => STRUCTS => ['Foo', 'Bar']; Adds 'Foo' and 'Bar' to the list of structs to bind to Perl. use Inline C => Config => STRUCTS => undef; Clears the list of structs to bind to Perl. use Inline C => Config => ENABLE => 'STRUCTS'; or use Inline C => Config => STRUCTS => 1; Enable binding structs to Perl, without specifying any structs to search for. As shown, this would bind all structs to Perl. use Inline C => Config => DISABLE => 'STRUCTS'; or use Inline C => Config => STRUCTS => 0; Disable binding structs to Perl. SEE ALSO
For more information about using C from Perl, see Inline::C. For more information about using C++ from Perl, see Inline::CPP. AUTHOR
Neil Watkiss (NEILW@cpan.org) COPYRIGHT
Copyright (C) 2001, Neil Watkiss. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. (see http://www.perl.com/perl/misc/Artistic.html) perl v5.16.2 2001-05-13 Struct(3)
All times are GMT -4. The time now is 09:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy