different output

why code 1 works fine whereas code 2 doesn't?????????

code 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
void main( )
{
int a[3][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 0, 1, 6
} ;
display ( a, 3, 4 ) ;

}
display ( int *q, int row, int col )
{
int i, j ;
for ( i = 0 ; i < row ; i++ )
{
for ( j = 0 ; j < col ; j++ )
printf ( "%d ", * ( q + i * col + j ) ) ;

printf ( "\n" ) ;
}
printf ("\n" ) ;
}


code 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
void main( )
{
int a[3][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 0, 1, 6
} ;

int i, j ;
for ( i = 0 ; i < 3; i++ )
{
for ( j = 0 ; j < 4 ; j++ )
printf ( "%d ", * ( a + i * 4 + j ) ) ;

printf ( "\n" ) ;
}
printf ("\n" ) ;
}


code 1 prints the array whereas code 2 prints a bunch of addresses
This code does not compile, much less run.
- void main() is illegal (use int main())
- You must declare display() before you try to call it in main()
- display() takes an int* as its first parameter, but you are passing it an int (*)[4]
code 1 ran fine on codeblocks with some warnings..
closed account (48T7M4Gy)
code 1 ran fine on codeblocks with some warnings..


Some people are just lucky. But I think that dream has just been shattered . You need to fix your code as indicated!

Sorry, but there is no way code 1 will run on any IDE on the face of the planet for the reasons given. If it did then ask for your money back.

code 2 runs if you change void to int. But the output might make a bit more sense if you do something like:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int  main( )
{
int a[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 0, 1, 6}
} ;


for ( int i = 0 ; i < 3; i++ )
{
   for ( int j = 0 ; j < 4 ; j++ )
       printf ( " %i ",   a[i][j] * 4 + j  ) ;

       printf ( "\n" ) ;
}
printf ("\n" ) ;
}
i dont know which compiler you are using but this code works on codeblocks and online ide too,
try

http://goo.gl/VhANXj

also if i wanted to do a[i][j] type of thing i would have done it before it is too easy but i am trying to learn a different method here.

q has base address of array, i*col+j increments the address one by one thus * ( q + i * col + j ) prints the value in the array

i was asking why is this not working in code 2
closed account (48T7M4Gy)
LOL
closed account (48T7M4Gy)
*(* ( a + i ) + j )
@ kemort
you are also right but i was trying to do it differently
i got it i was trying to increment the address directly i made some changes now it is working fine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
void main( )
{
int a[3][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 0, 1, 6
} ;

int i, j, *c;
c=&a;
for ( i = 0 ; i < 3; i++ )
{
for ( j = 0 ; j < 4 ; j++ )

printf ( "%d ", *( c + i * 4 + j ) ) ;

printf ( "\n" ) ;
}
printf ("\n" ) ;
}

i made a pointer pointing to base address of the array and then incremented that pointer and now it is working fine..
thanks everyone :)

but now
i am not getting why it printed addresses in gap of 16 when i was trying to increment the address of the array
output was like
482236192 482236208 482236224 482236240
482236256 482236272 482236288 482236304
482236320 482236336 482236352 482236368
closed account (48T7M4Gy)
1
2
int i, j; // note: it's unusual to declare i and j in this way when they are to be used in a for loop
int* c = &a[0][0];
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int  main( ) // just change it to this 
{
int a[][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 0, 1, 6}
} ;

int* c = &a[0][0];

for ( int i = 0 ; i < 3; i++ )
{
   for ( int j = 0 ; j < 4 ; j++ )
      printf ( "%d ", *( c + i * 4 + j ) ) ;

   printf ( "\n" ) ;
}
printf ("\n" ) ;
}


1 2 3 4 
5 6 7 8 
9 0 1 6 

 
Exit code: 0 (normal program termination)

Last edited on
Topic archived. No new replies allowed.