Sunteți pe pagina 1din 19

Technical Seminar

on
DESIGNATED INITIALIZER IN C
AND BIT FIELD IN C
by
AVESH KUMAR AGRAWAL
(1502931040)
CSE- VIII(C)
KIET Group of Institutions, Ghaziabad
DESIGNATED INITIALIZER IN C
Normal Initialization of array

main()
{
int a[6] = { 0, 0, 15, 0, 29, 0 };
for(int i=0;i<6;i++)
printf(“%d ”,a[i]);
}
Problem with Normal Initialization
⚫ Requires the elements of an initializer to appear in a
fixed order.
A[6]={1,2,3,4,5,6};
⚫ For initializing last element we need to initialize all
elements before it.
⚫ If a particular range of indices have same value then it
required to initialize all elements separately.
A[6]={1,1,1,1,4,4}
Solution: Designated Initializer
⚫ Standard C90 requires the elements of an initializer to
appear in a fixed order, the same as the order of the
elements in the array or structure being initialized.
⚫ In ISO C99 you can give the elements in random order,
specifying the array indices or structure field names they
apply to, and GNU C allows this as an extension in C90
mode as well. This extension is not implemented in
GNU C++.
To specify an array index, write
‘[index] =’ or ‘[index]’ 
before the element value.

For example,
int a[6] = {[4] = 29, [2] = 15 }; or
int a[6] = {[4]29 , [2]15 };
is equivalent to
int a[6] = { 0, 0, 15, 0, 29, 0 };

Note:- The index values must be constant expressions.


Initializing a range
To initialize a range of elements to the same value,
write ‘[first … last] = value’.

For example,
int a[] = {[0 ... 9] = 1, [50 ... 99] = 2, [100] = 3 };

Note: As with Normal initialization, all uninitialized values


are set to zero.
Size of array
The length of the array is the highest value specified plus
one.

void main(void)
{
    int numbers[] = {1, 2, 3, [10] = 80, 15,
                    [70] = 50, [42] = 400 };
    int n = sizeof(numbers) / sizeof(numbers[0]);
    printf("%d", n);
}
Advantages
● Elements can be initialized in random order.
int a[6] = {[4] = 29, [2] = 15 };
● For initializing last element we do not need to initialize
all elements before it.
int a[6] = {[5] = 2 };
● If a particular range of index have same value then it can
be initialized directly.
int a[] = {[0 ... 9] = 1, [50 ... 99] = 2, [100] = 3 };
BIT-FIELD IN C
Introduction
Bit fields can be used to reduce memory consumption
when a program requires a number of integer variables
which always will have low values. For example, in many
systems storing an integer value requires two bytes
(16-bits) of memory; sometimes the values to be stored
actually need only one or two bits. Having a number of
these tiny variables share a bit field allows efficient
packaging of data in the memory.
Example Without Bit-Field
consider the following declaration of date without use of
bit fields.
struct date
{  
  unsigned int d;
  unsigned int m;
    unsigned int y;
};

Since we know that the value of d is always from 1 to 31, value of m is from 1
to 12, we can optimize the space using bit fields.
Example With Bit-Field
A space optimized representation of date
struct date
{
   unsigned int d: 5;  
// d has value between 1 and 31, so 5 bits are sufficient
   unsigned int m: 4;
// m has value between 1 and 12, so 4 bits are sufficient
   unsigned int y;
};
  FACTS ABOUT BIT-FIELDS IN C
1. Array of Bit-Fields is not allowed
struct test
{
  unsigned int x[10]: 5;
};
int main()
{
 
}
error: bit-field 'x' has invalid type
2. We cannot have pointers to bit field members as they
may not start at a byte boundary.
struct test {
   unsigned int x: 5;
  unsigned int z;
} t;
int main() { 
printf("Address of t.x is %p", &t.x);
   printf("Address of t.z is %p", &t.z);
// This works fine as z is not a bit field member.
   return 0;
}
error: attempt to take address of bit-field structure member 'test::x‘.
3. It is implementation defined to assign an
out-of-range value to a bit field member.
#include <stdio.h>
struct test
{
   unsigned int x: 2;
};
int main()
{
   struct test t;
   t.x = 5;
   printf("%d", t.x);
   return 0;
}

S-ar putea să vă placă și