Initializing a pointer to a structure with a literal 
Author Message
 Initializing a pointer to a structure with a literal

I have the following code:

struct A
{
  int i;

Quote:
};

struct B
{
   struct A *a;

Quote:
};

I want to create a global variable of type struct B and initialize it.
 Currently, I do this:

struct A a = {1};
struct B b = {&a};

What I would prefer to do is this:

struct B b = { {1} };

But the compiler doesn't like it.  I get this error messages (gcc):
"initializer for scalar variable requires one element".

I also tried this:

struct B b = { &{1} };

But that gives me this error: "parse error before `{' token" (followed
by a bunch of other errors).

Is there any way I can define a variable of type struct B without
first defining a variable of type struct A?



Mon, 04 Apr 2005 06:48:07 GMT  
 Initializing a pointer to a structure with a literal

Quote:

> I have the following code:

> struct A
> {
>   int i;
> };

> struct B
> {
>    struct A *a;
> };

<snip>

> Is there any way I can define a variable of type struct B without
> first defining a variable of type struct A?

struct B b = {0};

This will initialise B's 'a' element to NULL, which is the only sensible
value it can have if there are no struct A objects to point at.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Mon, 04 Apr 2005 07:19:03 GMT  
 Initializing a pointer to a structure with a literal

Quote:

> I have the following code:

> struct A
> {
>   int i;
> };

> struct B
> {
>    struct A *a;
> };

> I want to create a global variable of type struct B and initialize it.
>  Currently, I do this:

> struct A a = {1};
> struct B b = {&a};

> What I would prefer to do is this:

> struct B b = { {1} };

> But the compiler doesn't like it.  I get this error messages (gcc):
> "initializer for scalar variable requires one element".

> I also tried this:

> struct B b = { &{1} };

> But that gives me this error: "parse error before `{' token" (followed
> by a bunch of other errors).

> Is there any way I can define a variable of type struct B without
> first defining a variable of type struct A?

struct B b = {1}

Thats what you try to do???......

-------------------

something different isn't always the best, but, always the best is
something different



Mon, 04 Apr 2005 08:28:50 GMT  
 Initializing a pointer to a structure with a literal


Quote:
> I have the following code:

> struct A
> {
>   int i;
> };

> struct B
> {
>    struct A *a;
> };

> I want to create a global variable of type struct B and initialize it.
>  Currently, I do this:

> struct A a = {1};
> struct B b = {&a};

> What I would prefer to do is this:

> struct B b = { {1} };

This is only possible in C99 using compound literals, thus:

struct B b = { &(struct A) {1} };

--
Kevin Bracey
http://www.bracey-griffith.freeserve.co.uk/



Mon, 04 Apr 2005 16:48:59 GMT  
 Initializing a pointer to a structure with a literal

Quote:

>> I have the following code:

>> struct A
>> {
>>   int i;
>> };

>> struct B
>> {
>>    struct A *a;
>> };

>> I want to create a global variable of type struct B and initialize it.
>>  Currently, I do this:

>> struct A a = {1};
>> struct B b = {&a};

>> What I would prefer to do is this:

>> struct B b = { {1} };

>> But the compiler doesn't like it.  I get this error messages (gcc):
>> "initializer for scalar variable requires one element".

>> I also tried this:

>> struct B b = { &{1} };

>> But that gives me this error: "parse error before `{' token" (followed
>> by a bunch of other errors).

>> Is there any way I can define a variable of type struct B without
>> first defining a variable of type struct A?

> struct B b = {1}

> Thats what you try to do???......

Where would b.a point to in that case? And would that be a valid
address to dereference? I doubt so. Kevin Bracey provided a
sollution for C99 and I don't think there is sollution for
C89/C90, despite a null pointer for the a-element of b.
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond


Mon, 04 Apr 2005 19:01:03 GMT  
 Initializing a pointer to a structure with a literal
Try to init it using the following
=================
struct A a={5};
struct B b={&a};


Sat, 09 Apr 2005 19:18:19 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Help in initializing function pointers within structures

2. initialize a pointer to pointer

3. function pointer - as a literal instead of a variable

4. Initializing structures

5. initializing global structures

6. Initializing arrays of structures

7. Initializing an array of structures

8. Initialize char [] as member of structure

9. initializing an array of structures?

10. Initializing structures

11. problem with initializing a structure

12. initializing arrays in structures

 

 
Powered by phpBB® Forum Software