Está en la página 1de 5

How to pass a multi-dimensional array to a function?

Posted on January 3, 2011 by anukampa When we pass an array to a function, what really gets passed is a pointer to the arrays base address. We know that when we declare a function that seems to accept an array as a parameter, the compiler quietly compiles the function as if that parameter were a pointer, since a pointer is what it will actually receive. What about multidimensional arrays? What kind of pointer is passed down to the function? When we pass a multi-dimensional array, since the first element of a multidimensional array is another array, what gets passed to the function is a pointer to an array. If you want to declare the function func in a way that explicitly shows the type which it receives, the declaration would be Source code
func(int (*a)[7]) { ... }

The declaration int (*a)[7] says that a is a pointer to an array of 7 ints. Lets take an example of adding two matrix.
#include<stdio.h> #define R 10 #define C 10 void addmat(int (*x)[],int (*y)[],int (*z)[],int,int); void readmat(int (*a)[],int,int); void showmat(int (*a)[],int,int); main() { int m1[R][C],m2[R][C],m3[R][C],r,c,i,j; printf("\nEnter no. of rows and columns you want to use"); scanf("%d %d",&r,&c); printf("\nEnter data for matrix1"); readmat(m1,r,c); printf("\nEnter data for matrix2"); readmat(m2,r,c); addmat(m1,m2,m3,r,c); printf("\nMatrix - 1"); showmat(m1,r,c); printf("\nMatrix - 2"); showmat(m2,r,c); printf("\nMatrix - 3 = Matrix -1 + Matrix -2"); showmat(m3,r,c); return 0; } void readmat(int (*a)[C],int r,int c) { int i,j; for(i=0;i<r;i++)

} } void showmat(int (*a)[C],int r,int c) { int i,j; for(i=0;i<r;i++) { printf("\n"); for(j=0;j<c;j++) { printf("%d\t",*(*(a+i)+j)); } } } void addmat(int (*x)[C],int (*y)[C],int (*z)[C],int r,int c) { int i,j; printf("\nAdding ..."); for(i=0;i<r;i++) { for(j=0;j<c;j++) { *(*(z+i)+j)=*(*(x+i)+j)+*(*(y+i)+j); } } }

for(j=0;j<c;j++) { scanf("%d",*(a+i)+j); }

How to Prepare for a Technical Interview


Posted on November 8, 2010 by admin

It is the thought of having to answer technical questions makes most job seekers really anxious in a campus interview. A technical interview for Engineering/MCA students not only requires more preparation; it is more in depth in that the interviewee must demonstrate his ability to solve problems. Within a time span of 20 40 minutes one needs to prove his/her technical excellence. The key to doing well in a technical interview is being able to confidently demonstrate or showcase your knowledge. The following are certain points one should keep in mind while preparing.
1. Make a list of the technical skills that you are confident with as those subjects you need to give maximum emphasis on. And do not mention any subject you are not confident about. 2. The projects that you have undertaken have to be prepared well. Review all the technology you have used in the project and prepare the documentation which you can produce during the interview if allowed.

3. Search for the companys website and learn as much as you can about the company with which you are interviewing. Make a note of the recent projects they have undertaken and which are the various technologies they are using etc. 4. Gather your study materials and take a few days to study them. Instead of attempting to memorize all of the information in your technical books and notes, study the subjects you are good in and give a overlook on others. Give yourself some time to look over the study materials as this will help you remember the information. 5. Your success in a technical interview depends more on your ability to solve problems than it does on how much information you can recite from memory. Reviewing case studies, word problems and brainteasers is a good way to develop your problem-solving skills. 6. Be prepared to deliver. Technical interviews often involve using a whiteboard or blank piece of paper to solve problems and demonstrate skills. An interviewer will ask you to do this in order to test your communication skills and determine what your thought process is when you solve problems. Your studies should include solving complex problems on paper. You should practice explaining the solution to the problems as you write them down 7. Practice answering interview questions. Now that you have studied and feel ready to answer any technical questions that the interviewer might ask you, you should focus on being confident during your interview. Maintaining good eye contact, sitting up straight and expressing your enthusiasm throughout the interview are just as important as demonstrating your skills. The best way to practice for your interview is to answer the questions out loud or in front of someone. Observe your body language and continue practicing until you feel comfortable and relaxed. 8. Review your resume. The best way to prove your technical skills is by reviewing the different ways in which you have used your skills in project works. Interviewers will often ask you about the different ways youve used your skills to solve problems and develop new procedures. You must respond to these questions by describing different work situations you have encountered and how you have accomplished difficult tasks or finished projects. 9. Find something comfortable and professional to wear to your interview. While the focus of your technical interview is to determine whether or not you have the technical skills to do the job, your appearance is just as important. As the saying goes First impression lasts long you only get one opportunity to make a positive first impression, and what you wear to the interview reveals a lot about your character. In researching the employer, you may have noticed that casual attire is allowed, but this dress code does not apply to you. Wear a formal wear that is comfortable and fits well.

Memory Layout of a C Program


Posted on December 7, 2010 by anukampa

There are four segments in C Program. Data, Code, Stack and Heap Segments. All this sections define the scope & storage class of the data and flow of control between the instructions.

Data Segments: The data segment is used to hold the value of those variables that need to be available throughout the lifetime of the program, so global & static variables (which are local but are required between function calls) are stored here. The data segment is divided into two parts: the Initialized data segment & the Un-initialized data segment. All the global/static variables which are not initialized are created in the un-initialized part of the data segment which is set to zero by loader. The uninitialized segment usually referred as BSS(Block Starting with Symbol). BSS gets its name from old IBM systems that had segment initialized to zero. The layout of this segment is in the control of the underlying OS, though some loaders might give some control to the users. Pointers can be used to access the data in this segment. Code Segment: The code segment is used to store the executable code. This area is also known as the Text Segment & is of fixed size. Only function pointers can access this area. Its only read only segment so any attempt to write this area by user can lead to undefined behavior. Stack segment: Here automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the callers environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesnt interfere with the variables from another instance of the function. The stack is a collection of stack frames. When a new frame needs to be added (as a result of a newly called function), the stack grows downward. Every time a function is called, an area of memory is set aside, called a stack frame, for the new function call. This area of memory holds some crucial information, like:

1. Storage space for all the automatic variables for the newly called function. 2. The line number of the calling function to return to when the called function returns. 3. The arguments, or parameters, of the called function. Heap segment: The heap is for dynamic memory allocation i.e. whenever memory is allocated using malloc/ calloc/ realloc functions etc heap area is used. The C library also gets dynamic memory for its own personal workspace from the heap as well. As more memory is requested at run-time, the heap grows upward. The stacks and heaps are un-initialized area, so any uninitialized variables get a default value as garbage value.

También podría gustarte