arrays in structs

This is a discussion on arrays in structs within the C Programming forums, part of the General Programming Boards category; sorry, i couldnt think of a better title. What im trying to do is to make a CAS system, and ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    arrays in structs

    sorry, i couldnt think of a better title. What im trying to do is to make a CAS system, and i need to define certain things, such as a "term". a term consists of a coefficient and symbols, such as xyz. its a bit more complicated but you get the picture. Ive got something working on python, and now im porting it to c, i dont really have much experience with c which is why i need a bit of help.

    Firstly, how do you pass strings to structs. For example
    Code:
    typedef struct test
    {
            char str[30];
    } test;
    
    void main()
    {
        test test;
        test.str[30] = "test";
        printf(test.str);
    }
    outputs a whole bunch of random characters.
    also, my struct for a term will look something like this.
    Code:
    typedef struct Term
    {
        coefficient coefficients[];
        symbol symbols[];
        bracket brackets[];
    } Term;
    The problem is is i don't know how many coefficients, symbols, or brackets each term is going to have. This is a problem because I want to put this onto my calculator, which has about 100kb of free ram if im lucky, so having an array of 26 symbols just to make sure i can fit them all will result in huge wastage. is there a method to get around this. Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by *DEAD*
    Firstly, how do you pass strings to structs.
    Use strcpy() or malloc(), like you ordinarily would.

    The problem is is i don't know how many coefficients, symbols, or brackets each term is going to have.
    Use a linked list.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > void main()
    main always returns an int

    > test.str[30] = "test";
    You can't assign one array contents to another, so you need something like
    strcpy( test.str, "test" );

    > printf(test.str);
    Never use a variable string as the control string for a printf statement.
    If the user ever gets control of that string, then bad things can happen.
    http://en.wikipedia.org/wiki/Format_string_attack

    > Term...
    Does this work as an alternative?
    Code:
    typedef struct Term
    {
        coefficient coefficient;
        symbol symbol;
        bracket bracket;
    } Term;
    
    Term Terms[10];
    If so, then this is the first step to either forming a variable length array or a linked list of terms.

    How big is each member of the struct, and do you really need to store each one for every single term in the expression?. Symbol and bracket seem to be mutually exclusive to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    i have something that works in python, had to get the theory working before i actually started programming, so i want to keep all my ideas the same. The linked list sounds good tho.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 11:50 AM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  3. Using strcpy() and arrays of structs
    By vital101 in forum C Programming
    Replies: 3
    Last Post: 04-26-2007, 10:04 PM
  4. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 08:38 AM
  5. Structs and arrays...
    By Verdagon in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 12:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21