CH- 8 POINTERS IN C

1.How is a pointer variable different from a normal variable?
A normal variable stores a value (such as an int, a double, a char),whereas a pointer stores a memory address. It is a special variable that stores the address of another variable.

2.Why is dynamic memory allocation an efficient memory management technique?
In dynamic memory allocation technique, when the user runs the program he or she can give the input based on the requirement. Accordingly that much amount of memory will be allocated. There is no wastage of memory. Thus this technique becomes an efficient technique.

3.How many bytes are needed to store an int pointer variable? is it the same for a char pointer variable? write a simple c program to explain your answer.

Ans: For storing an int pointer variable we need 4 bytes of memory allocation.

No, a char pointer variable needs 1 byte memory allocation.

#include <stdio.h>
int main()
{
int var = 5;
int *v;
v=&var;
char x = 'A';
char *ch;
ch=&x;
printf("value: %d\n", *v);
printf("Size of int pointer = %d bytes.\n", sizeof(*v));
printf("value: %c\n", *ch);
printf("Size of char pointer = %d bytes.\n", sizeof(*ch));

return 0;
}

4.Write the output of the following code segment:

a)

#include<stdio.h>
int main(){
 int *ptr , x = 9;
 ptr = &x;
printf("\n %d",(*ptr)++);
return 0;
}

Output: 9

b)

#include<stdio.h>
int main(){
 int *ptr , x = 9;
 ptr = &x;
printf("\n %d",(*ptr)++);
printf("\n %d",*ptr);

return 0;
}

Output:

9
10

c)

#include<stdio.h>
int main(){
 int *ptr , x = 9;
 ptr = &x;
int y=++(*ptr);
printf("\n %d",y);

return 0;
}

Output:

10

d)

#include<stdio.h>
int main(){
 char *ptr , x = 'A';
 ptr = &x;
char y=*ptr;
printf("\n %c",y);

return 0;
}

Output:

A

e)

#include<stdio.h>
int main(){
 char *ptr , x = 'A';
 ptr = &x;
char y=(*ptr)++;
printf("\n %c",y);

return 0;
}

Output:

A

f)

#include<stdio.h>
int main(){
 char *ptr , x = 'A';
 ptr = &x;
char y=++(*ptr);
printf("\n %c",y);

return 0;
}

Output:

B

g)

#include<stdio.h>
int main(){
 char *ptr , x = 'A';
 ptr = &x;
char *y;
y=ptr;
printf("\n %c",++(*y));

return 0;
}

Output:

B

5. Write a c program to dynamically allocate memory for an array to store 10 integers and display the first five out of them.

#include<stdio.h>
#include<stdliib.h>
int main() {
  int n;
  int *data;
data = (int *)calloc(10, sizeof(int));
  if (data == NULL) {
  printf("Error!!! memory not allocated.");
  exit(0);
  }
 for (int i = 0; i < 10; ++i) {
  printf("Enter number%d: ", i + 1);
  scanf("%d", data + i);
  }
 printf("First five numbers are =\n");
for (int i = 0; i < 5; ++i) {
 printf("%d\n", *(data+i));
}
return 0;
}

6.Write a c program to dynamically allocate memories for an array to store runs scored by Virat Kohli in the last 10 ODI cricket matches. Write a function to find the maximum one.


#include <stdio.h>
#include <stdlib.h>
int max();
int main() {
int maxrun=max();
printf("Highest run = %d", maxrun);
return 0;
}
int max()
{
int n;
int *data;
data = (int *)calloc(10, sizeof(int));
if (data == NULL) {
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < 10; ++i) {
printf("Enter score%d: ", i + 1);
scanf("%d", data + i);
}
for (int i = 0; i < 10; i++) {
if (*data < *(data + i)) {
*data = *(data + i);
}
}
return *data;
}

7.Write a C program and define a function that takes the length of your name as an input parameter and then allocates memory dynamically to store your name. Write another function to display the name.

 


#include<stdio.h>
#include<stdlib.h>
char *name;
int main()
{
    int num=0;
  printf("\n Enter Length of your name:");
  scanf("%d",&num);
  inputname(num);
display();
return 0;
}
int inputname(int num)
{
    name = (char *) malloc(num*sizeof(char));
   if ( name != NULL ) {
      printf("\nEnter your name: ");
      scanf("%s", name);
      }
      return 0;
}
int display(){
while(*name!='\0')
    printf("%c",*name++);
    return 0;
}

8. Write a c program to store some integer variables in an array. Then write functions to the following:-

               1.To calculate the number of even numbers in the array.

               2.To dynamically allocate memory to a new array to store only the even numbers.

              3. To copy the even numbers from the first array to the second one.


#include <stdio.h>
#include <stdlib.h>
int max();
int even = 0;
int main() {
max();
return 0;
}
int max() {
int n,j;
int *data;
int *newarray;
printf("Number of elements in first array?");
scanf("%d",&n);
data = (int *)calloc(n, sizeof(int));
if (data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < n; ++i)
{
printf("Enter score%d: ", i + 1);
scanf("%d", data + i);
}
for (int i = 0; i < n; i++)
{
if (*(data + i) %2==0)
{
even = even +1;
}
}
printf("Total even numbers are = %d", even);
newarray = (int *)calloc(even, sizeof(int));
if (newarray == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < n; i++)
{
if (*(data + i) %2==0)
{
*(newarray+j)=*(data + i);
j++;
}
}
printf("\nNew array elements are:\n");
for (int i = 0; i < even; i++)
{
printf(" %d",*(newarray+i));
}
return 0;
}

9.Write a c program to store some integer variables in an array. Then write functions to the following:-

1.To calculate the number of non zero elements that are divisible by 3.

2.To dynamically allocate memory to a new array to store only those elements.

3.To copy the selected elements from the first array to the second one.

4.To calculate the summation of these elements.

#include<stdio.h>
#include<stdlib.h>
int max();
int even = 0;
int main() {
 max();
  return 0;
   }
   int max() {
   int n,j;
   int *data;
   int sum=0;
      int *newarray;

        printf("Number of elements in first array?");
               scanf("%d",&n);


    data = (int *)calloc(n, sizeof(int));
    if (data == NULL)
     {
     printf("Error!!! memory not allocated.");
    exit(0);
     }
     for (int i = 0; i < n; ++i)
        {
      printf("Enter score%d: ", i + 1);
       scanf("%d", data + i);
     }
       for (int i = 0; i < n; i++)
        {
        if (*(data + i) %3==0 && *(data + i)!=0 )
        {
             even = even +1;
        }
         }
           printf("Total non zero elements that are divisible by 3  = %d", even);
      newarray = (int *)calloc(even, sizeof(int));
    if (newarray == NULL)
     {
     printf("Error!!! memory not allocated.");
    exit(0);
     }
for (int i = 0; i < n; i++)
        {
        if (*(data + i) %3==0 && *(data + i)!=0 )
        {
             *(newarray+j)=*(data + i);
             j++;
             }
         }
printf("\nNew array elements are:\n");

     for (int i = 0; i < even; i++)
{
      printf("\n%d",*(newarray+i));

}   
 for (int i = 0; i < even; i++)
{
    sum=sum + *(newarray+i);

}
      printf("\n\nSummation of these elements are %d",sum);

return 0;
         }

                                        ===END===

LEAVE A REPLY

Please enter your comment!
Please enter your name here