The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-27-2006
zhshqzyc zhshqzyc is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 34
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);
   
   }
  #2 (permalink)  
Old 02-28-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
i wonder,
first this would not compile,

Quote:
int A[max][max];
you need to mention an integral constant expression

may be you would have thought providing an #define value to max

Code:
#define max 20
  #3 (permalink)  
Old 02-28-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
I tried to compile this ealrier and found even more issues. I just gave up. If it really compiles for the OP, she/he's got a very interesting compiler. Maybe a C++ compiler being used for C.
  #4 (permalink)  
Old 02-28-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Tried to compile this a few hours ago on HP-UX 11.11. Being a non ANSI compliant compiler, the thing puked on so many points, I didn't even try to fix the problems.
  #5 (permalink)  
Old 03-28-2006
Rakesh Ranjan's Avatar
Rakesh Ranjan Rakesh Ranjan is offline
Registered User
  
 

Join Date: Aug 2005
Location: India
Posts: 42
I dare to rectify ur code though I agree with others that ur code shouldn't compile.

Code:
#include <stdio.h>
#include <stdlib.h>
#define max 20

//****************************************************
//     Input Matrix
//****************************************************

void inMatrixAA(int AA[max][max], int row, int col)
{     int i,j;
        for( i = 0; i<row; i++)
           {
               for( j = 0; j<col; j++)
                 scanf("%d",&AA[i][j]);
           }
}

//*****************************************************
//   Print Matrix
//*****************************************************

void prMatrix( int AA[max][max], int row, int col)
{       int i,j;
           for(i =0; i<row; i++)
              {
                 for(j = 0; j<col; j++)
                   printf("%d\n",AA[i][j]);
              }
}

int main(int argc,char *argv[])
   {
       int A[max][max];
       int m,k;
       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\n");
       scanf("%d%d",&m,&k);
       printf("Input Matrix A");
       inMatrixAA(A, m, k);
       printf("Print Matrix A");
       prMatrix(A, m, k);
       return 0;
   }
It shall compile.
I would like u to advise :
1) In C use constants expressions in specifying the size of array
U may use preprocessors like #define though.
2) Take care with pointers (if u r passing pointer to pointer in
invoking the function then keep the same in defintion of the function)

Hope these help u.
  #6 (permalink)  
Old 03-28-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
  #7 (permalink)  
Old 04-01-2006
Rakesh Ranjan's Avatar
Rakesh Ranjan Rakesh Ranjan is offline
Registered User
  
 

Join Date: Aug 2005
Location: India
Posts: 42
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:
your code can be further optimised to have a common function as below instead of having two seperate functions
I really couldn't figure it out how this tweak of urs ensured code optimization.
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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0