0

I have a struct definition that will need to be used by multiple different functions. In my struct definition, I have an array that is of size "len" and this len variable is the length of the string in argv[1]. As you might see, I need this variable but I cannot place the struct definition outside of main, or else I lose that variable. But I do need to place it before the prototype of one of my functions. What's the solution to this problem? Here is my code:

void randomize( type_darwin *darwin, type_monkey *monkey );

int main( int argc, char *argv[] )
{
    if ( argc < 2 )
    {
        printf("Error! Arguments are of form: ./%s [string to parse]\nBe sure your string is surrounded by double quotes.\n", argv[0]);
        return 0;
    }

    int len = strlen(argv[1]);              // length of string
    const char *string = argv[1];           // make string a constant

    // define two structs, one for the Darwinian algorithm and one for the monkey bashing algorithm
    // lock determines whether that element in sentence is locked (in programming terms, if it should now be a constant)
    typedef struct darwin
    {
        char sentence[len];
        int lock[len];  // 0 defines not locked. Nonzero defines locked.
    } type_darwin;

    typedef struct monkey
    {
        char sentence[len];
        int lock;   // 0 defines entire array not locked. Nonzero defines locked.
    } type_monkey;
6
  • VLAs are not allowed in a struct anyway. Commented Jun 14, 2016 at 17:56
  • In any case you will probably need one more as in char sentence[len+1]; to allow for a terminator. Commented Jun 14, 2016 at 17:56
  • @WeatherVane do argv arguments not contain null terminators? I found the size of the argument at argv[1] using strln. Does that not include any terminators? Commented Jun 14, 2016 at 18:01
  • Yes the argv[] do include the terminator, but strlen does not, it just uses the '\0' as an end marker. Commented Jun 14, 2016 at 18:03
  • @WeatherVane I see, so in that case I should just increment the len variable to include space in future arrays for a terminator. Thanks! Commented Jun 14, 2016 at 18:12

1 Answer 1

3

The structures need to have a consistent compile-time definition to be used in this fashion. You're better off using pointers instead of static arrays and allocating space for the arrays dynamically. You'd then need to make the length part of the struct.

typedef struct darwin
{
    int len;
    char *sentence;
    int *lock;  // 0 defines not locked. Nonzero defines locked.
} type_darwin;

typedef struct monkey
{
    int len;
    char *sentence;
    int lock;   // 0 defines entire array not locked. Nonzero defines locked.
} type_monkey;


int main(int argc, char *argv[] )
{
    if ( argc < 2 )
    {
        printf("Error! Arguments are of form: ./%s [string to parse]\nBe sure your string is surrounded by double quotes.\n", argv[0]);
        return 0;
    }

    int len = strlen(argv[1]);              // length of string
    const char *string = argv[1];           // make string a constant

    type_darwin my_darwin;
    my_darwin.len = len;
    my_darwin.sentence = malloc(len + 1);
    my_darwin.lock = malloc((len + 1) * sizeof(int));

    type_monkey my_monkey;
    my_monkey.len = len;
    my_monkey.sentence = malloc(len + 1);

    ...

}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your reply. One question, should the malloc for sentence not be len*sizeof(char)?
sizeof(char) is 1 by definition.
@WeatherVane really? Interesting, I did not know that. That makes sense though, you only need one byte to represent an ASCII character.
@UnclePutin "letters" and char are not bound together with or without ASCII. char is simply a signed or unsigned integer type of CHAR_BIT bits (usually 8). But the expression 'A' has the type int. Try printing sizeof 'A'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.