Could anyone explain me, how these two *(ptr + i) = i; and *(ptr + (i - 1); process to get the result ? I don't understand that syntax, could anyone tell me in clearer way?

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i;
	int *ptr;
	int number;
	printf("How many ints would you linke to store?");
	scanf("%d", &number);
	ptr = malloc(number * sizeof(int));
	if (ptr != NULL)
	{
		for (i = 0;i < number;i++)
		{
			*(ptr + i) = i;
		}
		for (i = number;i > 0;i--)
		{
			printf("%d\n", *(ptr + (i - 1)));
		}
		free(ptr);
		return 0;
	}
	else
	{
		printf("\nMemory allocation failed - not enough memory.\n");
		return 1;
	}
}

Recommended Answers

All 4 Replies

Let's say you have a row of beers[1] and you're looking at the first one. Oh, your name also happens to be ptr. Nice to meet you Mr. Ptr. Here's the game:

You want the first beer, so you try to drink from it. Let's say that to drink from a beer you're looking at you can say *ptr[2]. But gosh durn it, when you start to drink, you notice floaters in the beer. That's disgusting, so you put the beer down and decide to move on.

To move to another beer you simply have to shuffle to the side. Clearly if you want to move to the first beer, you don't have to shuffle at all because you're already there, right? Just to be mathematically rigorous, you might call that shuffling zero times, or (ptr + 0). To go from one beer to the next, you shuffle once, so to go to the second beer, you say (ptr + 1) because you want to shuffle once. The third beer is (ptr + 2), and so forth.

So you shuffle to the second beer by saying (ptr + 1). If you want to go back to the first beer because you suddenly decide you like floaters (you freak!), you can shuffle in the other direction by saying (ptr - 1). I don't like floaters, so let's go back to the second beer and then drink it: *(ptr + 1).

That was whimsical, wasn't it? But that's the basics of adjusting a pointer by an offset. A pointer points to an address. When you add to or subtract from that address, you shuffle the pointer to point to another address. Shuffling to the current address is always an offset of 0.

Because array notation is just a syntactic sugar for a pointer offset, that neatly explains why arrays are zero based in C: a[0] is the first element of the array because a is converted to a pointer to the address of the first element and the array notation becomes *(a + 0).

[1] How many beers are in the row is quite irrelevant at this point, but as a somewhat responsible adult, I can't let you have more than 3 without a designated driver.

[2] It kind of looks like you're chugging a beer, but only after you've already downed a few.

Thanks so much but sorry, still don't get it. Could u please make it clearer? Step by step instruction. I knw about * pointer, but in this term I don't get it. I m just a beginner to programming. Thanks again.

>Could u please make it clearer?
Nope, that's about as simple as I can get without drawing pictures right in front of you. Tell me what you do understand, what don't understand, and I'll try to clarify it for you. I'm guessing that you're asking for a simple explanation while at the same time overcomplicating the simple explanation I've already given you.

>Step by step instruction.
That's what I did! What's so hard about you looking at a bottle, *you drinking from the bottle, you+1 stepping to the right then looking at the next bottle, and you-1 stepping to the left then looking at the previous bottle? If you don't get it, go physically do it and then complain that I wasn't being clear.

>I m just a beginner to programming.
That's why my explanation used real-world concepts. I guess you don't drink. :icon_rolleyes:

Haha :) thanks so much anyway, now I get it. I was trying to understand the concept. Thanks.

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.