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??

Dani AI

Generated

Short answer: the expression ptr + 19 moves the pointer by 19 elements, not 19 bytes. Because ptr was (improperly) given the numeric value 10, adding 19 int-s steps the address to 10 + 19 sizeof(int). On a typical system where sizeof(int) == 4, that yields 10 + 194 = 86, which is why you saw 86. That arithmetic is what and were getting at; was right to warn about mixing pointers and integers.

What is actually wrong with the program (not just confusing):

  • int *ptr = 10; is non-portable and dangerous. Converting arbitrary integers to pointers is implementation-defined and often undefined when used.
  • Doing pointer arithmetic on a value that does not point into a valid object is undefined behaviour.
  • Printing a pointer with printf("%d", ptr) is a format mismatch; use printf("%p", (void*)ptr).

A safe way to inspect the numeric address differences (for demonstration only) is to use uintptr_t/intptr_t and the proper print macros:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main(void) {
    int *ptr = (int*)(uintptr_t)10;   /* demonstration only */
    uintptr_t before = (uintptr_t)ptr;
    int *p2 = ptr + 19;
    uintptr_t after = (uintptr_t)p2;

    printf("ptr = %p\n", (void*)ptr);
    printf("ptr+19 = %p  (numeric = %" PRIuPTR ")\n", (void*)p2, after);
    printf("bytes added = %" PRIuPTR "\n", after - before);
    return 0;
}

Compile with warnings on (e.g. gcc -std=c11 -Wall -Wextra) and avoid using integer literals as pointer addresses in real code. Use real objects (arrays, allocated memory) and proper casts when you must convert between integers and pointers.

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 . 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.