LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2010, 01:12 PM   #1
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Rep: Reputation: 53
Pointer arithmetic question : *ptr++ or (*ptr)++


What is the difference between *ptr++ and (*ptr)++


In my opinion the terminology is all over the place so if you could give an example it would be better.


Thanks in advance
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-01-2010, 01:20 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Try it yourself.

Then Google for "operator precedence"

PS:
This isn't the most elegant code in the world, but hopefully it'll help clarify:
Code:
include <stdio.h>

#define LEN 5

void
init_array (int * p, int len)
{
  int i;
  for (i=0; i < len; i++)
    p[i] = 2 * i;
}

void
print_array (const char * msg, int * p, int len)
{
  int i;
  printf ("%s, p= 0x%x: ", msg, p);
  for (i=0; i < len; i++)
    printf ("%d ", p[i]);
  printf ("\n");
}

int
{
  int a[LEN];
  int *ptr = NULL;
  int j = -1;

  init_array (a, LEN);
  print_array("Initial array", a, LEN);

  ptr = a;
  print_array("ptr = a", ptr, LEN);

  j = *ptr++;
  print_array("*ptr++", ptr, LEN);
  printf ("j= %d\n", j);

  ptr = a;
  j = (*ptr)++;
  print_array("(*ptr)++", ptr, LEN);
  printf ("j= %d\n", j);

  return 0;
}
Here's the output:
Quote:
$ gcc -o x x.c

$ ./x
Initial array, p= 0xbffffac0: 0 2 4 6 8
ptr = a, p= 0xbffffac0: 0 2 4 6 8
*ptr++, p= 0xbffffac4: 2 4 6 8 1073831192
j= 0
(*ptr)++, p= 0xbffffac0: 1 2 4 6 8
j= 0
Notice how:
a) "*ptr++" changes the value of the POINTER (it now points to the second element, address 0xbffffac4

b) "(*ptr)++" changed the value POINTED TO (it now contains "1" instead of "0")

Again: please Google for "C operator precedence" for a better explanation

Last edited by paulsm4; 06-01-2010 at 01:42 PM.
 
2 members found this post helpful.
Old 06-01-2010, 01:27 PM   #3
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
I already googled it

Quote:
Originally Posted by paulsm4 View Post
Hi -

Try it yourself.

Then Google for "operator precedence"
This summarizes how it is explained on the sites that I can find.


Now, let's consider some of the things the above examples have shown us. First off,
consider the fact that *ptr++ is to be interpreted as returning the value pointed to by ptr
and then incrementing the pointer value. This has to do with the precedence of the
operators. Were we to write (*ptr)++ we would increment, not the pointer, but that which
the pointer points to! i.e. if used on the first character of the above example string the 'T'
would be incremented to a 'U'. You can write some simple example code to illustrate this.



The value a pointer holds is another address. So it appears that (*ptr)++ is meant to increment the data that that address stores. If it is an array and it contains a character it would increment a character which of coarse is crazy so I thought I might save myself some trouble and just ask someone. This is what the proceeding explanation states at it's face (literal) level.

Last edited by theKbStockpiler; 06-01-2010 at 02:06 PM. Reason: the hell of it
 
Old 06-01-2010, 03:34 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Minor clarification:

a) An "char" pointer increments 1 byte
b) An "int" pointer increments 4 bytes (on a 32-bit PC)
etc.

'Hope that helps .. PSM

PS:
this shows the relationship between the "address" ("pointer") and value of elements in two different arrays: a "char" array vs. an "int" array:
Code:
#include <stdio.h>

char a1[] = "ABC";
int  a2[] = {1, 2, 3, 4};

int
main(int argc, char *argv[])
{
  int i;
  printf ("i:  a1[i]:       a2[i]:\n");
  printf ("--  ------       ------\n");
  for (i=0; i < 4; i++)
    printf ("%d   %c:0x%x  %d:0x%x\n",
      i,
      (a1[i] ? a1[i] : '0'),
      &a1[i],
      a2[i],
      &a2[i]);

  return 0;
}
Code:
$ gcc -o x x.c

$ ./x
i:  a1[i]:       a2[i]:
--  ------       ------
0   A:0x80495d0  1:0x80495d4
1   B:0x80495d1  2:0x80495d8
2   C:0x80495d2  3:0x80495dc
3   0:0x80495d3  4:0x80495e0

Last edited by paulsm4; 06-01-2010 at 03:47 PM.
 
Old 06-01-2010, 10:13 PM   #5
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
Beginners can't decipher that much code

It looks like they are stating that you can do arithmetic on the values held by the pointers. Is this possible and how is it possible to do this with characters? How do you ++ an "H" or another letter? What does 1 and H equal? Is it 35?

Here's the part from the tutorial again

Now, let's consider some of the things the above examples have shown us. First off,
consider the fact that *ptr++ is to be interpreted as returning the value pointed to by ptr
and then incrementing the pointer value. This has to do with the precedence of the
operators. Were we to write (*ptr)++ we would increment, not the pointer, but that which
the pointer points to! i.e. if used on the first character of the above example string the 'T'
would be incremented to a 'U'. You can write some simple example code to illustrate this.







Thanks in advance

Last edited by theKbStockpiler; 06-01-2010 at 10:15 PM.
 
Old 06-01-2010, 11:36 PM   #6
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
H+1=73, which can also be I depending on how you look at it. That would also be the result of $ + %. Look up the ascii table if you don't get it.

Last edited by jay73; 06-01-2010 at 11:38 PM.
 
1 members found this post helpful.
Old 06-01-2010, 11:52 PM   #7
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
The context is unveiled

Quote:
Originally Posted by jay73 View Post
H+1=73, which can also be I depending on how you look at it. That would also be the result of $ + %. Look up the ascii table if you don't get it.

I get it. What is the the purpose of incrementing a character if you don't mind sharing? I don't see it a direct association with a pointer other than it can be done.Maybe if you want to go through the alphabet or something.

Thanks again
 
Old 06-02-2010, 06:45 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by theKbStockpiler View Post
...
If it is an array and it contains a character it would increment a character which of coarse is crazy ...
No, it isn't. In abstract/mathematical terms there exist ordered sets, and both, say, natural numbers and ASCII characters are examples of such ordered sets.

So, in ordered sets often increment/decrement operations are defined meaning getting previous/next set element.
 
1 members found this post helpful.
Old 06-02-2010, 11:12 AM   #9
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
This concept has helped in the overall use of pointers

Quote:
Originally Posted by Sergei Steshenko View Post
No, it isn't. In abstract/mathematical terms there exist ordered sets, and both, say, natural numbers and ASCII characters are examples of such ordered sets.

So, in ordered sets often increment/decrement operations are defined meaning getting previous/next set element.

I concur at this point that it is anything but crazy. Now the purpose of pointers is blatantly apparent.

Thanks again!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i create Ptr Record? epamuk Linux - General 2 03-09-2010 12:26 AM
PTR in DIG DNS palisetty_suman Linux - Newbie 7 05-04-2009 08:27 AM
PTR record OTIM Linux - Server 3 11-22-2007 12:16 PM
PTR problem csdhiman Linux - Server 2 11-18-2007 01:20 PM
dns+dynamicIP+PTR shadowsa Linux - Networking 5 01-11-2006 06:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration