INTERVIEW QUESTIONS
C
ARRAYS IN C
DETAILS
Question: Can the size of an array be declared at runtime?
Answer: No. In an array declaration, the size must be known at compile time. You can’t specify a size that’s known only at runtime. For example, if i is a variable, you can’t write code like this:
char array[i]; /* not valid C */
Some languages provide this latitude. C doesn’t. If it did, the stack would be more complicated, function calls would be more expensive, and programs would run a lot slower. If you know that you have an array but you won’t know until runtime how big it will be, declare a pointer to it and use malloc() or calloc() to allocate the array from the heap.
|
Question:
Can the size of an array be declared at runtime?
Answer:
No. In an array declaration, the size must be known at compile time. You can’t specify a size that’s known only at runtime. For example, if i is a variable, you can’t write code like this:
char array[i]; /* not valid C */
Some languages provide this latitude. C doesn’t. If it did, the stack would be more complicated, function calls would be more expensive, and programs would run a lot slower. If you know that you have an array but you won’t know until runtime how big it will be, declare a pointer to it and use malloc() or calloc() to allocate the array from the heap. Source: CoolInterview.com
This is valid as per latest C standards. Try the following (I tried this on Linux, gcc version 4.1.2)
#include <stdio.h>
int my_fn(int size) { char s[size];
memset(s,0,size); memset(s,'1',size-1);
printf("%s ",s);
return 0; }
int main(int argc, char *argv[]) { int arg=10;
if ( argc == 2 ) { arg=atoi(argv[1]); printf("New arg = %d ",arg); } my_fn(arg);
return 0; }
Run ./a.out 5, ./a.out 15 etc Source: CoolInterview.com
Answered by: sbs | Date: 5/2/2009
| Contact sbs
Yes. consider the piece of code shown below,
main() { int size; printf("Enter array size"); scanf("%d",&size); int array[size]; /* Remaining code . */ }
Theoratically,this is true but this type of code is not implemented in real-time application.This type of code is poor programming practise but still the code works. Source: CoolInterview.com
Answered by: Lohit.A.H | Date: 7/28/2009
| Contact Lohit.A.H
In MSVC, you can't as suggested but in GNU C you can. For example, the code below is perfectly legitimate:
main(){ int size; scanf("%d", &size); int arr[size];
int i; for(i=0; i<size; ++i){ arr[i] = i; printf("%d ", arr[i]); } } Source: CoolInterview.com
Answered by: oguzhan | Date: 9/7/2009
| Contact oguzhan
size can be given at run time also... ex- int n; scanf("%d",&n); int a[n];
and it works successfully Source: CoolInterview.com
Answered by: ajay | Date: 10/16/2009
| Contact ajay
no,we cant give array size at runtime ...in subscript operator,there can be a integer or name of macro....there can not be anything else like constant and variable. Source: CoolInterview.com
Answered by: neha | Date: 8/16/2010
| Contact neha
Can an array be allocated mem at run time. No, never. All answers given have a common problem. See the question is can an array be defined without size and then allocated memory, that doesn't happen in any answers given.
void main() { int n; scanf("%d",&n); // now give input int a[n]; // which means array is given its size as 'n'. Now it is static } Then can u modify its size, no, impossible. Try and understand pointer.
A pointer is defined without any size and can be allocated memory later and can also be modified later. That is dynamic. Source: CoolInterview.com
Answered by: Sivakumar S K | Date: 8/19/2010
| Contact Sivakumar S K
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|