Hi, can anybody help me with this program output.

Program is based on GCC compiler standards

#include<stdio.h>
main()
{
    int *ptr=10,j;
    j=ptr+19;
    printf("ptr=%d\n",ptr);
    printf("j=%d\n",j);
}
output: ptr=10
        j=86

when ptr value is 10 in statement "j=ptr+19", why the j value is 86??

Recommended Answers

All 15 Replies

Because j is a local variable it takes whatever value that happens to be on the stack...To fix this initialize j to 0

int j = 0;

No, that might not be the reason. :icon_confused:

Dear, Adding n to a pointer mean go to nth location starting from here. Adding 1 would make make the pointer go to next memory location. Now, j is an integer pointer. Therefore its size is 4 (in linux).
Hence it move to next 19 memory location i.e. 4*19+10=86.

If you would have run this program in window (2 byte). you would have get 2*19+10=48

:-)

No, that might not be the reason. :icon_confused:

Its still unwise to use uninitialized local variables.

Ya, but it must be initialise by a address location or NULL (which is nothing but zero)

Ya, but it must be initialise by a address location or NULL (which is nothing but zero)

I'm not talking about the pointer, I'm talking about the j integer which is uninitialized.

It is not neccessary to initialise j there...it could be iniatialise in next line...that would not pose any problem.

It is not neccessary to initialise j there...it could be iniatialise in next line...that would not pose any problem.

I think I need to go to the optometrist....

Dont worry it happens

Explaination is the same as what navedalam said. But I am surprised that this even compiles. At least there should be some compiler warnings. In any case, this kind of usage has to be handled with great care. int *ptr=10,j; ptr is a pointer to int . j is an int .
The types are different. So you won't be able to assign one to the other without a cast .

int *ptr = (int*)10, j;
    j = (int)(ptr+19);

hi navedalam, ur explanation is convincing but j is not integer POINTER it is integer variable. So, by ur explanation...

ptr=10. so, 10*4 = 40. Now, this 40 shud be added to 19 b'coz j=ptr+19. i.e 40+19=59.
Therefore, j's value shud be 59. Please give me ur view..

Thank you.

Wolf pack, i agree with you. We get warnings for this program if typecasting is not done..

hi navedalam, ur explanation is convincing but j is not integer POINTER it is integer variable. So, by ur explanation...

ptr=10. so, 10*4 = 40. Now, this 40 shud be added to 19 b'coz j=ptr+19. i.e 40+19=59.
Therefore, j's value shud be 59. Please give me ur view..

Thank you.

When you add a integral value to a pointer, you use the integral * unit size. Unit size in this case is sizeof(int).

So

j = ptr + 10

is really

/*pseudocode*/
j = ptr + (10 * sizeof(int));

thanq gerard4143, WolfPack, navedalam..i got ur points..:)

The code below will demonstrate why pointer addition behaves this way

#include <stdio.h>

#define ARR_SIZE 4

int mya[ARR_SIZE] = {1233, 543, 567, 789}; 

int main(int argc, char**argv)
{
	int i = 0;
	int *iptr = mya;

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "value->%d\n", iptr[i]);/*array notation*/

	fputs("\n\n", stdout);

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "value->%d\n", *(iptr + i));/*add integral to pointer*/
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.