Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,489 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,278 people online right now. Registration is fast and FREE... Join Now!




pointers - problem #2

 
Reply to this topicStart new topic

pointers - problem #2

pietra
21 Mar, 2007 - 01:05 AM
Post #1

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
So, I'm stuck at pointers. If anyone can help me understand this code I would be pleased.


This is one code devided in two parts.

CODE

#include <iostream>

int main(void)
{
int firstvalue, secondvalue = 12;
  int * p1, * p2;

  p1 = &firstvalue;  
  p2 = &secondvalue;
  
  *p1 = 10;          
  
  *p2 = *p1;        


I understand until this line, next line I don't understand. If I comment p1 = p2 everything works as expected. Well as I expect it. I expect firstvalue to be 20, and secondvalue to be 10.

CODE

  p1 = p2;          
  *p1 = 20;        
  
  std::cout << "firstvalue is " << firstvalue << std::endl;
  std::cout << "secondvalue is " << secondvalue << std::endl;
    
    getchar();
    return 0;
}


Output (without comments) is:
firstvalue is 10
secondvalue is 20

How can that be? The last command is *p1 = 20. Firstvalue should be 20, shouldn't it? *sighs*


*Topic title edit*

This post has been edited by pietra: 23 Mar, 2007 - 02:22 AM
User is offlineProfile CardPM
+Quote Post

horace
RE: Pointers - Problem #2
21 Mar, 2007 - 01:14 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
p2 contains the address of secondvalue, you assign p2 to p1 so p1 also contains the address of secondvalue - you then use *p1 to assign 20 to secondvalue
CODE

  p2 = &secondvalue;      
  p1 = p2;          
  *p1 = 20;        



User is offlineProfile CardPM
+Quote Post

pietra
RE: Pointers - Problem #2
21 Mar, 2007 - 01:24 AM
Post #3

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
Tricky smile.gif but I like it.

Thanks horace, you helped alot. smile.gif
User is offlineProfile CardPM
+Quote Post

pietra
RE: Pointers - Problem #2
23 Mar, 2007 - 02:20 AM
Post #4

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
I have problem with this statement case sizeof(char) : (*((char*)data))++;

I'm not sure what's being increased? ASCII number of x? Since output is "y, 1603". If so, how?

CODE

#include <iostream>


void increase (void* data, int size)
{
  switch (size)
  {
    case sizeof(char) : (*((char*)data))++;
    break;
    case sizeof(int) : (*((int*)data))++;
    break;
  }
}

                                                                        
int main (void)
{
  char a = 'x';
  int b = 1602;
  
  increase (&a,sizeof(a));
  increase (&b,sizeof(b));
  
  std::cout << a << ", " << b << std::endl;
  
  getchar();
  return 0;
}

User is offlineProfile CardPM
+Quote Post

horace
RE: Pointers - Problem #2
23 Mar, 2007 - 02:27 AM
Post #5

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
chars are representaed by byte sized integer values, e.g. ASCII
http://www.asciitable.com/
you are incrementing the charcater code

User is offlineProfile CardPM
+Quote Post

pietra
RE: Pointers - Problem #2
23 Mar, 2007 - 02:37 AM
Post #6

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
So if you have (pseudocode)

char = 'a'
char++
print char

output:
b

That's the point with chars? You can increment them and get next ASCII number that represents next letter or symbol?
User is offlineProfile CardPM
+Quote Post

horace
RE: Pointers - Problem #2
23 Mar, 2007 - 02:53 AM
Post #7

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(pietra @ 23 Mar, 2007 - 10:37 AM) *

So if you have (pseudocode)

char = 'a'
char++
print char

output:
b

That's the point with chars? You can increment them and get next ASCII number that represents next letter or symbol?

yes, inside the computer characters are represented by byte sized integers in C/C++ - so you can manipulate them like integers - cout << and cin >> converts betwen the internal (integer) and external (character on a screen or keyboard) representation
User is offlineProfile CardPM
+Quote Post

pietra
RE: Pointers - Problem #2
23 Mar, 2007 - 03:23 AM
Post #8

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
QUOTE(horace @ 23 Mar, 2007 - 03:53 AM) *

yes, inside the computer characters are represented by byte sized integers in C/C++ - so you can manipulate them like integers - cout << and cin >> converts betwen the internal (integer) and external (character on a screen or keyboard) representation

Ok, I wasn't aware of that. Thanks horace smile.gif


Can you help me with this?
(*((char*)data))

First asterisk is the dereferecne operator, "(char*)" is pointer that points on data, right?

I don't understand why they wrote ((char*)data) why couldn't it be just (char* data). Without one pair of brackets?

I'm headed to work now, so I might not be able to answer soon.
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Pointers - Problem #2
23 Mar, 2007 - 06:36 AM
Post #9

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
The (char *) part is a cast.
The statement (*((char*)data)) would evaluate (roughly) to
Give back what the (char pointer) data is pointing to.
That would be the first character of for example the string.

Then i see a ++
That is a dirty trick. You do the ++ operation on a pointer. The pointer will be moved further in memory by one elementsize of what it is pointing to.
So if it is a string (consisting of 8 bit characters) then the pointer will be moved to the next character.
If it is an array of 64 bit floats (and how large a float is is platform dependent) then the ++ will make it shift 8 bytes forward, to the next element.

Note again that the pointer it self is changed!

User is offlineProfile CardPM
+Quote Post

pietra
RE: Pointers - Problem #2
23 Mar, 2007 - 09:10 AM
Post #10

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
*nods to Trogdor*
I understand part wuth increment now, but I still don't understand why are brackets dividing "char *" and "data"? (*((char*)data))


If I remove those brackets and code looks like this (*(char*data)) I get compile time errors.

expected primary-expression before "char"
expected `)' before "char"
expected `)' before ';' token


User is offlineProfile CardPM
+Quote Post

horace
RE: Pointers - Problem #2
23 Mar, 2007 - 09:43 AM
Post #11

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
consider this section of code
CODE

    char ch='x';            // define a char variable initialised to x
    char *data;             // define a char * variable (pointer to char)
    data = &ch;             // assign the address of ch to data (data points to ch)
    cout << *data << endl;  // print character pointed to by data
    (*data)++;              // increment character pointed to by data
    cout << *data << endl;  // print character pointed to by data

which increments the character code stored in variable ch, e.g. it prints
x
y

you need the () in the expression (*data)++ because ++ has a higher precedance than *, i.e. the expression
CODE

    *data++

would increment the pointer data and then defreerence it

in
CODE

void increase (void* data, int size)
{
  switch (size)
  {
    case sizeof(char) : (*((char*)data))++;

the parameter data is of type void * which may point to any type so before you can use data you have to tell the compiler what it is, in this case a char with the expression
CODE

(char*)data

which casts it to a char *
User is offlineProfile CardPM
+Quote Post

pietra
RE: Pointers - Problem #2
23 Mar, 2007 - 02:36 PM
Post #12

D.I.C Head
**

Joined: 8 Feb, 2007
Posts: 70


My Contributions
Thank you for your help horace smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month