Array with undefined length in C

后端 未结 3 487
半阙折子戏
半阙折子戏 2021-01-21 09:12

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

相关标签:
3条回答
  • 2021-01-21 09:37

    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;
    
    0 讨论(0)
  • 2021-01-21 09:41

    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.

    0 讨论(0)
  • 2021-01-21 09:43

    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;
    }
    
    0 讨论(0)
提交回复
热议问题