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);
        }
    }
}
rici
  • 234,347
  • 28
  • 237
  • 341
  • 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]`... – nem035 Oct 01 '14 at 04: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. – Luis Fernando Campos Oct 01 '14 at 04:20
  • 1
    Please compile with all warnings and debug info (`gcc -Wall -g`) and **use the debugger** (`gdb`) – Basile Starynkevitch Oct 01 '14 at 04:31

1 Answers1

-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

SumS
  • 25
  • 6
  • 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! – Luis Fernando Campos Oct 01 '14 at 04:28
  • @LuisFernandoCampos- That expression helps you to print array using pointer form. you are welcome. – SumS Oct 01 '14 at 04:29
  • @SumS: elaborate on using it in function-form – chouaib Oct 01 '14 at 04:31
  • 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. – nem035 Oct 01 '14 at 04:41
  • @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]);` – Luis Fernando Campos Oct 01 '14 at 04:48
  • @LuisFernandoCampos what you mean by `bad results`! can you give us your inputs and outputs ? – chouaib Oct 01 '14 at 04:53
  • @nem The textbook my professor is using doesn't mention that. I'm working with it now. Thanks! – Luis Fernando Campos Oct 01 '14 at 04:57
  • @chouaib That line outputs `Array [1][2] = 4199456`. The array I input was 1,1,1,1. Meanwhile the function I made with @SumS' code prints just fine. :((( – Luis Fernando Campos Oct 01 '14 at 05:01
  • @LuisFernandoCampos you probably don't have the element `Array[1][2]` you only have `Array[0][0]`,`Array[0][1]`,`Array[1][0]`,`Array[1][1]` – chouaib Oct 01 '14 at 05:03
  • @LuisFernandoCampos: `Reading or writing to an object or array at an offset that is negative, or beyond the size of that object (stack/heap overflow)` you are having an undefined behavior ! – chouaib Oct 01 '14 at 05:05
  • That's right, thanks! But now `printf("Array [0][1] = %d.\n\n\n", array[0][1]);` also outputs `Array [0][1] = 4199456`. (Input was 1,1,1,1). – Luis Fernando Campos Oct 01 '14 at 05:15
  • This does not answer the OP's question. – alk Oct 01 '14 at 05:58
  • @LuisFernandoCampos-I am trying to learn online at most of the time.I found some sites.I think it is fast approach to learn.Try it. If our expert friends share some of good sites with us ,then it is surely helpful to all of us.Thanx in advance. – SumS Oct 02 '14 at 05:26