0

Could anyone explain why this doesn't print correctly? This is basic program with functions to read and print an array. All seems to be according to what I read... I'm new and can't seem to make pointers work. Thanks in advance!

#include <stdio.h>
#include <stdlib.h>

#define SIZE 2

void readArray(int *a);
void printArray(int *a);

int main (int argc, char *argv[])
{
    int array[SIZE][SIZE];

    readArray(&array[SIZE][SIZE]);

    printf("Array [1][2] = %d.\n\n\n", array[1][2]);

    printArray(&array[SIZE][SIZE]);

    system ("PAUSE");
    return 0;
}

void readArray(int *a)
{
    int i, j;
    for (i = 0; i < SIZE; i++)
    {
        for (j = 0; j < SIZE; j++)
        {
            printf("Array [%d] [%d]: ", i, j + 1);
            scanf("%d \n",&a);
        }
    }
}

void printArray(int *a)
{
    int i, j;
    for (i = 0; i < SIZE; i++)
    {
        for (j = 0; j < SIZE; j++)
        {
            printf("Array [%d] [%d]: ", i, j + 1);
            printf("%d \n",*a);
        }
    }
}
3
  • 3
    your parameter for readArray and printArray is a pointer to an int, NOT an array or a 2D array as you might need... first thing you should change is make the parameter be int a[][SIZE]... Commented Oct 1, 2014 at 4:05
  • Thank you for the reply. It still prints a sequence of numbers :( Now I get the warning: passing arg 1 of `readArray' makes pointer from integer without a cast. The same for the other function. Commented Oct 1, 2014 at 4:20
  • 1
    Please compile with all warnings and debug info (gcc -Wall -g) and use the debugger (gdb) Commented Oct 1, 2014 at 4:31

1 Answer 1

-1
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
         int i,j,a[2][2];
         printf("Enter array\n");
         for(i=0;i<2;i++) //take condition i<2 because of your default size
         {
           for(j=0;j<2;j++)
            {
              scanf("%d",&a[i][j]);
            }
         }
          printf("Printing array\n");
          for(i=0;i<2;i++)
          {
            for(j=0;j<2;j++)
            {
                printf("%d\t",*(*(a+i)+j));
            }
            printf("\n");
          }
getch();
}

try this code

Sign up to request clarification or add additional context in comments.

13 Comments

Thank you for the reply! Oh wow, I've never seen "*(*(a+i)+j)" thing before. That code is a thing I did to train for an activity in which the professor will ask to use functions :(. But your code helped, thanks!
@LuisFernandoCampos- That expression helps you to print array using pointer form. you are welcome.
@SumS: elaborate on using it in function-form
here is a simple clarification...for example, when you say a[3] all you are saying is *(a+3) because *a points to the first location of the array i.e. a[0] and since arrays in C are consecutive parts of memory, and pointers are just memory addresses, if you add 3 to the address of the first element you get the fourth element.
@SumS @chouaib Thanks! I've elaborated in a function and it worked. I only get bad results when printing like this: printf("Array [1][2] = %d.\n\n\n", array[1][2]);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.