dynamically allocate a structure

This is a discussion on dynamically allocate a structure within the C Programming forums, part of the General Programming Boards category; I am programming C on a VAX. I am trying to define a structure using an extern variable. struct RF_MOVES ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    dynamically allocate a structure

    I am programming C on a VAX. I am trying to define a structure using an extern variable.

    struct RF_MOVES rf_move_struct_p[rf_max_moves_ex]

    The value, rf_max_moves_ex, is derived by reading a system logical early in a calling program. The value is then assigned to an extern variable. The structure is then defined in one of the calling program using the extern variable. When I compile this, I get the following message:

    In the declaration of "rf_move_struct_p", "rf_max_moves_ex" is not constant, but occurs in a context that requires a constant expression.

    I am pretty sure you can do this in VB, but that is not an option, looking for a way to accomplish this using C. Any suggestions would be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > struct RF_MOVES rf_move_struct_p[rf_max_moves_ex]

    Is declared like this
    struct RF_MOVES *rf_move_struct_p;


    And space is actually allocated (when you know rf_max_moves_ex) as follows

    rf_move_struct_p = malloc ( rf_max_moves_ex * sizeof(struct RF_MOVES) );

    You can then access rf_move_struct_p[i] like any normal array.

    Don't forget to
    #include <stdlib.h>

    And please remember to use a C compiler to compile it

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    2
    Thanks for the timely reply

    I tried your suggestion and I am now getting quite a few compiler errors in the statements where I reference the structure

    rf_move_struct_p[i-y].move_status = MR$_NO_LOAD;
    .................^
    %CC-E-NEEDPOINTER, In this statement, "rf_move_struct_p" has a signed int type,
    but occurs in a context that requires a pointer.
    at line number 4364 in file
    _______________________________
    Here's a synopsis of what's happening:
    _______________________________

    In the calling program I defined the following (as suggested) above the main:
    struct RF_MOVES *rf_move_struct_p;

    later, I call the calling program with

    sts$value = crp_build_link_list_part(&p_cat_id_no, rf_move_struct_p);

    This compiles cleanly. In the called program, the prototype is

    int crp_build_link_list_part (user_input_cat_id_no, rf_move_struct_p)

    and I have the statement below the main

    rf_move_struct_p = malloc(rf_max_moves_ex * sizeof(struct RF_MOVES));

    When I compile the program I get the message listed at the top for all the times the structure is referenced. This is probably simple, but I'm not seeing it. I tried using the -> reference, but I got the same results.

    Thanks for your help so far

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > This compiles cleanly. In the called program, the prototype is
    > int crp_build_link_list_part (user_input_cat_id_no, rf_move_struct_p)

    Doesn't look much like a prototype to me

    Should be something like

    int crp_build_link_list_part ( int *user_input_cat_id_no, struct RF_MOVES * rf_move_struct_p) ;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically allocate a char
    By manzoor in forum C++ Programming
    Replies: 8
    Last Post: 10-08-2008, 08:05 AM
  2. Dynamically allocate a pointer?
    By theJ89 in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2006, 03:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Dynamically allocate size of array for strings
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-04-2002, 06:06 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

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