why am i getting garbage values in the outputCode:#include<stdio.h> void main() { int n[3][3]={{1,2,3},{4,5,6},{7,8,9}}; printf("\n%d %d %d", *n , n[0][0], n[2][2]); }
why am i getting garbage values in the outputCode:#include<stdio.h> void main() { int n[3][3]={{1,2,3},{4,5,6},{7,8,9}}; printf("\n%d %d %d", *n , n[0][0], n[2][2]); }
What did you expect to get from *n?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
isn't *n same as n[0][0]??
*n is NOT the same as anything....
and neither is
your programs should always look like the following unless your main is taking in command line argumentsCode:void main()
Code:int main(void) //void in the () is optional { . . . return 0; }
*n is going to be the same as n[0] (which is the same as &n[0][0]), which is a pointer to the first element of your array.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thats his prob, *n is not pointing to the array. hence the garbage output from the printf for the *n position
this code:
and this code:Code:#include<stdio.h> int main() { int n[3][3]={{1,2,3},{4,5,6},{7,8,9}}; printf("\n %d %d %d", *n , n[0][0], n[2][2]); return 0; }
are giving different outputs!! any explanations??Code:#include<stdio.h> int main() { int n[3][3]={{1,2,3},{4,5,6},{7,8,9}}; printf("\n %d %d %d", n[0][0] , n[0][0], n[2][2]); return 0; }
guess you want that
KurtCode:#include<stdio.h> int main() { int n[3][3]={{1,2,3},{4,5,6},{7,8,9}}; printf("\n %d %d %d", **n , n[0][0], n[2][2]); return 0; }
thanks a lot kurt !!
though can you or someone provide a little explanation since n is a double-dim array and *n should denote the value at its base address
Last edited by nmn; 03-20-2013 at 03:30 PM.
*n is a pointer to the first element in the array and that is an array of 3 integers and if you dereference that (**n) you get n[0][0]
Kurt
ok...got it!!
thanks...