Posts

Showing posts from January, 2011

All 1's on one side

Program to see if all the ones in a number appear on the right side of the number and all zeros appear on the left, how can I do this most efficiently? (i.e. 00000111 is true but 100010 is false)

Malloc and segmentation fault

int main(int argc, char **argv) { char *buf; buf = malloc(1<<31); fgets(buf, 1024, stdin); printf("%s\n", buf); return 1; } What is the o/p and reason for its behaviour?

Number series

Which is next number in the series 9 15 17 21 ....

Sizeof structure in c

// (typical 32 bit machine) // char 1 byte // short int 2 bytes // int 4 bytes // double 8 bytes // structure A typedef struct structa { char c ; short int s ; } structa_t ; // structure B typedef struct structb { short int s ; char c ; int i ; } structb_t ; // structure C typedef struct structc { char c ; double d ; int s ; } structc_t ; // structure D typedef struct structd { double d ; int s ; char c ; } structd_t ; int main () { printf ( "sizeof(structa_t) = %d \n " , sizeof ( structa_t )); printf ( "sizeof(structb_t) = %d \n " , sizeof ( structb_t )); printf ( "sizeof(structc_t) = %d \n " , sizeof ( structc_t )); printf ( "sizeof(structd_t) = %d \n " , sizeof ( structd_t )); return 0 ; } What should be the o/p and expla...

unique paths

Image
A robot is located at the top-left corner of a  m  x  n  grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). How many possible unique paths are there?

Global and local name collision

Given the following source code static int a_static_var = 5 ; void foo ( void ) {     static int a_static_var = 6 ;     return ; } How your compiler will differentiate between global and local variable when they have same name.However answer to this question is compiler dependent but you can give your answer according to compiler you use.

Void pointer in c++

In c++ int main () { int i = 99 ; void * vp = & i ; * vp = 3 ; } Why there is compile time error at line 4 .

Pass by reference in c++

#include <iostream> using namespace std ; void f ( int & r ) { cout << "r = " << r << endl ; cout << "&r = " << & r << endl ; r = 5 ; cout << "r = " << r << endl ; } int main () { int x = 47 ; cout << "x = " << x << endl ; cout << "&x = " << & x << endl ; f ( x ); // Looks like pass-by-value, // is actually pass by reference cout << "x = " << x << endl ; } Suggest your opinion about this pass by reference in c++.