Pointer indirection with care

* and . operator struct rec { int i; float f; char c; }; int main() { struct rec \*p; p=(struct rec \*) malloc (sizeof(struct rec)); (\*p).i=10; } To initialise pointer to 10, we have to use parentheses, because . operator has higher precedence than * operator. ++ and * Consider **\*p++** The postfix “++” operator has higher precedence than prefix “*” operator. Thus, *p++ is same as *(p++); it increments the pointer p, and returns the value which p pointed to before p was incremented. [Read More]

Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A?

Here is a code in c: #include <stdio .h> #include <conio .h> int isSubset(char \*a, char \*b); int main(){ char str1\[\]="defabc"; char str2\[\]="abcfed"; if(isSubset(str1, str2)==0) printf("\\nYes, characters in B=\[%s\] are a subset of characters in A=\[%s\]\\n",str2,str1); } else { printf("\\nNo, characters in B=\[%s\] are not a subset of characters in A=\[%s\]\\n",str2,str1); } getch(); return(0); } // Function to check if characters in "b" are a subset // of the characters in "a" int isSubset(char \*a, char \*b) { int letterPresent\[256\]; int i; for(i=0; i<256; i++) letterPresent\[i\]=0; for(i=0; a\[i\]! [Read More]

What's the difference between const char *p, char * const p and const char * const p?

`const char *p - This is a pointer to a constant char. One cannot change the value pointed at by p, but can change the pointer p itself.  *p = ‘A’ is illegal. p = “Hello” is legal.  Note that even char const *p is the same! const * char p - This is a constant pointer to (non-const) char. One cannot change the pointer p, but can change the value pointed at by p. [Read More]

Syntax in C

qualifier:
            volatile
            const

storage-class: 

            auto            extern
            static          register

type:
            void            char            short
            int             long            float
            double          signed          unsigned
            enum-specifier
            typedef-name
            struct-or-union-specifier

How to add two numbers without using the plus operator?

Actually, SUM = A XOR B CARRY = A AND B Recursive: int add(int a, int b){ if (!a) return b; else return add((a & b) « 1, a ^ b); } Iterative **unsigned long add(unsigned long integer1, unsigned long integer2) { unsigned long xor, and, temp;  and = integer1 & integer2; /* Obtain the carry bits */ xor = integer1 ^ integer2; /* resulting bits */  while(and ! [Read More]

What are the common causes of pointer bugs?

Uninitialized pointers** :** One of the easiest ways to create a pointer bug is to try to reference the value of a pointer even though the pointer is uninitialized and does not yet point to a valid address. For example: int *p; *p = 12; The pointer p is uninitialized and points to a random location in memory when you declare it. It could be pointing into the system stack, or the global variables, or into the program’s code space, or into the operating system. [Read More]

What is the difference between malloc() and calloc()?

#include void *calloc(size_t n, size_t size); void *malloc(size_t size); The two functions malloc() and calloc() are functionally same in that they both allocate memory from a storage pool (generally called heap). Actually, the right thing to say is that these two functions are memory managers and not memory allocators. Memory allocation is done by OS specific routines (like brk() and sbrk()). Here are some differences between these two functions.. [Read More]

Convert the decimal number to any base (binary, hex, oct etc...)

#include 

int main()
{
  decimal_to_anybase(10, 2);
  decimal_to_anybase(255, 16);
  getch();
}

decimal_to_anybase(int n, int base)
{
  int i, m, digits[1000], flag;
  i=0;

  printf("\n\n[%d] converted to base [%d] : “, n, base);

  while(n)
  {
     m=n%base;
     digits[i]="0123456789abcdefghijklmnopqrstuvwxyz”[m];
     n=n/base;
     i++;
   }

   //Eliminate any leading zeroes
   for(i–;i>=0;i–)
   {
     if(!flag && digits[i]!='0’)flag=1;
     if(flag)printf("%c”,digits[i]);
   } 
}

C code to dynamically allocate one, two and three dimensional arrays (using malloc())

One Dimensional Array int \*myarray = malloc(no\_of\_elements \* sizeof(int)); //Access elements as myarray\[i\] 2 Dimensional Array Method1 int \*\*myarray = (int \*\*)malloc(nrows \* sizeof(int \*)); for(i = 0; i < nrows; i++) { myarray\[i\] = malloc(ncolumns \* sizeof(int)); //allocating 1 D array = number of elements in column } // Access elements as myarray\[i\]\[j\] Method 2 (Contagious Allocation) int \*\*myarray = (int \*\*)malloc(nrows \* sizeof(int \*)); myarray\[0\] = malloc(nrows \* ncolumns \* sizeof(int)); for(i = 1; i < no\_of\_rows; i++) myarray\[i\] = myarray\[0\] + (i \* ncolumns); // Access elements as myarray\[i\]\[j\] In either case, the elements of the dynamic array can be accessed with normal-looking array subscripts: array[i][j]. [Read More]