1. Printf();函数中的参数是按从右到左顺序运算的
如:printf("%d %d\n", *ptr, *(++ptr)); 是先进行++ptr,再运行*ptr。
2. 为了便于结构体Struct内元素的访问和管理,当结构体内的元素的长度都小于处理器的位数时,便以结构体里最长的数据元素为对齐单位,也就是说,结构体的长度一定是最长的数据元素的整数倍。如果结构体内存长度大于处理器位数的元素,那么就以处理器的位数为对其单位。但是结构体内类型相同的连续元素将在连续的空间内。
数据对齐(data alignment):是指数据所在的内存位置(内存地址)必须是该数据长度的整数倍。
3. 静态变量是存放在全局数据区中的,而sizeof计算的是栈中分配的大小,所以如果一个类中有static类型的变量,sizeof某个类时,该static变量是不计算在内的。
4. Class的sizeof大小的计算方式跟struct是一样的,也要考虑最大对齐单位。没有任何virtual元素(函数)的空类sizeof为1,单一继承、多继承空类的空类sizeof也为1,虚继承(Class B:public virtual A)的空类的sizeof为4。类中含有virtual函数(不管多少个virtual函数),该类的sizeof大小为该类去掉virtual的 sizeof大小再+4。
比如:
class A{
};
sizeof(A)为1。
class A{
int a;
public:
A(){}
~A(){}
virtual void f(){}
virtual int g(){}
};
sizeof(A)为8。
class A{
public:
A(){}
~A(){}
virtual void f(){}
virtual int g(){}
};
sizeof(A)为4。
class A{
public:
A(){}
~A(){}
void f(){}
int g(){}
};
sizeof(A)为1。
5. sizeof可以计算函数的大小,只是根据函数的返回类型的大小来计算的。比如 int f(int a,char b),sizeof(f(a,b))结果为4。总之,对函数使用sizeof,在编译阶段会被函数返回值的类型取代。
6. sizeof()括号中的代码不会被编译的,所说int a=6; sizeof(a++); 后a的值仍然是6。
7. 只要是指针,sizeof返回的大小都是4。
8. sizeof求数组的大小返回的是各维数的乘机*数组元素的大小。
9. sizeof(string)为4.
10. int test(char var[])函数中,sizeof(var)为4,因为var[]等价于*var,已经退化为一个指针了。
11. Class B{
flaot f; char p; int adf[3];
}; 则cout<
12.内联函数(inline)被不包含for, while, switch语句
13. (1)void (*f) ( ) 函数指针;(2)void *f()函数返回指针;(3)const int * const指针;(4)int * const 指向const的指针;(5)const int * const 指向const的const指针。
14. int a[]={1,2,3,4,5};
int * ptr=(int *)(&a+1);
printf("%d,%d",*(a+1), *(ptr-1));
输出结果为2,5。因为&a+1表示a+6,即ptr指向第六个位置。
牢记一点:数组名本身就是指针,再加个&,就变成了双指针,这里的双指针就是指二维指针,加1,就是数组整体加一行。
15. int *p; *p=3; //错误,因为p只是一个指针,*p并不存在。
16. 拷贝构造函数和拷贝复制函数的区别
17. 32位机器上,int和long 都是占4个字节;64位机器上,int和long分别占4、8个字节
18. int x=4; x+=x-=x-x--; 最后x为7.而在Java中相同的代码后x为8.
//test1
#include
int main()
{
int i=3;
int j;
void *k; k++; //compile error, no type
j=sizeof(++i + ++i); //type is int, so is 4B, not execute in sizeof()
printf("i= %d j= %d \n", i, j); //3 and 4
}
----------------------------------------------------------------------------
---------
//test2
#include
int main()
{
char *p = malloc(20); //sizeof(p)=4B/8B, sizeof(*p) = 1B
int a[5]={10, 20, 30, 40, 50}; /a and &a[0] is same pointstobase.&a+1-->whole array int b[5]={100, 200, 300, 400, 500}; //sizeof(b)-->4*5=20B
int *ptr = (int *)(&a+1); sizeof(a) is 5 elements, and p is an int pointer 4B
int *t = (int *)(&a -1);
printf("%d %d %d \n", *(a+1), *(ptr-1), *(t+1)); //20, 50, 200
//push to stack: 4G--50,40,30,20,10, 500,400,300,200,100,----0G
}
---------------------------------------------------------------------
//test3
#include
int main()
{
char *p;
char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf+1)[5]; //buf+1-->2
printf("%d \n", p); //9
}
--------------------------------------------------------------------------
//test4
#include
int main()
{
int a[][3]={1,2,3,4,5,6}; // 123 456
int (*ptr)[3] = a; //int **x=a; compile wrong type
printf("%d %d \n", (*ptr)[1], (*ptr)[2]); //row0: col1 and 2: 2 3
++ptr;
printf("%d %d \n", (*ptr)[1], (*ptr)[2]); // row1: col1 2: 5 6
}
-----------------------------------------------------------------------
//test5
#include
void f(char **p);
int main()
{
char *argv[]={"ab", "cd", "ef", "gh"}; //argv[0]=ab, [1]=cd...
f(argv);
}
void f(char **p)
{
char *t;
t=(p+=sizeof(int))[-1]; p+=sizeof(int) -->back 1 [-1]-->gh
printf("%s\n", t); //gh
}
---------------------------------------------------------------------
//test6
#include
void foo(int b[][3]);
int main()
{
int a[3][3]={{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
foo(a); // must be same col
printf("%d\n", a[2][1]); // 8-->0
}
void foo(int b[][3])
{
++b; //b points 1,2,3 now points to 4 5 6 after ++b, b points 4 5 6
b[1][1]=0; // 8-->0
}
./test1
i= 3 j= 4
./test2
20 50 200
./test3
9
./test4
2 3
5 6
./test5
gh
./test6
0
/*
Method #1 (No tricks, just an array with empty first dimension)
===============================================================
You don't have to specify the first dimension!
*/
int func1(short mat[][3])
{
register short i, j;
printf(" Declare as matrix, explicitly specify second dimension: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", mat[i][j]);
}
}
printf("\n");
return;
}
/*
Method #2 (pointer to array, second dimension is explicitly specified)
======================================================================
*/
int func2(short (*mat)[3])
{
register short i, j;
printf(" Declare as pointer to column, explicitly specify 2nd dim: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", mat[i][j]);
}
}
printf("\n");
return;
}
/*
Method #3 (Using a single pointer, the array is "flattened")
============================================================
With this method you can create general-purpose routines.
The dimensions doesn't appear in any declaration, so you
can add them to the formal argument list.
The manual array indexing will probably slow down execution.
*/
int func3(short *mat)
{
register short i, j;
printf(" Declare as single-pointer, manual offset computation: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", *(mat + 3*i + j));
}
}
printf("\n");
return;
}
/*
Method #4 (double pointer, using an auxiliary array of pointers)
================================================================
With this method you can create general-purpose routines,
if you allocate "index" at run-time.
Add the dimensions to the formal argument list.
*/
int func4(short **mat)
{
short i, j, *index[3];
for (i = 0 ; i < 3 ; i++)
index[i] = (short *)mat + 3*i;
printf(" Declare as double-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", index[i][j]);
}
}
printf("\n");
return;
}
/*
Method #5 (single pointer, using an auxiliary array of pointers)
================================================================
*/
int func5(short *mat[3])
{
short i, j, *index[3];
for (i = 0 ; i < 3 ; i++)
index[i] = (short *)mat + 3*i;
printf(" Declare as single-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", index[i][j]);
}
}
printf("\n");
return;
}
Return to contents page
http://zhidao.com from baidu->c
http://www.allinterview.com/showanswers/99704.html
(2)
#ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); } 1
to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD Synergy 6
main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); } 1 // 1 4 8
#include
Is the following code legal? struct a { int x; struct a b; } 1
main() { char *p = “ayqm”; char c; c = ++*p++; printf(“%c”,c); } 1
Write a C function to search a number in the given list of numbers. donot use printf and scanf Honeywell 4
main() { int i=10; i=!i>14; Printf ("i=%d",i); } 1
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); } 1
main() { unsigned int i=10; while(i-->=0) printf("%u ",i); } 1
main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); } 1
Link list in reverse order. NetApp 7
main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); } 1
main() { 41printf("%p",main); }8 1
How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com Deshaw 9
struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); } 1
program to Reverse a linked list Ness-Technologies 4
#include
main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); } 1
void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }
main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); } 1
main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); } 1
Finding a number multiplication of 8 with out using arithmetic operator NetApp 8
main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); } 1
int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } 1
main() { int i, j; scanf("%d %d"+scanf("%d %d", &i, &j)); printf("%d %d", i, j); } a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered by the user HCL 1
void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); } 1
main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); } 1
main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); } 1
What are segment and offset addresses? Infosys 1
main() { extern int i; i=20; printf("%d",i); } 1
main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); } 1
Is the following statement a declaration/definition. Find what does it mean? int (*x)[10]; 1
Write a C function to search a number in the given list of numbers. donot use printf and scanf Honeywell 4
void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); } 1
main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); } 1
Sorting entire link list using selection sort and insertion sort and calculating their time complexity NetApp 1
main() { if (!(1&&0)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above HCL 1
main() { int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); } 1
program to find magic aquare using array
how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..! GATE 0 5 [Send This Question to Your Friend]
How to count a sum, when the numbers are read from stdin and stored into a structure? 0 28 [Send This Question to Your Friend]
How we print the table of 3 using for loop in c programing? 3 146 [Send This Question to Your Friend]
How we print the table of 2 using for loop in c programing? 1 45 [Send This Question to Your Friend]
main() { char a[4]="HELL"; printf("%s",a); } Wipro 1 157 [Send This Question to Your Friend] // HELLsdfsd->garbage till found \0
char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); } 1 68 [Send This Question to Your Friend]
//return stack memory-->wrong->garbage
char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); } 1 64 [Send This Question to Your Friend]
// return text or data part: ok
Printf can be implemented by using __________ list. 1 85 [Send This Question to Your Friend]
va_list: (fmt,...)
main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } int i; 0 50 [Send This Question to Your Friend]
main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); } 1 56 [Send This Question to Your Friend]
//compile err. void type
main() { char a[4]="HELLO"; printf("%s",a); } 1 61 [Send This Question to Your Friend]
// compile err.
Is this code legal? int *ptr; ptr = (int *) 0x400; 1 56 [Send This Question to Your Friend]
//legal
void main() { char ch; for(ch=0;ch<=127;ch++) printf(“%c %d \n“, ch, ch); } 1 53 [Send This Question to Your Friend]
//if char is unsigned-ok, if signed then always <=127-->loop forever
void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(“%d”,k); } 1 53 [Send This Question to Your Friend]
//compile err. compilier see /*jp like a comment
Which version do you prefer of the following two, 1) printf(“%s”,str); // or the more curt one 2) printf(str);
// first one.
Design an implement of the inputs functions for event mode 300
Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ? 433 Honeywell
Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange 338
main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } int i; 50
Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector 508
Develop a routine to reflect an object about an arbitrarily selected plane 302
main() { int (*functable[2])(char *format, ...) ={printf, scanf}; int i = 100; (*functable[0])("%d", i); (*functable[1])("%d", i); (*functable[1])("%d", i); (*functable[0])("%d", &i); } a. 100, Runtime error. b. 100, Random number, Random number, Random number. c. Compile error d. 100, Random number 7 HCL
Implement a t9 mobile dictionary. (Give code with explanation ) 513 Yahoo
create a C-code that will display the total fare of a passenger of a taxi if the driver press enter,the timer will stop. Every 10 counts is 2 pesos. Initial value is 25.00 120 Microsoft
find simple interest & compund interest 45
Write a routine to implement the polymarker function 407
How to count a sum, when the numbers are read from stdin and stored into a structure? 28
Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines 319 IBM
can u give me the c codings for converting a string into the hexa decimal form...... 202
why nlogn is the lower limit of any sort algorithm? 22
Write a program to model an exploding firecracker in the xy plane using a particle system 391 HCL
write a program for area of circumference of shapes 29
how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!
No comments:
Post a Comment