Thread: quick question

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    106

    quick question

    Hey everyone could someone tell me why the first code is working(without functions) and the second one is not?
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2,flag =1;
      int **A;
      int i = 0, j = 0;
      char a,c;
    
    
    do
      {
        A = malloc(2 * sizeof (int *));
    
    printf("\ncmd> ");
    c = getchar();
    
    if(c == 'A')
     {
    
    scanf("%d %d",&row_size1,&col_size1);
    
    A[0]= malloc(row_size1*sizeof(int*));
    
    for(i=0;i<row_size1;i++)
       {
           A[i]=malloc(col_size1*sizeof(int));
            }
    
    for(i=0;i<row_size1;i++)
         {
             for(j=0;j<col_size1;j++)
                  {
                    scanf("%d",&A[i][j]);
                    // if (A[i][j] == 99) // 'x' is character variable i declared
                         // break;
                  }
              }
    
    printf("The required matrix is\n");
    for(i=0;i<row_size1;i++)
        {
           for(j=0;j<col_size1;j++)
                printf("%d ",A[i][j]);
                printf("\n");
              }
    
    for(i=0;i<row_size1;i++)
         {
            free(A[i]);
              }
    free(A);
          
    }
    
    
    if(c == 'B')
    {
    scanf("%d %d",&row_size1,&col_size1);
    
    A[1]= malloc(row_size1*sizeof(int*));
    for(i=0;i<row_size1;i++)
        {
           A[i]=malloc(col_size1*sizeof(int));
              }
    
    for(i=0;i<row_size1;i++)
        {
           for(j=0;j<col_size1;j++)
                {
                    scanf("%d",&A[i][j]);
                    if (A[i][j] == 99) // 'x' is character variable i declared
                      break;
                  }
              }
    
    printf("The required matrix is\n");
    for(i=0;i<row_size1;i++)
        {
           for(j=0;j<col_size1;j++)
                printf("%d ",A[i][j]);
                printf("\n");
              }
    
    for(i=0;i<row_size1;i++)
         {
            free(A[i]);
              }
    free(A);
     }
     }
    
    while(flag == 1);
    return 0;
    }
    same code using functions
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2,flag =1;
      int **A=NULL;
      int i = 0, j = 0;
      char a,c;
    
    
    
    
    
    
    do
      {
        A = malloc(2 * sizeof (int *));
    
    
        printf("\ncmd> ");
        c = getchar();
    
    
        if(c == 'A')
          {
    
    
      scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
      A[0]= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          A[i]=malloc(col_size1*sizeof(int));
        }
    
    
    
    
    for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              scanf("%d",&A[i][j]);
     }
        }
     matrix_display(A[0], row_size1, col_size1);
          }
     if(c == 'B')
       {
    
    
         scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
         A[1]= malloc(row_size1*sizeof(int*));
         for(i=0;i<row_size1;i++)
           {
             A[i]=malloc(col_size1*sizeof(int));
           }
    
    
    
    
         for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
         matrix_display(A[1], row_size1, col_size1);
     }
      }
     while(flag == 1);
    //return 0;
    }
    
    
    void matrix_display(int *A, int row_size1, int col_size1)
    {
      int i,j;
      printf("The required matrix is\n");
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            printf("%d ",A[i][j]);
          printf("\n");
        }
      for(i=0;i<row_size1;i++)
        {
          free(A[i]);
        }
      free(A);
    }
    Last edited by zafy; 11-15-2012 at 07:42 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The first one might 'work', but it's still broken none the less.
    Code:
    $ valgrind ./a.out
    ==4438== Memcheck, a memory error detector
    ==4438== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==4438== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
    ==4438== Command: ./a.out
    ==4438== 
    
    cmd> A 3 2
    ==4438== Invalid write of size 8
    ==4438==    at 0x400786: main (bar.c:26)
    ==4438==  Address 0x51d2050 is 0 bytes after a block of size 16 alloc'd
    ==4438==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==4438==    by 0x4006FB: main (bar.c:14)
    ==4438== 
    1 2 3
    4 5 6
    ==4438== Invalid read of size 8
    ==4438==    at 0x4007B4: main (bar.c:31)
    ==4438==  Address 0x51d2050 is 0 bytes after a block of size 16 alloc'd
    ==4438==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==4438==    by 0x4006FB: main (bar.c:14)
    ==4438== 
    The required matrix is
    1 2 
    3 4 
    ==4438== Invalid read of size 8
    ==4438==    at 0x40081A: main (bar.c:40)
    ==4438==  Address 0x51d2050 is 0 bytes after a block of size 16 alloc'd
    ==4438==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==4438==    by 0x4006FB: main (bar.c:14)
    ==4438== 
    5 6 
    ==4438== Invalid read of size 8
    ==4438==    at 0x400878: main (bar.c:45)
    ==4438==  Address 0x51d2050 is 0 bytes after a block of size 16 alloc'd
    ==4438==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==4438==    by 0x4006FB: main (bar.c:14)
    ==4438== 
    
    cmd>
    Look at this code.
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
      int row_size1, col_size1, row_size2, col_size2, flag = 1;
      int **A[2];
      int i = 0, j = 0;
      char a, c;
    
    
      do {
    //    A = malloc(2 * sizeof(int *));
    
        printf("\ncmd> ");
        c = getchar();
    
        if (c == 'A') {
    
          scanf("%d %d", &row_size1, &col_size1);
    
          A[0] = malloc(row_size1 * sizeof(int *));
          for (i = 0; i < row_size1; i++) {
            A[0][i] = malloc(col_size1 * sizeof(int));
          }
    
          for (i = 0; i < row_size1; i++) {
            for (j = 0; j < col_size1; j++) {
              scanf("%d", &A[0][i][j]);
              // if (A[i][j] == 99) // 'x' is character variable i declared
              // break;
            }
          }
    
          printf("The required matrix is\n");
          for (i = 0; i < row_size1; i++) {
            for (j = 0; j < col_size1; j++)
              printf("%d ", A[0][i][j]);
            printf("\n");
          }
    
          for (i = 0; i < row_size1; i++) {
            free(A[0][i]);
          }
          free(A[0]);
        }
      }
    
      while (flag == 1);
      return 0;
    }
    If you want, you can start with
    int ***A;
    and
    A = malloc( 2 * sizeof(int**) );
    to replace the array of 2 elements.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Oh I thought the first one was perfect but thanks

    but why isn't the second one even compiling? I tried the int ***A

    but i honestly thought that this line
    Code:
    A = malloc( 2 * sizeof(int**) );
    

    just tells the program I am allocating space for 2 matrices A and B

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you turned everything into pointers, then you need

    void matrix_display(int **A, int row_size1, int col_size1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    is this what you mean?
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2,flag =1;
      int **A=NULL;;
      int i = 0, j = 0;
      char a,c;
    
    
    
    
    
    
    do
      {
        A = malloc(2 * sizeof (int **));
    
    
        printf("\ncmd> ");
        c = getchar();
    
    
        if(c == 'A')
          {
    
    
      scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
      A[0]= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          A[0][i]=malloc(col_size1*sizeof(int));
        }
    
    for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              scanf("%d",&A[0][i][j]);
              //      if (A[i][j] == 99) // 'x' is character variable i declared
                        // break;
            }
        }
     matrix_display(A[0][i][j], row_size1, col_size1);
          }
     if(c == 'B')
       {
    
    
         scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
         A[1]= malloc(row_size1*sizeof(int*));
         for(i=0;i<row_size1;i++)
           {
             A[1][i]=malloc(col_size1*sizeof(int));
           }
    
    
    
         for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[1][i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
         matrix_display(A[1][i][j], row_size1, col_size1);
     }
      }
     while(flag == 1);
    }
    
    
    void matrix_display(int **A, int row_size1, int col_size1)
    {
      int i,j;
      printf("The required matrix is\n");
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            printf("%d ",A[i][j]);
          printf("\n");
        }
      for(i=0;i<row_size1;i++)
        {
          free(A[i]);
        }
      free(A);
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    No, nothing like what I meant.

    int ***A;

    A is a pointer to a number of matrices
    A[0] is a pointer to the first matrix
    A[0][0] is a pointer to the first row of the first matrix.


    To display one of the matrices, you would do
    matrix_display(A[1], row_size1, col_size1);

    I suggest you prototype the function before calling it, then you would get some useful diagnostics.
    Though TBH, I can't see how this compiles at all without a great deal of complaining from the compiler.

    FWIW, matrix_display seems good now, if you can figure out how to call it properly.

    Perhaps you should start with
    int **A[2];
    and get that working properly, before trying to allocate something for int ***A;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    please tell me this is what you mean
    way too many warnings!
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    void matrix_display(int *matrix, int row_count, int column_count);
    
    
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2,flag =1;
      int **A[2];
      int i = 0, j = 0;
      char a,c;
    
    
    
    
    
    
    do
      {
        A = malloc(2 * sizeof (int **));
    
    
        printf("\ncmd> ");
        c = getchar();
    
    
        if(c == 'A')
          {
    
    
      scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
      A[0]= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          A[0][i]=malloc(col_size1*sizeof(int));
        }
    
    
    
    
    for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              scanf("%d",&A[0][i][j]);
              //      if (A[i][j] == 99) // 'x' is character variable i declared
                        // break;
            }
        }
     matrix_display(A[0], row_size1, col_size1);
          }
     if(c == 'B')
       {
    
    
         scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
         A[1]= malloc(row_size1*sizeof(int*));
         for(i=0;i<row_size1;i++)
           {
             A[1][i]=malloc(col_size1*sizeof(int));
           }
    
    
    
    
         for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[1][i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
         matrix_display(A[1], row_size1, col_size1);
     }
      }
     while(flag == 1);
    //return 0;
    }
    
    
    void matrix_display(int *A, int row_size1, int col_size1)
    {
      int i,j;
      printf("The required matrix is\n");
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            printf("%d ",A[i][j]);
          printf("\n");
        }
      for(i=0;i<row_size1;i++)
        {
          free(A[i]);
        }
      free(A);
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void matrix_display(int *matrix, int row_count, int column_count);
    Are you paying even the slightest bit of attention?
    In my last post, I said matrix_display was OK, and you go and muck it up again!

    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:15:8: warning: unused variable ‘a’ [-Wunused-variable]
    bar.c:12:37: warning: unused variable ‘col_size2’ [-Wunused-variable]
    bar.c:12:27: warning: unused variable ‘row_size2’ [-Wunused-variable]
    bar.c:92:1: warning: control reaches end of non-void function [-Wreturn-type]
    $ 
    $ valgrind ./a.out
    ==6204== Memcheck, a memory error detector
    ==6204== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==6204== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
    ==6204== Command: ./a.out
    ==6204== 
    
    cmd> A
    3 2
    1 2 3
    4 5 6
    The required matrix is
    1 2 
    3 4 
    5 6 
    
    cmd> 
    cmd> ^C==6204== 
    ==6204== HEAP SUMMARY:
    ==6204==     in use at exit: 0 bytes in 0 blocks
    ==6204==   total heap usage: 4 allocs, 4 frees, 48 bytes allocated
    ==6204== 
    ==6204== All heap blocks were freed -- no leaks are possible
    ==6204== 
    ==6204== For counts of detected and suppressed errors, rerun with: -v
    ==6204== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
    TWO changes.
    1. fix the prototype of matrix_display
    2. remove line A = malloc(2 * sizeof (int **));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    So I did what you said I got warnings but... THE program works!
    in total I need 4 variables A B C and D so I added for C and D and once again we're back to square 1
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    void matrix_display(int *matrix, int row_count, int column_count);
    
    
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2,flag =1;
      int ****A[4];
      int i = 0, j = 0;
      char a,c;
    
    
    
    
    
    
    do
      {
        //  A = malloc(2 * sizeof (int **));
    
    
        printf("\ncmd> ");
        c = getchar();
    
    
        if(c == 'A')
          {
    
    
      scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
      A[0]= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          A[0][i]=malloc(col_size1*sizeof(int));
        }
    
    
    
    
    for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              scanf("%d",&A[0][i][j]);
              //      if (A[i][j] == 99) // 'x' is character variable i declared
                        // break;
            }
        }
     matrix_display(A[0], row_size1, col_size1);
          }
     if(c == 'B')
       {
    
    
         scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
         A[1]= malloc(row_size1*sizeof(int*));
         for(i=0;i<row_size1;i++)
           {
             A[1][i]=malloc(col_size1*sizeof(int));
           }
     for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[1][i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
         matrix_display(A[1], row_size1, col_size1);
    
    
    
    
       }
    
    
     if(c == 'C')
       {
    
    
         scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
         A[2]= malloc(row_size1*sizeof(int*));
         for(i=0;i<row_size1;i++)
           {
             A[2][i]=malloc(col_size1*sizeof(int));
           }
    
    
         for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[2][i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
         matrix_display(A[2], row_size1, col_size1);
       }
    
    
     if(c == 'D')
       {
    
    
         scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
         A[3]= malloc(row_size1*sizeof(int*));
         for(i=0;i<row_size1;i++)
           {
             A[3][i]=malloc(col_size1*sizeof(int));
           }
    
    
         for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[3][i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
         matrix_display(A[3], row_size1, col_size1);
       }
    
    
    
    
      }
     while(flag == 1);
    //return 0;
    }
    
    void matrix_display(int ****A, int row_size1, int col_size1)
    {
      int i,j;
      printf("The required matrix is\n");
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            printf("%d ",A[i][j]);
          printf("\n");
        }
      for(i=0;i<row_size1;i++)
        {
          free(A[i]);
        }
      free(A);
    }

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    I made the same changes right?

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    what thing did i miss again?

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by zafy View Post
    Code:
    void matrix_display(int *matrix, int row_count, int column_count);
    void matrix_display(int ****A, int row_size1, int col_size1)
    The prototype doesn't match the definition, and now you have... how many stars in there?

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Yes! I got it
    honestly guys thank you so much I appreciate it so much

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I asked you to do it on the last topic you posted - Can you PLEASE fix your indentation

    This is my preferred style - Indent style - Wikipedia, the free encyclopedia
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question
    By zach48191 in forum C Programming
    Replies: 7
    Last Post: 10-21-2011, 12:05 AM
  2. Quick question about std
    By blueshift1980 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2009, 10:43 PM
  3. Quick Question
    By Cdrwolfe in forum C++ Programming
    Replies: 9
    Last Post: 06-21-2006, 02:56 PM
  4. Quick question
    By Stiks in forum C Programming
    Replies: 6
    Last Post: 10-10-2005, 08:35 PM
  5. quick hex question
    By shoobsie in forum C Programming
    Replies: 3
    Last Post: 06-29-2005, 08:36 AM