简体   繁体   English

如果在动态/静态分配的双精度数组中输入字符,会发生什么情况?

[英]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 ): 您可以检查scanf返回值(以及诸如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. 返回值为0表示未分配任何字段。 The return value is EOF for an error or if the end of the string is reached before the first conversion. 如果发生错误或在第一次转换之前到达字符串的末尾,则返回值为EOF。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建静态分配的动态大小的数组? - How can I create a statically allocated dynamically sized array? 如果我不释放/删除动态分配的数组会发生什么? - What happens if i don't free/delete dynamically allocated arrays? 在范围结束后,静态分配的内存会发生什么? - What happens to statically allocated memory after its scope ends? 有没有办法在 C++ 中静态初始化动态分配的数组? - Is there a way to statically-initialize a dynamically-allocated array in C++? C ++动态分配静态大小的数组 - C++ dynamically allocated array of statically dimensioned arrays 正常声明数组时,memory 是静态分配、动态分配还是其他方式分配? - When an array is declared normally, is the memory allocated statically, dynamically or otherwise? 没有句柄的动态分配变量会发生什么? (C++) - What Happens To Dynamically Allocated Variables Without Handles ? (C++) 如果我们将一个动态分配的指针分配给另一个指针,会发生什么? - If we assign a dynamically allocated pointer to another pointer, what happens? 动态或静态分配复杂数据成员更好? - Better for complex data members to be dynamically or statically allocated? 如何在C ++中引用动态分配的字符数组 - How to reference a dynamically allocated character array in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM