Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

problem with printing two dimensional array

array printing

  • Please log in to reply
1 reply to this topic

#1 AlexB

AlexB

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:C
  • Learning:C

Posted 22 October 2012 - 10:34 AM




#include <stdio.h>

#include <stdlib.h>



#define ROW 2

#define COL 2



void func(int **arr)

{

    int i, j;



    arr = (int**)calloc(ROW,sizeof(int*));

    for(i=0; i < ROW; i++)

        arr[i] = (int*)calloc(COL,sizeof(int));



    printf("Input: \n");

    for(i=0; i<ROW; i++)

    for(j=0; j<COL; j++)

    scanf_s("%d", &arr[i][j]);

}



void print(int **arr)

{

    int i, j;

    for(i=0; i<ROW; i++)

    {

        for(j=0; j<COL; j++)

            printf("%5d", arr[i][j]);



        printf("\n");

    }

}



void main()

{

    int *arr;

    func(&arr);

    print(&arr);

}



//It seems like I'm having a problem with passing the arr address to the function.


Edited by dargueta, 22 October 2012 - 09:49 PM.
Please use code tags!

  • 0

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 23 October 2012 - 09:08 AM

You need to declare arr as int** and when allocating it you need to do:

*arr = (int **)calloc(ROW, sizeof(int *))

This means func() will accept an int*** but your print() should still accept int**, so pass arr instead of &arr to it in your main().
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download