I have a variable of type int array[10]
. Is it possible to initialize only the last item of the array?
Yes, using designated initializers (introduced in C99), you can write code like this:
int array[10] = {[9] = 42};
which is equivalent to:
int array[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 42};
This feature is also available in some compiler as an extension, for instance, GCC .
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.