Embedded C Interview Questions Part -3

1) What is a Constant Pointer and a Pointer to Constant in Embedded C?

A constant pointer is a pointer that cannot change the address it is holding. Once a constant pointer points to a variable then it cannot point to any other variable.

#include "stdio.h"

void main(){
    
    // Constant pointer

    int a=10,b=20;
    int *const ptr=&a;  // Constant Pointer
    ptr=&b;             // This statement will give a error as 'ptr' is read- only and cannot be changed
    
}

A pointer to a constant is one where we cannot change the value of variable it points to. These pointers can change the address they point to but cannot change the value kept at the address.

#include "stdio.h"

void main(){
    
    //Pointer to constant

    int a=10;
    const int *ptr = &a;  //Pointer to constant
    *ptr=11;              // This statement will give error as it is a pointer to constant
}

Constant pointer to constant is combination of above two, it neither changes address it is pointing to nor it can change value at that address.

#include "stdio.h"

void main(){
    // Syntax for Constant Pointer to constant
    
    int a =10;
    int b =20;
    const int *const ptr = &a;      // This statement will give error as it is a pointer to constant
    
    ptr = &b;                       // This statement will give a error as 'ptr' is read- only and cannot be changed       
    *ptr = 11;                      // This statement will give error as it is a pointer to constant
}

2) What is a Dangling Pointer in Embedded C?

Pointer pointing to a memory location that is freed or deleted is called dangling pointer. Dangling pointer can be avoided by assigning it to NULL. Below are some ways when a pointer becomes dangling-

#include "stdio.h"

void main(){
    
    int *ptr = (int*) malloc(sizeof(int));
    free(ptr);       // After this statement the pointer becomes dangling
    ptr = NULL;      // No more dangling
}

3) What is a Void Pointer in Embedded C?

Void pointer is a pointer that points to some data location in storage, which doesn’t have a specific type.

#include "stdio.h"

void main(){
    
    int x=4;

    float y=5.5;

    void *ptr;

    ptr=&x;

    printf("%d",(*(int*)ptr));       //Pointer is now integer

    ptr=&y;

    printf("\n%f",(*(float*)ptr));   // Pointer is now float
}

4) What is a Null Pointer in Embedded C?

Null Pointer is a pointer which is pointing to nothing. In case, we don’t have a address to be assigned to a pointer, we can simply use NULL.

#include "stdio.h"

void main(){
    
    int *ptr = NULL;    //Pointer assigned to NULL is a null pointer
}

5) What is a Wild Pointer in Embedded C?

A pointer that has not been initialized to anything, not even NULL.

#include "stdio.h"

void main(){
    
    int *p;       //Wild Pointer

    int x= 10;

    p=&x;         //Not a Wild Pointer
}

6) What are different Pointer increment Concepts in Embedded C?     

Some concepts like [ *PTR++ , *++PTR , ++*PTR , (*PTR)++] are defined below-

        _____________
ptr--> |  25  |  38  |
       |______|______|
         2000   2004

1)x=*ptr++                  --> x= *ptr;           
  x=25;                         ptr = ptr+1;
  ptr = 2004;
  *ptr=38;

2)x=*++ptr                  --> ptr = ptr+1;
  x=38;                         x = * ptr;
  ptr= 2004;
  *ptr=38;

3)x=++*ptr                  --> *ptr=*ptr+1;
  x=26;                         x=*ptr;
  ptr=2000;
  *ptr=26;

4)x=(*ptr)++                --> x=*ptr;
  x=25;                         *ptr=*ptr+1;
  ptr=2000;
  *ptr=26;
           

7) What is Pragma Directive in Embedded C?

The Pragma Directives are special purpose directives used to turn on or off certain features.

Example

#pragma startup – This specifies functions needed to execute before startup i.e before main executes.

#pragma exit – This specifies functions needed to execute just before program exit.

Let us look at example below to understand the concept-

#include <stdio.h>
#include <stdlib.h>

void func1();
void func2();

#pragma startup func1            // Startup Pragma Directive
#pragma exit func2               // Exit Pragma Directive

static int x=100;

/* The program output sequence will be 
300
200
400
*/
/* The program will bot work with gcc compiler */

int main()
{  
    x= 200;
    printf("%d",x);              
    func1();
    func2();
    return 0;                                    
}

void func1()
{
    x= 300;
    printf("%d",x);
}

void func2()
{
    x= 400;
    printf("%d",x);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like