Searching \ for '[OT] C programming question' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devprogs.htm?key=programming
Search entire site for: 'C programming question'.

Exact match. Not showing close matches.
PICList Thread
'[OT] C programming question'
2006\04\24@153701 by William Couture

face picon face
Let's make sure I haven't lost my mind:

The code snippet:

     struct foo {char a;} one = { 1 };
     struct foo *baz[] = { &one };
     struct gorp { char b; struct foo *c[]; } plop[] = { 2, baz };
     struct foo *bar[];
     main() { bar = plop[0].c; }

should be legal, correct?

Thanks,
  Bill

--
Psst...  Hey, you... Buddy...  Want a kitten?  straycatblues.petfinder.org

2006\04\24@154756 by Marcel Birthelmer

picon face
I think the problem is that
struct foo baz[] and struct foo* baz are both of type struct foo*, whereas
struct foo* baz[] is of type struct foo**.
- Marcel

On 4/24/06, William Couture <spam_OUTbcoutureTakeThisOuTspamgmail.com> wrote:
{Quote hidden}

> -

2006\04\24@164043 by John Pfaff

picon face
William Couture wrote:
> Let's make sure I haven't lost my mind:
>
> The code snippet:
>
>       struct foo {char a;} one = { 1 };
>       struct foo *baz[] = { &one };
>       struct gorp { char b; struct foo *c[]; } plop[] = { 2, baz };
>       struct foo *bar[];
>       main() { bar = plop[0].c; }
>
> should be legal, correct?
>
> Thanks,
>    Bill
>
> --
> Psst...  Hey, you... Buddy...  Want a kitten?  straycatblues.petfinder.org
>
>  
Try this:

     struct foo {char a;} one = { 1 };
     struct foo *baz[] = { &one };
     struct gorp { char b; struct foo **c; } plop[] = { 2, baz };
     struct foo **bar;
     main() { bar = plop[0].c; }

If you're getting compile errors, could you post them?  Or are they
runtime errors?

2006\04\24@170454 by Gerhard Fiedler

picon face
William Couture wrote:

> Let's make sure I haven't lost my mind:

This is always a good thing to do :)

>       struct foo {char a;} one = { 1 };
>       struct foo *baz[] = { &one };
>       struct gorp { char b; struct foo *c[]; } plop[] = { 2, baz };

First, I don't think you can have an array with undefined length in a
struct. Second, baz is of type struct foo**, wheras an initializer for the
array c would expect a series of struct foo*. I'm not sure what you want,
but this works and is something similar to what you may have intended:

 struct gorp { char b; struct foo **c; } plop[] = { 2, baz };

>       struct foo *bar[];
>       main() { bar = plop[0].c; }

bar is not an lvalue; that is, you can't assign anything to it. You can
either make bar a struct foo** (which is something similar and /is/ an
lvalue), or you can assign something to bar[0] (e.g. plop[0].c[0]) (but of
course only after you have reserved storage for it, for example through the
definition struct foo *bar[1];).

While pointers and arrays (and therefore pointers to pointers and arrays of
pointers) are quite similar in C and in some instances can be used
interchangeably, they are different. It's always good to be very clear
about why you make a variable a pointer or an array.

A pointer may point to a datum of a certain type or to a sequence of data
of identical types (which could be, and typically would be, an array), and
it can be set to point to any datum of that type; it is an lvalue. A
pointer has after its definition only memory space set aside for an
address.

An array is a specific sequence of a defined number of data of a certain
type in memory. It can be initialized (with specific data or with a
length), but it cannot be set to any value (only array members can be set),
and therefore it is not an lvalue. An array has, after its initialization,
memory space set aside for everyone of its members. It has no memory space
set aside for its address (which is one way to explain why the pointer is
an lvalue -- there is a memory space for the address, therefore you can
assign it that address --, and the array is not an lvalue: there is simply
no memory location where that address could get stored).

It is often helpful to picture what exactly happens in memory with a
certain definition.

Gerhard

2006\04\24@193412 by Byron A Jeff

face picon face
On Mon, Apr 24, 2006 at 03:37:01PM -0400, William Couture wrote:
> Let's make sure I haven't lost my mind:
>
> The code snippet:
>
>       struct foo {char a;} one = { 1 };
>       struct foo *baz[] = { &one };
>       struct gorp { char b; struct foo *c[]; } plop[] = { 2, baz };
>       struct foo *bar[];
>       main() { bar = plop[0].c; }
>
> should be legal, correct?

Nope. bar doesn't have a size specification (same problem with c in gorp).
baz was declared as length 1 due to the initializer.

Make c and bar for ** and then you should be alright.

Here are the errors in your code from the GCC compiler:

cc     testme.c   -o testme
testme.c:3: warning: initialization from incompatible pointer type
testme.c:3: error: initialization of flexible array member in a nested context
testme.c:3: error: (near initialization for `plop[0].c')
testme.c: In function `main':
testme.c:5: error: `bar' has an incomplete type
testme.c: At top level:
testme.c:4: warning: array `bar' assumed to have one element
make: *** [testme] Error 1

Changing both field c and bar to foo ** gave compilation with no errors.

BAJ

>
> Thanks,
>    Bill
>
> --
> Psst...  Hey, you... Buddy...  Want a kitten?  straycatblues.petfinder.org
>
> --

2006\04\24@214426 by kravnus wolf

picon face
The code below is correct.
#include <stdio.h>

struct foo {char a;} one = { 1 };
struct foo *baz[] = { &one };
struct gorp {
  char b;
  struct foo **c;   <--
}plop[] = {2, baz};
struct foo **bar;    <--
int main()
{
       bar = plop[0].c;
}


I have changed the following lines b'cos:
a) struct foo *c[]
c is an array of pointer but there is no allocation
for the array size.  Therefore when you set
plop[] = {2, baz}, the compiler DOES not know where to
assign the baz to variable in struct foo *c[] since
there is no allocation to store the pointer.
b)  struct foo *bar[];
The same problem as (a). Once again no allocation for
the size for array of pointers. The folloing line does
not allocation array size.
*bar = plop[0].c;*
If you want the compiler to allocation the array size
automatically then you should place it in the
definition line.
struct foo *baz[] = { &one };

regards,
john




--- William Couture <.....bcoutureKILLspamspam@spam@gmail.com> wrote:

{Quote hidden}

> --

More... (looser matching)
- Last day of these posts
- In 2006 , 2007 only
- Today
- New search...