You have an int array with n elements and a structure with
three int members.
ie
struct No
{
unsigned int no1;
unsigned int no2;
unsigned int no3;
};
Point1.Lets say 1 byte in the array element is represented
like this - 1st 3 bits from LSB is one number, next 2 bits
are 2nd no and last 3 bits are 3rd no.

Now write a function, struct No* ExtractNos(unsigned int *,
int count)
which extracts each byte from array and converts LSByte in
the order mentioned in point1.and save it the structure
no1, no2, no3.

in the function struct No* ExtractNos(unsigned int *, int
count), first parameter points to the base address of array
and second parameter says the no of
elements in the array.

For example: if your array LSB is Hex F7 then result no1 =
7, no2 = 2, no3 = 7. In the same way convert all the
elements from the array and save the result in array of
structure.

Answer Posted / vadivelt


#include<stdio.h>
struct No* ExtractNos(unsigned int *p, unsigned int count);

struct No
{
unsigned int no1;
unsigned int no2;
unsigned int no3;
};

void main()
{
unsigned int array[20],*p, count, i;
struct No *r;
printf("ENTER THE NO OF ELEMENTS IN THE ARRAY: \n");
scanf("%d", &count);

printf("ENTER TH ELEMENTS IN THE ARRAY:\n");
for(i = 0; i<count; i++)
{
scanf("%d", &array[i]);
}

p = &array[0];
r = ExtractNos(p, count);
printf("\n");

for(i = 0; i<count; i++)
{
printf("ELEMENT IN THE STRUCTURE %d \n", (i+1));
printf("NO1: %d\nNO2: %d\nNO3: %d \n\n", r->no1, r->no2, r-
>no3);
r++;
}

getch();
}

struct No* ExtractNos(unsigned int *p, unsigned int count)
{
int i;
struct No *q;
q = (struct No*)malloc(sizeof(struct No)*count);
if(p != NULL && q != NULL && count > 0)
{
for(i = 0; i<count; i++)
{
q->no1 = (*p & 0x07);
q->no2 = (*p & 0x18) >> 3;
q->no3 = (*p & 0xE0) >> 5;
q++;
p++;
}
}
return (q - count);
}

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between far and near ?

684


What is meant by realloc()?

676


How do you print an address?

746


What is the advantage of an array over individual variables?

742


How macro execution is faster than function ?

666






Is it possible to initialize a variable at the time it was declared?

755


how can f be used for both float and double arguments in printf? Are not they different types?

608


If i have an array 0 to 99 i.e,(Size 100) I place the values 1 to 100 randomly like a[0]=29,a[1]=56 upto array[99].. the values are only between 1 to 100. getting the array values by using scanf.. If i entered one wrong element value line a[56]=108. how can i find it.. and also how to find the missing value in 1 to 100.. and i want to replace the missing values.. any one of them know please post your answer..

1591


What are volatile variables in c?

519


in iso what are the common technological language?

1634


What is data type long in c?

623


What is const keyword in c?

747


What is a const pointer in c?

670


write an algorithm to display a square matrix.

2222


what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;

2007