Thread: allocating space dynamically for an array of structures

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    AFAIK, it means that either what you assign to cannot be assigned (ie array), or you are trying to assign something invalid to a variable, or both.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    to clarify what happens in the program - the user is asked to enter how many integers they would like to have for each element - within the structure so that malloc can calculate how much memory to dynamically calculate. The array:
    Code:
    'time pts[4] = {{4,8,0.75}, {5,10,0.00457}, {27,75,3.4}};'
    is only used to test that I could successfully communicate with the structure - I will alter the program so the user can enter values into the array later. For now I would like to discover why I received the error specified when trying to dynamically allocate memory based upon the integers the user inputs. These values are stored in 'qty qty2 and qty3' - and added together in the 'getnumber()' function - This value is then multiplied against 'sizeof', to discover how many bytes to reserve.

    Note - I am aware that I am not using this memory allocated correctly at this point
    Last edited by cus; 01-08-2009 at 10:38 AM.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So post up-to-date, indented code so people can locate the line of error.
    And better yet, mark the line in question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Code:
             #include <stdlib.h>
             #include <stdio.h>
             typedef struct exec_time
        {
            int et;                
            int pn;                
            float at;            
        }time; 
    
          int qty, qty2, qty3, i;
         int getnumber();
         void printnumber();
         time pts[4] = {{4,8,0.75}, {5,10,0.00457}, {27,75,3.4}}; 
    
    int main()
    {
            time *numbers;
            getnumber();
            numbers = malloc( qty * sizeof(int) ); 
            if(numbers != NULL) 
    {
                   for(i=0; i<qty ; i++) numbers[i] = i+1;      /*THIS IS THE LINE IN QUESTION LINE 22/*
                   for(i=0; i<qty ; i++) 
                   printf("%d ", numbers[i]);
                   printf("\n");
                           free(numbers); /*free allocated memory*/
                           printnumber();
                           return 0;
    }
               else {
                        printf("\nMemory allocation failed - not enough memory.\n");
                        return 1;}
    }
    
    
    int getnumber()
    {
         printf("\nHow many ints would you like to store for et? ");
         scanf("%d", &qty);
         printf("\nHow many ints would you like to store for pn? ");
          scanf("%d", &qty2);
         printf("\nHow many floats would you like to store for at? ");
         scanf("%d", &qty3);
                    qty = qty + qty2 + qty3; /*adds to total number of integers to be used with malloc and sizeof*/
                    return qty;
    }
    
    void printnumber() /*simply prints the value of the structure*/
    {
          printf("\n\nCode et= execution time, pn =process number, at = average time\n");
          printf("Variable 'time' at element 0 et: %d pn: %d at: %f\n",pts[0].et, pts[0].pn, pts[0].at);
          printf("Variable 'time' at element 1 et: %d pn: %d at: %f\n",pts[1].et, pts[1].pn, pts[1].at);
          printf("Variable 'time' at element 2 et: %d pn: %d at: %f at:\n\n",pts[2].et, pts[2].pn, pts[2].at);
    }
    I have outlined the line in question in a comment above - i cant see how 'numbers', 'i', or 'qty' can be wrong. thanks

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What makes you think you can assign an integer to a structure?
    And your indentation is all messed up, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by Elysia View Post
    What makes you think you can assign an integer to a structure?
    And your indentation is all messed up, too.
    because i'm a beginner and asking for some help - if i'm doing this the wrong way all i need is some sound advice on how to use malloc to allocate space dynamically for a structure - with the elements stated in the question.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is not about malloc, but you assigning an integer to a non-integer type!
    Code:
    struct foo {};
    int main()
    {
        struct foo myfoo;
        myfoo = 10;
    }
    This is essentially what you are doing. See why it's wrong?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    @ line 22 you use array subscripting
    Code:
    for(i=0; i<qty ; i++) numbers[i] = i+1;
    but you never declared 'numbers' to be an array.
    could that be the problem?
    Just a thought.
    Good luck.
    Last edited by MikeyIckey; 01-08-2009 at 10:59 AM. Reason: code tags didn't work the first time around

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, numbers is a pointer and thus can be used with array notation. However, numbers is a number of structs, not integers, thus it's not a valid assignment.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    yes i can now see that problem - but dont know how to remedy it

  11. #26
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by Elysia View Post
    No, numbers is a pointer and thus can be used with array notation. However, numbers is a number of structs, not integers, thus it's not a valid assignment.
    ooh. ok, i'm still learning that bit myself. Thanks.
    or I guess sorry to chime in on something i'm not totally clear on.
    Last edited by MikeyIckey; 01-08-2009 at 11:08 AM.

  12. #27
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cus View Post
    yes i can now see that problem - but dont know how to remedy it
    You can probably just remove that line. What was it supposed to do? Were you thinking that this would create numbers[0] to numbers[qty]?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #28
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Besides equating a struct to an int value, there is the additional problem of malloc()'ing memory for storing the time struct.

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    Besides equating a struct to an int value, there is the additional problem of malloc()'ing memory for storing the time struct.
    No. That's a typedef definition. There is no struct exec_time time. There is a new variable type, "time", whose prototype was a struct exec_time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cus View Post
    yes i can now see that problem - but dont know how to remedy it
    The question begs what you are trying to do?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. dynamically allocating a 2dim array
    By agerealm in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 02:40 PM
  4. 2d array of structures
    By spudtheimpaler in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 03:17 PM
  5. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 03:59 PM