i was watching an exercise in my textbook that says: Create a C program that take from the keyboard an array with length "N".
The question is: In C language, how can i create an undefined length array?
Thank you all.
i was watching an exercise in my textbook that says: Create a C program that take from the keyboard an array with length "N".
The question is: In C language, how can i create an undefined length array?
Thank you all.
Do not create an array of undefined length.
After getting the needed length N, if C99 use a VLA (Variable Length Array)
int A[N];
... or allocate memory
int *A = malloc(sizeof *A * N);
...
// use A
...
free(A);
[Edit]
Good to add validation on N before proceeding. Example:
if (N <= 0 || N >= Some_Sane_Upper_Limit_Like_1000) return;
int A[N]; i receive an error: Use of undeclared identifier 'N'.N. Suggest 2nd method. If that fails, post your code.N < 0, but I disagree about the checks for N == 0 and N >= Some_Sane_Upper_Limit_Like_1000. I've seen such limits transform into really nasty bugs when the limit, which seemed to be insanely large to the programmer who wrote it, was surpassed by a valid use case. Now, whether you need to check for N == 0 is entirely up to the requirements of the subsequent processing (it may be sensible, or it may be just another unrequired limitation of usecases), but I regard any arbitrary upper limits as bugs waiting to strike.for more information about VLA in c99 The New C:Why Variable Length Arrays?
#include <stdlib.h>
int main(){
int n;
printf("size of array: ");
scanf("%d",&n);
// limit, and erro verification omited
int f[n],i;
printf("\nREADING INPUT\n");
for(i=0;i<n;i++){
printf("\tValue of f[%d]: ", i);
scanf("%d",f+i);
}
/*
calc_max(f,n);
calc_min(f,n);
calc_avg(f,n);
*/
printf("\n\nPRINTING VALUES:\n");
for(i=0;i<n;i++){
printf("\tValue of f[%d]= %d \n", i,f[i]);
}
return 0;
}
It is not easy to satisfy 100% your needs, but you can give it a try:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *addMe(void);
int main(void){
char *test = addMe();
free(test);
return 0;
}
char *addMe(void){
unsigned int maxLength = 15;
unsigned int i =0;
char *name;
int c;
name = malloc(1);
if(name == NULL){
exit(1);
}
printf("Enter your name:> ");
while ((c = getchar()) != '\n' && c != EOF) {
name[i++] = (char) c;
if (i > maxLength) {
printf("Names longer than %d not allowed!\n",maxLength);
name[maxLength] = '\0';
break;
}else if (i < maxLength){
name = realloc(name, maxLength + 2);
}
}
name[i] = '\0';
printf("Your name is:> %s\nThe length is:> %zu\n",name ,strlen(name));
return name;
}
Output:
Enter your name:> l Your name is:> l The length is:> 1
As you probably noticed I allocated memory only for one letter, and if you need more it will be allocated to satisfy your needs. You can also with this approach to have control of a maximum size:
Enter your name:> Michael jacksonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn Names longer than 15 not allowed! Your name is:> Michael jackson The length is:> 15
Like I said, give it a try.
}else if (i < sizeof(char) * 15){ be }else if(i < maxLength){ ?else if(i < maxLength)? Both are OK. (i < sizeof(char) * 15) is the same as (i < maxLength) in my Example. I had my reasons why I used (i < sizeof(char) * 15)