0

I'm beginner in C language and currently playing with pointers to understand. The following code creates an array of ten elements and I try to output the address of the first and second array element by using pointers here:

#include <stdio.h>

int main()
{
   int array[10]; 
   int i;

   for ( i = 0; i < 10; i++ ) {
      array[ i ] = i; 
   }

   int *ptr = &array[0];
   int *ptr2 = &array[1];

   printf("Element[%d] = %d  at Address = %x\n", 0, *ptr, ptr);
   printf("Element[%d] = %d  at Address = %x\n", 1, *ptr2, ptr2);

  return 0;
}

After compiling, I get the following output:

Element[0] = 0  at Address = 195ab640
Element[1] = 1  at Address = 195ab644

So the address of the first array element array[0] and the second array element array[1] are 195ab640 and 195ab644. There is always 4 difference between consecutive array element address numbers.

I was expecting if the first array element's address is m the next element address would be m+1 but I come across m+4. Why is the address is increased by four but not one?

I use this online compiler: https://www.onlinegdb.com/online_c_compiler

Chris Tang
  • 567
  • 7
  • 18
user16307
  • 131
  • 1
  • 9
  • 5
    The `int` on your platform is 4 bytes... – Patrick Roberts Jul 14 '19 at 19:22
  • 1
    next address element should be `previous_element_address + sizeof(type)` – AviatorX Jul 14 '19 at 19:26
  • 3
    `%x` is not a legal format for printing a pointer, you have to use `%p`. – n. m. could be an AI Jul 14 '19 at 19:28
  • @n.m. When I use %p I get the address at this format: 0x7fff0763dad4. This has 12 hexadecimal characters, And if each hexadecimal is 4 bits the address becomes 48 bits. Does that mean the memory is 2^48 bytes? Thanks – user16307 Jul 14 '19 at 19:43
  • 2
    @user16307: The virtual address space has that size. It's correspondence to actual memory is entirely determined by the page tables set up by the OS. – Linuxios Jul 14 '19 at 19:44
  • 1
    @user16307: See https://stackoverflow.com/questions/6716946/why-do-x86-64-systems-have-only-a-48-bit-virtual-address-space. – Linuxios Jul 14 '19 at 19:45
  • *Does that mean the memory is 2^48 bytes?* No. Memory is more complex than that. Valid addresses do not go from zero to the maximum. A pointer on a modern platform is typically 64 bits, but only a small part of the total 2^64 address space corresponds to existing memory, and it doesn't have to be all consecutive. – n. m. could be an AI Jul 14 '19 at 19:49
  • @user16307: It *could* mean that memory space is 48 bits wide - however, it’s more likely that the memory space is 64 bits wide and that `%p` is simply not showing leading zeros in the address value (i.e., the actual value is `0x00007fff0763dad4`). – John Bode Jul 14 '19 at 22:47

2 Answers2

4

(While none of this corresponds to the complex legalese of the C standards, I believe it is true for essentially every architecture in use today.)

A pointer points to a location in memory, and in modern computers, the basic addressable unit of memory is the byte. An int is typically 4 bytes -- you can determine its size on your system with sizeof(int). (This is true for at least x86, x86-64, and ARM. Still, always use sizeof for both portability and style.)

When you have an array of ints, the first 4 bytes, as in your case, correspond to a single integer. Similarly for the next four bytes, and so on. To increment the pointer to refer to the next integer in the array, at a low level, the machine must move forward 4 bytes, giving the behavior you saw.

(Other types have other sizes; you can see them in C with sizeof(long), sizeof(float), and so on.)

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • 1
    @PatrickRoberts: True. I guess a lot of us forget about embedded, which is a mistake. I've added a note to the answer to hopefully make it clearer. – Linuxios Jul 14 '19 at 19:42
0

The size of an integer on most systems is 4 bytes. Your compiler automatically deduces the the magnitude of the increment based on the data type, which, in your case is an int.