Question about C

By JohnHassink

Ambassador (5655)

JohnHassink's picture

19-10-2010, 16:41

Hello & HEEEEEEEEEELP!!!!!

I'm sorry that this is not directly MSX-related, but in a sense it is, since I will use this knowledge to make MSX games in the future. Smile
I have a problem with understanding a certain aspect of pointers and I have nowhere else to go to ask this.

Say, that I would define a char and a pointer, like so:

char *pointer_1, character_1;

And if I'm not wrong, a char will take 8 bits = 1 byte on any platform, right?
Then I divert the pointer to the address where this char is being stored:

pointer_1=&character_1;

And I can do things with the parameters (?) in 4 ways known to me:

(1)

*pointer_1 +=1

(2)

++*pointer_1

(3)

(*pointer_1)++

(4)

*pointer_1++

So, ehm, I suppose that in the case of (1) and (4), the address to which the pointer is going will be raised with 1 byte, correct?
And nothing will happen with the value stored in that address?

What happens in case (2) and (3) with both value and address?
What is the difference? oO

I would be sooooo grateful if someone can help me.

Cheers

Login or register to post comments

By Creepy

Champion (335)

Creepy's picture

19-10-2010, 17:26

You are correct for case 4. In case 1, the address stays the same, the value is incremented. E.g. if character was 'A', it will be 'B' after case 1. Its the same for case 2 and 3. The operator += has a much lower precedence then the ++ operator which explain the difference between case 1 and 4. And before someone asks: I just tested it (on Linux gcc) Wink

You can test it yourself:

printf("%c\r\n", character1); // print the value
printf("%c\r\n", *pointer1); // print the value of what pointer1 points to
printf("%d\r\n", pointer1); // print the address of pointer1

Note: a char is not always 1 byte. It should be on MSX.

By JohnHassink

Ambassador (5655)

JohnHassink's picture

19-10-2010, 17:51

Thanks a lot!! Big smile

So, case 1, 2 and 3 make no practical difference (in this situation)?

Then I think I know enough and I'm set!

By JohnHassink

Ambassador (5655)

JohnHassink's picture

19-10-2010, 19:04

Thanks to your explanation, I have scored 16 out of 20 questions correct in my exam which I just took!
So a big thanks again! Smile

By Creepy

Champion (335)

Creepy's picture

19-10-2010, 22:45

A C exam? Wow, that's been a while Tongue And no problem, glad I could help.