1

Here is the c code:

#include<stdio.h>

int main()
{
    int a[]={0,1,2,3,4};
    int*p[]={a,a+1,a+2,a+3,a+4};
    int**ptr=p;
    ptr++;

    printf("%d , %d  ",*ptr-a , **ptr);
    *ptr++;
    printf("\n%d , %d  ",*ptr-a , **ptr);
    *++ptr;
    printf("\n%d , %d ",*ptr-a , **ptr);
    ++*ptr;
    printf("\n%d , %d ", *ptr-a , **ptr);

    return 0;
}

the gdb error highlighted in black??

I am unable to understand the error and what should be amended in the code.

7
  • 2
    You didn't let debugger execute line 8. I guess that's the reason you are getting this error. Commented Aug 1, 2017 at 11:31
  • Yes that works. Thank you. I didn't know how break points work.. Commented Aug 1, 2017 at 11:36
  • You need to cast *ptr - a to int, because the resulting type is ptrdiff_t: ideone.com/CfjqzO Also the * in *ptr++; is unnecessary. Commented Aug 1, 2017 at 11:36
  • Ok thanx .. mch . I will do that. Commented Aug 1, 2017 at 11:40
  • 1
    BTW, the message Warning: Source file is more recent than executable should be taken into account. You need to recompile your program Commented Aug 1, 2017 at 13:15

2 Answers 2

1

You have not yet executed line 8 in gdb.

8    int **ptr = p;

So till now ptr is not defined yet in your program. Hence, can not access it. You can press n command in gdb prompt and then follow by print

(gdb) n 
(gdb) print *ptr

Also, your program should have %ld instead of %d in printf as format specifier to silence compiler warning as in

printf("%ld , %ld  ",*ptr-a , **ptr);
Sign up to request clarification or add additional context in comments.

Comments

1

Replace %d with %ld then it should be working fine.

You can refer format specifiers details for further clarification on this.

2 Comments

*ptr - a results in a type of ptrdiff_t. "%ld" is for long. Since C99, should use "%td".
It's also worth recommending to add -Wformat to the compilation command (or, better, -Wall -Wextra).

Your Answer

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