简体   繁体   中英

How to print summary of array elements in C

i'm trying to print a summary of integers in an array. For instance if the user types: "1 4 5 8" The result should be: "1 5 10 18" (0+1= 1, 1+4= 5, 5+5= 10, 10+8= 18) when I run this code with input: "1 1 1" first time result is: "1 2 3 32767 256" and any other time it runs as expected :\\ The algorithm to do this is simple and it works but I'm used to OOP and just started learning C. Something here doesn't seem right..something with the pointers or malloc? any help? What is the "right" way of implementing this? Cheers..!

#include <stdio.h>
#include <stdlib.h>
#define MAX 50

/*function prototype*/
int* summary(int a[], int length);
void printArray(int array[], int i);

int main(){
    int k;
    int length=0;
    int userArray[MAX];
    printf("Enter some numbers:\n");

    while((scanf("%d", &k) ==1)){
        userArray[length] = k;
        length++;

        /*print the array after 'Enter' key pressed*/
        if(getchar() == '\n'){
            summary(userArray, length);
            length = 0;
            printf("\n");
        }/*if*/ 
    }/*while*/

    summary(userArray, length);
    printf("\n");

    return 0;
}/*main*/

int* summary(int a[], int length){
    int i;
    int counter = 0;
    int *p;
    p = (int *) malloc(length * sizeof(int));

    if(!p){
        printf("memory allocation failed!");
        exit(0);
    }/*if null*/


    for(i=0 ; i<length ; i++){
        counter+=a[i];
        p[i] = counter;
    }/*for*/

    a[length] = '\0';
    printArray(p, 0);
    return p;
    free(p);
}/*summary*/

void printArray(int array[], int i){
    if(array[i]=='\0'){
        printf("finished printing");
        return;
    }
    else{
        printf(" %d\n", array[i]);
        printArray(array, i+1);
    }
}/*printArray*/

The problem is in the lines

a[length] = '\0';
printArray(p, 0);

printArray iterates until it finds an element with value 0. You've tried writing a zero value beyond the end of a then passed array p into printArray . p doesn't have any zero element so printArray continues reading beyond it for an unpredictable time until it either crashes or finds memory with value 0.

To fix this, you should allocate space for length+1 elements in p then set p[length] = 0 before calling printArray . Alternatively, you could change the second argument to printArray to be the array size and reimplement it as

void printArray(int* array, int size){
    for (int i=0; i<size; i++) {
        printf(" %d\n", array[i]);
    }
    printf("finished printing");
}/*prinArray*/

You also need to decide what to do with the memory you allocate for p in summary . You currently return this from the function then try to free it. The free(p) line will never be executed since the preceding return statement returns control to the calling function. Since no caller of summary makes use of the return value, the easiest fix would be to change summary to return void and remove the return p line.

Note that this last issue should be flagged by the compiler if you enable warnings. (Add -Wall to your command line for gcc or /W4 for MSVC)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM