Showing posts with label C Programming - Arrays. Show all posts
Showing posts with label C Programming - Arrays. Show all posts

Thursday, March 17, 2016

Pointers in C

Pointers are often used to make C programming much easier and it helps programmers improve their program's efficiency. Not only that, by using the pointer's you can achieve unlimited amount of memory usage in your program, that’s cool right.

Definitions and Uses of Pointers

Pointers are the variables that hold an address of another variable of the same data type. The cool thing about the address of a variable is that, you'll then be able to go to that address and access the data stored in it. Pointers are basically used in the C in different ways. Some of them are:
  • To create dynamic and flexible data structures.
  • To pass and handle variable arguments passed to functions (Call by reference).
  • To retrieve information stored in list like arrays.

Pointer Variables

As I said earlier, pointers indicate the memory address of any kind of object i.e. It can be address of any primitive data types or structures or even functions. The variable which holds the address is called the pointer variable.

Sunday, August 10, 2014

C Programming - Arrays

1. 
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int arr[1]={10};
    printf("%d\n", 0[arr]);
    return 0;
}
A.1B.10
C.0D.6
2. 
What will be the output of the program if the array begins at address 65486?
#include<stdio.h>

int main()
{
    int arr[] = {12, 14, 15, 23, 45};
    printf("%u, %u\n", arr, &arr);
    return 0;
}
A.65486, 65488B.65486, 65486
C.65486, 65490D.65486, 65487

3. 
What will be the output of the program ?
#include<stdio.h>

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%d\n", sizeof(arr)/sizeof(arr[0]));
    return 0;
}
A.5B.4
C.6D.7
4. 
What will be the output of the program if the array begins 1200 in memory?
#include<stdio.h>

int main()
{
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %u\n", arr, &arr[0], &arr);
    return 0;
}
A.1200, 1202, 1204B.1200, 1200, 1200
C.1200, 1204, 1208D.1200, 1202, 1200
5. 
Which of the following is correct way to define the function fun() in the below program?
#include<stdio.h>

int main()
{
    int a[3][4];
    fun(a);
    return 0;
}
A.
void fun(int p[][4])
{
}
B.
void fun(int *p[4])
{
}
C.
void fun(int *p[][4])
{
}
D.
void fun(int *p[3][4])
{
}

Saturday, August 9, 2014

C Programming - Arrays

1. 
What will be the output of the program ?
#include<stdio.h>

int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}
A.2, 3, 4, 5B.1, 2, 3, 4
C.0, 1, 2, 3D.3, 2, 1 0
2. 
What will be the output of the program ?
#include<stdio.h>
void fun(int **p);

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
    int *ptr;
    ptr = &a[0][0];
    fun(&ptr);
    return 0;
}
void fun(int **p)
{
    printf("%d\n", **p);
}
A.1B.2
C.3D.4

3. 
What will be the output of the program ?
#include<stdio.h>

int main()
{
    static int arr[] = {0, 1, 2, 3, 4};
    int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
    int **ptr=p;
    ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *++ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    ++*ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    return 0;
}
A.0, 0, 0
1, 1, 1
2, 2, 2
3, 3, 3
B.1, 1, 2
2, 2, 3
3, 3, 4
4, 4, 1
C.1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
D.0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
4. 
What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}
A.65474, 65476B.65480, 65496
C.65480, 65488D.65474, 65488
5. 
What will be the output of the program in Turb C (under DOS)?
#include<stdio.h>

int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)
        printf("%d, ", arr[i]);

    return 0;
}
A.1, 2, 3, 4, 5,B.Garbage value, 1, 2, 3, 4
C.0, 1, 2, 3, 4,D.2, 3, 4, 5, 6,

Friday, August 8, 2014

C Programming - Arrays

1. 
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
A.The element will be set to 0.
B.The compiler would report an error.
C.The program may crash if some important data gets overwritten.
D.The array size would appropriately grow.
2. 
What does the following declaration mean?
int (*ptr)[10];
A.ptr is array of pointers to 10 integers
B.ptr is a pointer to an array of 10 integers
C.ptr is an array of 10 integers
D.ptr is an pointer to array
3. 
In C, if you pass an array as an argument to a function, what actually gets passed?
A.Value of elements in array
B.First element of the array
C.Base address of the array
D.Address of the last element of array

4. 
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}
A.2, 1, 15B.1, 2, 5
C.3, 2, 15D.2, 3, 20
5. 
What will be the output of the program ?
#include<stdio.h>

int main()
{
    static int a[2][2] = {1, 2, 3, 4};
    int i, j;
    static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i), 
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return 0;
}
A.1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4
B.1, 2, 1, 2
2, 3, 2, 3
3, 4, 3, 4
4, 2, 4, 2
C.1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3
D.1, 2, 3, 4
2, 3, 4, 1
3, 4, 1, 2
4, 1, 2, 3