Answer to Question #259399 in C for Abhik

Question #259399

1. In the following program, what would be the final output? Explain in


detail.


v oid c o p y a r r ( ch a r ∗p1 , ch a r p2 [ ] ) {


memcpy ( p2 , p1 , s i z e o f ( p1 ) ) ;


memcpy ( p2 , ‘ ‘ABCD’ ’ , 4 ) ;


}


i n t main ( ) {


ch a r a r r 1 [ 1 0 0 ] ;


ch a r a r r 2 [ 1 0 0 ] ;


p r i n t f ( ‘ ‘ Enter a s t r i n g : ’ ’ ) ;


s c a n f ( ‘ ‘%[ ˆ\n ] s ’ ’ , a r r 1 ) ;


c o p y a r r ( a r r 1 , a r r 2 ) ;


p r i n t f ( ‘ ‘ \ n %s ’ ’ , a r r 2 ) ;


r e t u r n 0 ;


}


2. Consider the following program:


i n t main ( v oid ) {


i n t a = 2 , ∗b = NULL;


b = &a ;


p r i n t f ( ‘ ‘%p ’ ’ , b + 1 ) ;


p r i n t f ( ‘ ‘%p ’ ’ , ( ch a r ∗) b + 1 ) ;


p r i n t f ( ‘ ‘%p ’ ’ , ( v oid ∗) b + 1 ) ;


}



Suppose the address of a is equal to 0x1000. What is the output of the


above program? Explain in detail.


What to submit:


• Write-up describing an explanation for the output (i.e. why you observe


what you observe), for both the cases.


1
Expert's answer
2021-11-02T17:36:26-0400

Copying an array involves index-by-index copying. The length of the array is known and is used in iteration. Both arrays are of the same data types and sizes. The output is the content of array2 which is a copy of the first array arr1.

#include<stdio.h>
#include<string.h>
void copyarr(char *p1 , char p2[] ) {
memcpy ( p2, p1, sizeof( p1));
memcpy ( p2, "ABCD", 4) ;
}
int main ( ) {
char arr1 [100];
char arr2 [100];
printf("Enter a string :" );
scanf ("[\n] %s" , arr1 );
copyarr( arr1 , arr2 );
printf( "\n %s" , arr2 );
return 0;
int a = 2, *b = NULL;
b = &a ;
printf ("%p" , b + 1 );
printf( "%p" , ( char *) b + 1 );
printf("%p", ( void *) b + 1 );
}
 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS