1

Can any one explain this?

#include<stdio.h>

void FunPrinter(int *x)
{
 int i, j;
 for(i = 0; i < 4; i++, printf("\n"))
  for(j = 0; j < 5; j++)
   printf("%d\t",*x++);
}

int main()
{
 int x[][5] = {
        {17, 5, 87, 16, 99},
        {65, 74, 58, 36, 6},
        {30, 41, 50, 3, 54},
        {40, 63, 65, 43, 4}
       };
 int i, j;
 int **xptr = x;
 printf("Addr:%ld,Val:%ld,ValR:%ld",(long int)x,(long int)*x,(long int)**x);
 printf("\nInto Function\n");
 FunPrinter(&x[0][0]);
 printf("\nOut Function\n");
 for(i = 0; i < 4; i++, printf("\n"))
  for(j = 0; j < 5; j++)
   printf("%d\t",**xptr++);
 return 0;
}

Output:

Addr:140734386077088,Val:140734386077088,ValR:17
Into Function
17  5   87  16  99  
65  74  58  36  6   
30  41  50  3   54  
40  63  65  43  4   

Out Function
Segmentation fault (core dumped)

Why Direct Addressing is not working? I am accessing through pointers. I have used double pointer but it's not working. I have also tried to use single pointer as xptr. But still not working.

4
  • Also, why (long int)x and (long int)*x have same value? Commented Sep 25, 2012 at 7:33
  • 1
    The syntax of the first "for" in each section is absolutely dreadful! Drop it now if you do not want to be beaten up by future programming co-workers. Commented Sep 25, 2012 at 7:40
  • @jpinto3912 Can you mark the problems please? Commented Sep 25, 2012 at 8:12
  • It's legal syntax, but very unusual. A seasoned programmer will know it is ok, but will still find it odd that you placed cycle operations within the control section of the statement. You're allowed to comma separate any number of instructions, but it makes it less readable. So my advise: for(init; compare; inc/decrement){ operation if compare OK}. Commented Sep 25, 2012 at 10:16

1 Answer 1

1

You are getting a segmentation fault, because you are iterating over the wrong pointer (over the outermost dimension pointer). There are only 4 valid blocks on that dimension you can iterate over (not 20).

Here, x and *x (use %p to print pointers, not casting) are the same value, because they both point to the beginning of the array (same address), but the pointer arithmetic's are different (the size of element is different). You can iterate via x possibly needing to cast it to int *.

Also, the approach you are using in the FunPrinter - degrading a 2D array into 1D array is not guaranteed to work, it does indeed involve undefined behavior (although most reasonable compilers will compile this just fine).

Sign up to request clarification or add additional context in comments.

13 Comments

Actually I know that a 2D array is basically a 1D array in memory. So, I have used that concept in FunPrinter. But Can you explain the thing x and *x a little. and what is happening to xptr? I will be highly obliged to know.
int *xptr = &x; for(i = 0; i < 4; i++, printf("\n")) for(j = 0; j < 5; j++) printf("%d\t",*xptr++); Now, this is printing my output. But gives a warning: TwoDi.c:20:14: warning: initialization from incompatible pointer type [enabled by default]
@soham.m17 That's because you are not supposed to do this. Again, degrading a 2D array into 1D does involve undefined behavior. As for x and *x, they simply have the same value, the address of the beginning of the array. I don't really know what more to tell you.
What I want to do is to iterate over the array via pointer only. I understand x and *x have the value of the address of the beginning of the array i.e &x[0][0]
@soham.m17 There's no problem in that, but you need to switch between the inner and the outer dimension.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.