简体   繁体   中英

What happens if I enter a character to a dynamically/statically allocated double array?

Right now I am using a dynamically allocated double array. How can I check if someone has entered a character?

double* scanarray(int length)
{
    double* arr;
    arr = (double*)malloc(sizeof(double)*length);
    printf("Enter %d real numbers: \n", length);
    for (int loop = 0; loop < length; loop++)
        scanf_s("%lf", &arr[loop]);
    return arr;
}

You could check return value of scanf (and related functions such as scanf_s ):

Return Value

Each of these functions returns the number of fields that are successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.

https://msdn.microsoft.com/en-us/library/t6z7bya3.aspx

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