![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| set Ulimit data segment to Unlimited | terala_s | UNIX for Advanced & Expert Users | 1 | 12-22-2007 12:10 PM |
| extract segment | mpang_ | Shell Programming and Scripting | 1 | 01-12-2007 01:33 AM |
| Network Access on Different Segment | HASM | IP Networking | 3 | 12-07-2006 12:01 PM |
| Segment and join tar archive | Pox | UNIX for Dummies Questions & Answers | 1 | 06-17-2006 11:24 AM |
| Extract data segment using awk?? | apalex | Shell Programming and Scripting | 1 | 07-27-2004 06:13 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Segment Fault
When run it, segment fault.
What is wrong? Code:
#include <stdio.h>
#include <stdlib.h>
const int max =20;
//****************************************************
// Input Matrix
//****************************************************
void inMatrixAA(int *AA, int row, int col)
{ int i,j;
for( i = 0; i<row; i++)
{
for( j = 0; j<col; j++)
scanf("%d",AA+i*max+j);
}
}
//*****************************************************
// Print Matrix
//*****************************************************
void prMatrix( int *AA, int row, int col)
{ int i,j;
for(i =0; i<row; i++)
{
for(j = 0; j<col; j++)
printf("%d\n",*(AA+i*max+j));
}
}
int main(int argc,char *argv[])
{
int m,k;
int A[max][max];
int ii,jj;
for (ii = 0; ii<max;ii++)
{
for(jj =0;jj<max;jj++)
{
A[ii][jj] = 0;
}
}
printf("Enter values for m, k");
scanf("%d",&m);
scanf("%d",&k);
printf("Input Matrix A");
inMatrixAA(&A[0][0], m, k);
printf("Print Matrix A");
prMatrix(&A[0][0], m, k);
}
|
|
||||
|
i wonder,
first this would not compile, Quote:
may be you would have thought providing an #define value to max Code:
#define max 20 |
|
|||||
|
Good work, Rakesh Ranjan. I like your code. As for zhshqzyc's code, while I do dislike the techniques being used, I don't find the code to as alien as everyone is indicating. I see two items that violate Ansi C: the // style one line comments and the use of "const int max =20;" instead of "#define max 20". Both of these are legal in C++. But also there is a new C standard out called C99. I don't have access to a C99 compiler, but I strongly suspect that the code is legal C99. And for the record, gcc accepts the code as well. When I run the code, it more or less works for me. I also dislike the output format and will never regard output a 2 dimensional array as a column "correct". The coding techniques being used invite the programmer to confuse rows and columns and that is what happened here. I used square matrices to avoid any problems.
Compiling the code on a compiler that does not understand the one line comment style results in a lot of errors. This is because a comment like: //****** has the beginning of a old c style comment starting with the second character. The compiler has a very hard time recovering from that error. If your C compilers are freaking out, I think that is the principal reason. Even gcc with an -ansi switch gave up, no -pedantic switch needed. |
|
|||||
|
Elegant piece of code!! I really appreciate the way u join the two functions. And yes stdlib really didn't mean to be there. I just skipped the non-essential include part and concentrated on the part where zhshqzyc might be erring. But on the combining of the two functions I would like to make a small comment though ur code does what is expected of the code but this might not be the real intension of the programmer, may be he was presenting it in most simple way. Although here the calls to get input and display results are sequential but in real problem he might be facing he may be calling input once and printing the elements twice or thrice so it doesn't make sense to combine two functions with two different functionality.
Further I quote Quote:
The calls are two separate calls and overhead of switching from main to ur function is also incurred twice (as it might have been, had there been two functions as before) rather I feel that ur induction of a global variable 'glb' makes the code more inefficient for most modern C compilers alias local variables to CPUs register's or SRAM but can't do so with global variables. Furthermore, if all variables in a given scope are local, then an optimizing compiler, can forgo maintaining the variable outside the scope, and therefore has more simplification optimization opportunities than with global. Add to it the time in evaluation of logical operators. The only way I feel ur code can be made really efficient might be by declaring the function static making the call a fixed function call. Please rectify me if I've gone wrong somewhere. Last edited by Rakesh Ranjan; 04-01-2006 at 07:13 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|