How do I set the length of an array using a value from the user?
That dosen't work^.... Any ideas? Thanx.Code:struct colHeads headers[colNum];
How do I set the length of an array using a value from the user?
That dosen't work^.... Any ideas? Thanx.Code:struct colHeads headers[colNum];
I think you have 2 way.
1) declare the array more bigger than the user will input.
For example, user will input array from 10 - 100 elements of array, So, declare the array bigger than 100.
2) Use dynamic array.
Code:#include <stdio.h> int main() { int nSIZE; double *ptr; int i,j; while(nSIZE != 0) { printf("enter size : \n"); scanf("%d", &nSIZE); ptr = (double *)malloc(sizeof(double)*nSIZE*nSIZE); for(i=0;i<nSIZE;i++) { for(j=0;j<nSIZE;j++) { (ptr[i]) = j; printf("%f", (ptr[i]+j)); } printf("\n"); } free(ptr); } }
array size will be user input, nSIZE.
when user input = 0, the program will stop.
Use malloc
If you don't do sizeof(headers) or &headers, then the rest of the code remains the same.Code:struct colHeads *headers = malloc ( colNum * sizeof *headers );
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.
well thats cool, but maybe you could explain it just a little more?
Once a Freak, Always a Freak.
International Freaks
You could always do something fun like...
And "myprog.c"...Code:#include <stdio.h> int main( void ) { unsigned int arraysize; chat buf[BUFSIZ] = 0; printf("Enter the array size: "); scanf("%u", &arraysize ); sprintf( buf, "gcc -o myprog myprog.c -DARRAYSIZE %u", arraysize ); /* FAQ link for system specific child process spawning... */ return 0; }
Code:#include <stdio.h> int main( void ) { int foo[ARRAYSIZE]; ...do stuff here... return 0; }
Quzah.
Hope is the first step on the road to disappointment.
originally posted by Kosong
should beCode:#include <stdio.h> ...
Code:#include <stdio.h> #include <stdlib.h>
--------------------------take it easy------------------------..................enjoy................
so are those the easiest ways? cause they are still pretty confusing
Once a Freak, Always a Freak.
International Freaks
What's to be confused about?
If you have
struct colHeads headers[200];
Replace it with
struct colHeads *headers = malloc ( 200 * sizeof *headers );
> so are those the easiest ways?
Or use C++, which allows such things
So does the new C standard (called C99), but compilers for that are not yet common.
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'd probably go with malloc to allocate your array. Basicly, set up a pointer, call it whatever your array is going to be called. Use malloc to allocate the amount of memory you need. (See Salem's example.) When you're all done, you call free on the pointer to get rid of your "array". Remember, for every malloc call, you'll need a matching free some place.
[edit]Curses, foiled again.[/edit]
Quzah.
Hope is the first step on the road to disappointment.
ok I think that makes sense
Once a Freak, Always a Freak.
International Freaks
This gives me an error:Code:struct colHeads *headers = malloc( colNum * sizeof *headers );
Code:Cannot initialize 'colHeads near*' with 'void near*'
Once a Freak, Always a Freak.
International Freaks
Lemme guess, you called your source file prog.cpp instead of prog.c
Which means you're compiling your code as C++ and not as C
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.
Are you using a 16-bit compiler?
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
I changed it to .c and I still get the same error.
Once a Freak, Always a Freak.
International Freaks
Or cut to the chase, which compiler?
And a decent sized block of code - like more than the one line which is causing you problems
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.