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.
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: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.Code:'time pts[4] = {{4,8,0.75}, {5,10,0.00457}, {27,75,3.4}};'
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.
I have outlined the line in question in a comment above - i cant see how 'numbers', 'i', or 'qty' can be wrong. thanksCode:#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); }
@ line 22 you use array subscripting
but you never declared 'numbers' to be an array.Code:for(i=0; i<qty ; i++) numbers[i] = i+1;
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
yes i can now see that problem - but dont know how to remedy it
Last edited by MikeyIckey; 01-08-2009 at 11:08 AM.
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
Besides equating a struct to an int value, there is the additional problem of malloc()'ing memory for storing the time struct.
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