Dynamic Array Allocation in C with Variable Array Size
Variable-sized arrays, also known as dynamic arrays, pose a challenge in standard C. Consider the following program:
n = fread(Sbuf, sizeof(char), siz, picture); /* ... do stuff with the buffer ... */ /* memset(Sbuf, 0, sizeof(Sbuf)); */
}
The code above attempts to allocate an array of characters with a size determined by a variable siz. However, in standard C, array sizes must be constants. This poses the question: how can we declare siz correctly to allow code compilation?
Unfortunately, there is no direct method to declare an array with a variable size in standard C. However, several alternatives exist:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3