once again i struck with pointers

Ashokkumar01cbe

Broken In
#include <stdio.h>

#include <math.h>

main()

{

float x,y;

long int i, j, m[6], *k[5] ;

char *letter, *course[3], name [ ] = "I I T Madras";

course[0]="Numerical Methods";

course[1]=" and Programming";

printf ("\n%s %s\n ", course[0], course[1]) ;

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

m=3*i;

k[0]=&m[1];

k[3]=&m[3];

j=k[3] - k[0];

printf ("\n%d %d %d %d\n ", j, *k, *(k[0]+1), *k[3]) ;

}

i compile this program in a gcc compiler,in this program i struck at the last line that on printing *k as %d it needs to print the first element of the array (ie.. 0) instead it is displaying an address..please help me to solve this .....
 

Windows

Journeyman
First of all, this won't work
Code:
char *letter, *course[3], name [ ] = "I I T Madras";

course[0]="Numerical Methods";

course[1]=" and Programming";

Code:
for(i=0;i<=5;i++)

m[i]=3*i;
You never entered the values in m?
 

miltus_31

Broken In
long int i, j, m[6], *k[5] ;
In this you are declaring k as array of size 5 of pointer to long int.

So, each element of k is pointer to long int. That's exactly what you are getting.

side tip:
If you are doubtful about data types you may try this site.
cdecl
 

nbaztec

Master KOD3R
Code:
printf ("\n%d %d %d %d\n ", j, [COLOR="#FF0000"][B]*[/B][/COLOR]*k, *(k[0]+1), *k[3]) ;

And use %ld instead of %d.
 

gameranand

Living to Play
First study about pointers and then do the programming, it would really help you.
As for program
What do you really want to do with this program, elaborate, just writing the program and asking is not a good way. You need to tell what you want to do and state your logic which you know about the particular program. :)
 
Top Bottom