Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

dumb question

GoblinmanGoblinman Member Posts: 13
Hi..

What kind of type or variable is this: {'x':2;'y':1} ?
If I send that expression as an argument to a function, what variable type shall i write in the function declaration?

Regards
Johan

Comments

  • eggie5eggie5 Member Posts: 36
    : Hi..
    :
    : What kind of type or variable is this: {'x':2;'y':1} ?
    : If I send that expression as an argument to a function, what variable type shall i write in the function declaration?
    :
    : Regards
    : Johan
    :
    :
    The letters are char)s and the numbers are int)s, if that is what you were asking...
  • GoblinmanGoblinman Member Posts: 13
    : : Hi..
    : :
    : : What kind of type or variable is this: {'x':2;'y':1} ?
    : : If I send that expression as an argument to a function, what variable type shall i write in the function declaration?
    : :
    : : Regards
    : : Johan
    : :
    : :
    : The letters are char)s and the numbers are int)s, if that is what you were asking...
    :

    Hi!
    maybe I should be more specific.
    For example if you call a function like this

    func({'x':2;'y':3});

    if you consider {'x':2;'y':3} as one argument, what kind of type would it be then? I saw this expression in a book, but I didnt see any explanation for it, so i thought i could ask here.

    /Johan







  • DariusDarius Member Posts: 1,666
    Give more context from the book.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • compuchipcompuchip Member Posts: 273
    Oh, and Goblinman, only the fact that no-one has been able to answer you so far, means it's not a dumb question.
    You can't ask dumb questions!
  • whoiewhoie Member Posts: 672
    : : : Hi..
    : : :
    : : : What kind of type or variable is this: {'x':2;'y':1} ?
    : : : If I send that expression as an argument to a function, what variable type shall i write in the function declaration?
    : : :
    : : : Regards
    : : : Johan
    : : :
    : : :
    : : The letters are char)s and the numbers are int)s, if that is what you were asking...
    : :
    :
    : Hi!
    : maybe I should be more specific.
    : For example if you call a function like this
    :
    : func({'x':2;'y':3});
    :
    : if you consider {'x':2;'y':3} as one argument, what kind of type would it be then? I saw this expression in a book, but I didnt see any explanation for it, so i thought i could ask here.

    I believe this is some form of compiler extension for a compound literal. The closest thing I know of, is C99's new compound literal, but that has different syntax than what you have shown here. Please provide more context, i.e. the parameter type that your function 'func' is expecting. I can only assume that it is expecting a structure of some sort, but that can only be a guess without more information. No, this is not a dumb question, but you might be on the wrong board.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • stoberstober Member Posts: 9,765 ✭✭✭
    [b][red]This message was edited by stober at 2002-10-27 16:29:9[/red][/b][hr]
    Those are bit values. For example
    [code]
    typedef sturct
    {
    unsigned a:1;
    unsigned b:2;
    unsigned c:1;
    };
    [/code]
    The size of the above struct is sizeof(unsigned). This defines one unsgned int with named bit fields.

    So the expression

    int foo({a:1, b:2})

    would define one parameter that has two named bit fields, a and b.


  • whoiewhoie Member Posts: 672
    : Those are bit values.

    Perhaps in another language, but not in ISO C or ISO C++.


    : For example
    : [code]
    : typedef sturct
    : {
    : unsigned a:1;
    : unsigned b:2;
    : unsigned c:1;
    : };
    : [/code]
    : The size of the above struct is sizeof(unsigned).

    Aside from some syntax errors (misspellings and no typename), the size of that struct is implementation-dependant. There is nothing to preclude an implementation from packing the entire structure into unsigned char, or spreading each member over 3 unsigned longs. The intention is to pack, but that is far from a guarantee.


    : So the expression
    :
    : int foo({a:1, b:2})
    :
    : would define one parameter that has two named bit fields, a and b.

    Again, there are some syntax errors here, but a compound literal(C99) using the above declaration would not work this way. Member initializing is done using the '.' operator like so:
    [code=ffffff]
    foo( (struct bitfield){.a = 1, .b = 2} );
    [/code]

    Now, compare this with the original post:
    [code=ffffff]
    func({'x':2;'y':1});
    [/code]
    First, member names cannot be character constants. Second, the colon is used to assign the number of bits in a bit-field, not initializing the value of the field. Third, a comma, not a semi-colon, is used to separate the initializer list. Fourth, the literal needs to be preceded by a parenthesed typename.

    If the intent was to declare a new type here, then that is again a syntax error, because the struct keyword is missing. So again, I think we need some more context before we can give a definitive answer.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • Andre YoungAndre Young USAMember Posts: 0

    ______ { http://forcoder.org } free ebooks and video tutorials about [ R, PL/SQL, Ruby, Objective-C, C, Perl, Swift, Scratch, PHP, Go, Visual Basic .NET, C++, Assembly, C#, MATLAB, Visual Basic, Java, JavaScript, Python, Delphi Alice, Dart, Lisp, Lua, ML, ABAP, FoxPro, Awk, Rust, Hack, Scheme, COBOL, Kotlin, Clojure, Fortran, Transact-SQL, Scala, Erlang, LabVIEW, Bash, Prolog, F#, Ada, Julia, Crystal, Apex, SAS, D, Logo, VBScript ] _____________

Sign In or Register to comment.