Why iterating through an array by incrementing pointer gives such output?

This is my code snippet, I was trying to print the elements of the arr by accessing its values from the pointers.
#include <stdio.h>

/**
* main - entry point
* Return: Always sucess (0)
*/

int main(void)
{
/* Declaring vars */
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i;
int* parr = arr;

/* Try to iterate by pointer incrementation */
for (; *parr != '\0'; parr++)
{
/* output: arr elements plus an unknown element */
printf("%d ", *parr);
}

printf("\n");

/* Try to iterate by index */
for (i = 0; i < 10; i++)
{
/* output: arr elements exactly as they are */
printf("%d ", arr[i]);
}

printf("\n");

/* Try to iterate by both index and pointer */
for (i = 0; i < 10; i++)
{
/* output: arr elements in hexadecimal format */
printf("%p ", *&arr[i]);
}

return (0);
}
#include <stdio.h>

/**
* main - entry point
* Return: Always sucess (0)
*/

int main(void)
{
/* Declaring vars */
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i;
int* parr = arr;

/* Try to iterate by pointer incrementation */
for (; *parr != '\0'; parr++)
{
/* output: arr elements plus an unknown element */
printf("%d ", *parr);
}

printf("\n");

/* Try to iterate by index */
for (i = 0; i < 10; i++)
{
/* output: arr elements exactly as they are */
printf("%d ", arr[i]);
}

printf("\n");

/* Try to iterate by both index and pointer */
for (i = 0; i < 10; i++)
{
/* output: arr elements in hexadecimal format */
printf("%p ", *&arr[i]);
}

return (0);
}
This is the output; look at the first line of the output, why this 33 showed up?
1 2 3 4 5 6 7 8 9 10 **33**
1 2 3 4 5 6 7 8 9 10
0000000000000001 0000000000000002 0000000000000003 0000000000000004 0000000000000005 0000000000000006 0000000000000007 0000000000000008 0000000000000009 000000000000000A
1 2 3 4 5 6 7 8 9 10 **33**
1 2 3 4 5 6 7 8 9 10
0000000000000001 0000000000000002 0000000000000003 0000000000000004 0000000000000005 0000000000000006 0000000000000007 0000000000000008 0000000000000009 000000000000000A
Thanks in advance 😇
1 Reply
sigma
sigma•16mo ago
Basically, iterating an array by a pointer is, most of the time, a risky operation, because with only the pointer, you don't know how long the array is. You are using the value '\0', which is just 0, to stop the iteration, but C does not make any guarantees that any element after the last one in an array has 0. Thus, your loop continues right after the last element, until it finally finds a 0. The iteration could have continued until the program crashed due to a segmentation fault or similar. I suggest that, in the first loop, you print the addresses that you are using to access, as well as the address of the i variable, and the parr variable. You can do all of them with:
printf("%p\n", (void *)parr);
printf("%p\n", (void *)&i);
printf("%p\n", (void *)&parr);
printf("%p\n", (void *)parr);
printf("%p\n", (void *)&i);
printf("%p\n", (void *)&parr);
If it looks confusing, the first printf prints the address that parr is holding, while the third printf prints the address of parr itself. My guess as to why it printed 33 and stopped is because the 33 comes from printing whatever i is holding before being initialized, and the reason it stopped right after is because there might be some padding in the memory place after the i, and that padding was, by pure coincidence, 0.
Want results from more Discord servers?
Add your server