Here what we are trying to learn is the difference between:-

    const char *;
    

    and

    char * const;
    

    The difference is that const char * is a pointer to a const char, while char * const is a constant pointer to a char.

    const char * :- In this, the value being pointed to can’t be changed but the pointer can be.
    charĀ  * const :- In this, the value being pointed at can change but the pointer can’t.

    The third type is

    const char * const;
    

    which is a constant pointer to a constant char (so nothing about it can be changed).
    One rule of the thumb to remember this is by reading backwards:-

    • char * const :- constant pointer to a character
    • const char * :- pointer to a constant character
    • const char * const:- constant pointer to a constant character

    It will be more clear by this following example:-

    #include<stdio.h>
    
    int main()
    {
        const char * p; // value cannot be changed
        char z;
    
    	//*p = 'c'; // this will not work
    
    	p = &z;
        printf(" %c\n",*p);
        return 0;
    }
    
    int main()
    {
        char * const p; // address cannot be changed
        char z;
        *p = 'c';
    
    	//p = &z;   // this will not work
    
    	printf(" %c\n",*p);
        return 0;
    }
    
    int main()
    {
        const char * const p; // both address and value cannot be changed
        char z;
    
    	*p = 'c'; // this will not work
        p = &z; // this will not work
    
    	printf(" %c\n",*p);
        return 0;
    }
    
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    How will you reverse a queue?

    by nikoo28
    4 minutes read

    Question: Give an algorithm to reverse a queue. You can only use generic functions of the Queue ADT. Input: 4, 8, 15, 16, 23, 42 Output: 42,23,16,15,8,4 To solve this question will take the help of an auxiliary stack. The steps involved will be:- Create an auxiliary stack S. Until the queue Q is not empty, push the elements of the queue, in the stack S. Now we have a stack in which the last element of the Queue is at the TOP. Until the stack is empty POP(S) and …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Implementing a Queue

    by nikoo28
    4 minutes read

    Before implementing a queue, we must know the basic operations that should be available for a queue. Main Queue Operations:- enQueue(int data) :- Inserts an element at the end of the queue. int deQueue():- Removes and returns the element at the front of the queue. Auxiliary Queue Operations:- int front():- Returns the first element at the queue, without removing it. int queueSize():- Returns the size of the queue. int isEmptyQueue():- Indicates whether no elements are present. We can implement a queue using an ADT (Abstract Data Type). To implement a …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    What is a Queue?

    by nikoo28
    3 minutes read

    A queue is a linear data structure that stores data (similar to Linked Lists and Stacks). In a queue, the order in which the data arrives is important. Definition: A queue is an ordered list in which insertions are done at one end (rear) and deletions are done at other end (front). The first element that you insert, is the first one that is removed. This makes it a First In First Out (FIFO) or Last In Last Out (LILO) list. Basic Operations: The basic operations on this data structure …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Finding spans in an array.

    by nikoo28
    2 minutes read

    Question: Find spans in an array. Given an array arr[], the SPAN s[i] of arr[i] is the maximum number of consecutive elements arr[j] immediately before arr[i] such that arr[j] <= arr[i]. Let us try to understand the question once again. This is a very common problem in stock markets to find the peaks. Spans have applications to financial analysis(Example:- You might have heard a saying “STOCKS AT 52 WEEK HIGH”). The span of a stocks price on certain day, i , is the maximum number of consecutive days (up-to the …

    0 FacebookTwitterLinkedinWhatsappEmail
  • This is one of the very basic pointer usage in C and to understand it let us start with a very simple example. Suppose we have a program like:- int main(void) { char *p = "Hello"; while(*p++) printf("%c", *p); return 0; } In this case the first statement: char *p = "Hello"; declares p as a pointer to char. When we say “pointer to a char“, what does that mean? It means that the value of p is the address of a char; p tells us where in memory there …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: How do we implement 2 stacks using only one array? Our stack routines should not indicate an exception unless every slot in the array is used? SOLUTION: Algorithm: Start with two indexes, one at the left end and other at the right end The left index simulates the first stack and the right index simulates the second stack. If we want to push an element into the first stack then put the element at left index. Similarly, if we want to push an element into the second stack then …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More