-3

Here is an example of my code (magic square):

    int magicsqr(int *magic,int size);
    int main()
    {
        int size,*ptr;
        char stop;
        repeat:
        printf("Please Enter an Odd number for the magic square(3 or greater):\n");
        scanf("%d",&size);
        fflush(stdin);
        ptr=(int*)calloc(size*size,sizeof(int));
        while((size%2==0)||(size<=1))
        {
            printf("U entered a wrong number.\n");
            repeat1:
            printf("Do you wish to continue?(Y or N)\n");
            scanf("%c",&stop);
            fflush(stdin);
            if(stop=='Y'||stop=='y')
                goto repeat;
            else if(stop=='N'||stop=='n')
                printf("Thanks for trying our beta program.\n");
            else
            {
                printf("U entered a wrong character.\n");
                goto repeat1;
            }
        }
        magicsqr(ptr,size);
        return 0;
    }
    int magicsqr(int *magic,int size)
    {
        int i,j,num;
        i=1;
        j=(size+1)/2;
        for(num=1;num<=size*size;num++)
        {
            *(magic+i*size+j)=num;
            if(num%size==0){
                i++;
                continue;
                }
            if(i==1)
               i=size;
            else
               i--;
            if(j==size)
               j=1;
            else
               j++;
        }
        for(i=1;i<=size;i++)
        {
            printf("\n");
            for(j=1;j<=size;j++)
                printf("%d\t",*(magic+i*size+j));
        }
}

so i got few questions that confused me ..

1- As far as i know Arr[i][j]==*(Arr[i]+j) so why only this works : *(magic+i*size+j).

2- I read a lot in passing 2 dimensional array into a function using pointers but somehow i am still confused, how to represent a 2D array or more in this code.

3- I am still a beginner in programming, so i wish if you could explain a little.

  • Thanks a lot guys, finally i got it worked using pointer to pointer and array of pointers.

3 Answers3

0

1- Ass far as i know Arr[i][j]==*(Arr[i]+j) so why only this works : (magic+isize+j).

Don't cast result of calloc when yo are doing

ptr = calloc(size*size,sizeof(int)); 

It's allocating 1D array (or ptr is 1D array pointer ) so (magic+isize+j) this is working. it's just conversion of 2D index to 1D to access array.

2- I read a lot in passing 2 dimensional array into a function using pointers but somehow i am still confused, how to represent a 2D array or more in this code.

For passing 2 dimensional array into a function have look on correct way of passing 2D array.

Community
  • 1
  • 1
Mohan
  • 1,871
  • 21
  • 34
0

This is not a 2D array at all, but a one dimensional array which is used to imitate a 2D array.

So, the memory allocation is

ptr=calloc(size*size,sizeof(int));

This is an allocation for a 1D array of (size * size) elements

Data is accessed for this array like this

*(magic+i*size+j)=num;

Each element position is calculated by from the base index by adding the no of rows * i + j.

These are called flat arrays.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • Now i get it but what should i do if wish to change it to a 2D ? – Mohamed Rabah Jan 21 '16 at 12:05
  • There are a lot of things you need to change. The declaration of ptr, the memory allocation, the function call, the function declaration and the function body will have to be modified to accept a 2D array instead of a 1D array. After that, you need to recompile it and see if any other changes are required. If the code is working, i don't think it is worth the effort. – Rishikesh Raje Jan 21 '16 at 12:11
  • Thanks a lot, and ye the code is working but i am just testing different methods for more understanding – Mohamed Rabah Jan 21 '16 at 12:13
  • Try rewriting the whole thing, with a double pointer. You will get a better understanding. – Rishikesh Raje Jan 21 '16 at 12:15
0

Finally got it, thanks for helping.

int main()
{
char stop;
int i,j,size,num;
repeat:
printf("Please Enter an Odd number for the magic square(3 or greater):\n");
scanf("%d",&size);
fflush(stdin);
int **magic=(int**)malloc(size*sizeof(int*));   //size = row
if((size%2==0)||(size<=1))
{
    printf("U entered a wrong number.\n");
    repeat1:
    printf("Do you wish to continue?(Y or N)\n");
    scanf("%c",&stop);
    fflush(stdin);
    if(stop=='Y'||stop=='y')
        goto repeat;
    else if(stop=='N'||stop=='n')
        printf("Thanks for trying our beta program.\n");
    else
    {
        printf("U entered a wrong character.\n");
        goto repeat1;
    }
}

for(i=0;i<size;i++)
    magic[i]=(int*)calloc(size,sizeof(int));
for(i=0;i<size;i++)
{
    printf("\n");
    for(j=0;j<size;j++)
        printf("%d\t",*(magic[i]+j));
}
i=0;
j=(size)/2;;
for(num=1;num<=size*size;num++)
{
    magic[i][j]=num;
    if(num%size==0){
        i++;
        continue;
        }
    if(i==0)
       i=size-1;
    else
        i--;
    if(j==size-1)
        j=0;
    else
        j++;
}
for(i=0;i<size;i++)
{
    printf("\n");
    for(j=0;j<size;j++)
        printf("%d\t",*(magic[i]+j));
}
return 0;
}