help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: equivalent for C-style init: structname varname[] = {...} ?


From: Jordi Gutiérrez Hermoso
Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
Date: Mon, 19 Nov 2012 10:59:20 -0500

On 19 November 2012 10:34, Dimitri Maziuk <address@hidden> wrote:
> On 11/18/2012 11:42 AM, Sergei Steshenko wrote:
>>
>>
>> "have mixed-type fields" - "foo" and "foo bar" are _both_ strings, so it's
>> the same type.
>>
>> I suggest to read http://en.wikipedia.org/wiki/Hash_table first.
>>
>> "two" and 2 would be different types (string vs (integer) number), but
>> it's not what I was talking about.
>
>
> But that was what *I* was talking about:
> {
>  string name;
>  float value;
> }
> which an array or map does not allow. So octave structs are about as similar
> to structs as Pink Floyd is similar to Cherry Garcia ice cream.

Right, if you have in C

    typedef struct {
        const char* name;
        float value;
    } S;

    S s = {"hello", 3.14},

    printf("%s = %f\n", s.name, s.value);

Then in Octave you can do,

    s.name = "hello";
    s.value = single(3.14) ## You wanted single, not double, right?

    printf("%s = %f\n", s.name, s.value);

amongst other possible syntaxes for defining a struct.

Struct arrays are similar in Octave and C too, except that Octave
allows a different way of indexing struct arrays that C doesn't.

- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]