vinay
vinay

Reputation: 21

i try this piece of code in all possible way ,but i cant find why?

Here is the program in C and its output

#include <stdio.h>
#include <conio.h>

void main()
{
    int i, t[4], s[4];

    for(i=0;i<=3;i++)
    {
        printf("\n%d",&s[i]);
        printf("  %d",&t[i]);
    }

    for(i=0;i<=3;i++)
    {
        printf("\n%d %d",&s[i],&t[i]);
    }
}

output:

8600 8608
8602 8610
8604 8612
8606 8614

8600 8641
8602 8641
8604 8641
8606 8641

I want to know what exactly happened in second for loop statement that making different from first for loop.

Upvotes: 1

Views: 89

Answers (3)

Evil Dog Pie
Evil Dog Pie

Reputation: 2320

Your printf statements are printing an integer, put you're putting a pointer (&t[i] means address of the i th element of the t array).

An integer and a pointer are not necessarily the same number of bytes and most implementations of printf takes a fixed number of bytes from the stack for each % field. Also the 'endianism' of the machine will determine whether the least or most significant bit of the address are used as in integer when printf takes its field data from the stack. It looks like you are running on a 16 bit machine with 24 bit addresses and LSB ordering - some kind of micro-controller, I'd guess.

Your arrays are at the memory addresses (converted to hex from your output: s : 0xC12198 t : 0xC121A0 (24 bit addreses, I think.)

The first loop handles each array seperately in diffierent printf statements, hence you can see the least significant bits of each array incrementing with each iteration.

The second loop tries to handle both arrays in one `printf. So you get values indicating the incrementing part of one of the addresses, plus the second is the most significant part of the address, which is not incrementing, and the second array's address is not output at all.

Upvotes: 1

ams
ams

Reputation: 25559

The problem is that you are using the wrong format code for printing a pointer. As @PascalCuoq says, you should use %p, not %d.

The reason is that pointers and integers are clearly not the same size, on your system.

When you pass the two pointers to different printf calls %d will print the first part of the pointer value.

When you pass the two pointers to the same printf call, getting the lengths wrong will mean that it will print two different values that do not line up with either pointer.

Upvotes: 2

Pascal Cuoq
Pascal Cuoq

Reputation: 80255

The only obvious problem in your program is that you are passing pointer arguments corresponding to printf's %d format. This is undefined behavior. It can happen to work for some compilation platforms, but you shouldn't count on it.

The most likely explanation is that the ABI for passing pointer arguments to a variadic functions such as printf is, on your platform, different from the ABI for passing int arguments. For all we know, on your platform, pointers are not even the same width as int.

Use the %p format to print a pointer. Or better, use printf("%p", (void*)…);, which is even more portable, in case not all pointer types have the same representation.

Upvotes: 4

Related Questions