0

I'm studying C++ using a few books, trying to learn SDL along side it. I understand that pointers "point" to a variable's memory address, and that they can be used to "reference" variables. But I don't understand their purpose? And how to use them properly?

I have a few examples from the book (I've added under the code that confuses me):

#include <iostream>

int main(void)
{
    char string[6] = "Hello";
    char* letter = string;
    letter += 3; 

OK, so there's a char pointer called 'letter' that points to the memory address of string. Then somehow we use the += operator on the pointer? How? What's going add? What are we adding the 3 to?

    *letter = 'p';

And now here we use '*letter' instead of 'letter' - this means it's dereferenced, right? What does this actually DO?

    string[4] = '!';
    std::cout << string << '\n';
    system("PAUSE");

    return 0;
}

The rest of the code I understand.

Thanks for any answers!

George

EDIT: so let me get this straight - dereferencing a pointer (e.g. *pointer = 2;) is used to change the value of the variable (or array position, for that matter) when you want to?

EDIT 2: thanks to everybody's answers I almost completely understand the code I used as an example - however, I am still unsure as to the use of '&' (ampersand) in the context of pointers, and how/why they're used.

George_
  • 13
  • 5

4 Answers4

1

You need a start from understanding pointers from beginning.

char string[6] = "Hello"; //string an array of 6 characters including null char
char* letter = string;    //letter pointer pointing to string array,stores first char address
letter += 3;  // Letter pointer in incremented by 3 char. Now it points to 4th char of string
*letter='p';  //replacing 4th char of string by p
cout<<letter; //prints "po";

Notes to help you

What are the barriers to understanding pointers and what can be done to overcome them?

Community
  • 1
  • 1
shivakumar
  • 3,297
  • 19
  • 28
  • Thanks for the link and the comments - it also helps clear it up a little. So by default, the pointer letter is set to position one (or zero depending on how you look at it) of the string array? Ah this is confusing :D – George_ Feb 08 '14 at 14:19
  • `string` is an array of **6** characters. – juanchopanza Feb 08 '14 at 14:24
  • Yeah I get that, I meant why is the pointer set to point to an array position in the first place - shouldn't it be pointing to the array's memory address, so to speak. Or are they equivalent? – George_ Feb 08 '14 at 14:27
1

In this definition

char* letter = string;

letter is set to the address of the first element of character array string.

So *letter has value 'H'. Let consider for example statement

++letter;

This statement "moves" the pointer to point to the next element of string. That is now the pointer points to the second element of string. Now the value pf *letter is 'e' because letter points to the second element.

Applying three times operator ++ as for example

++letter; ++letter; ++letter;

is equivalent to

letter += 3;

that is the pointer was moved three times and now it points to the second 'l' in string.

This statement

*letter = 'p'

will replace 'l' with 'p' and you will get that string now looks as

"Helpo"

After executing statement

string[4] = '!';

string contains

"Help!"

Instead of string[4] = '!';

you could write

++letter;
*letter = '!';

Or you could combine these two statements in one statement

*++letter = '!';

EDIT: 'pointer' contains address of an object. When you write *pointer you access directly the object itself so *pointer = 2 change the object pointed by 'pointer'.

Also you should understand that if for example you have

int a[10];
int *p = a;

and let assume that the value of p (the address stored in p) is 4. Then expression ++p or p + 1 means that the pointer was "moved" to the next element of the array. It means that new value of p is not 5. The new value of p is p + sizeof( int ) that is 8 provided that sizeof( int ) is equal to 4.

In your example sizeof( char ) is always equal to 1 according to the C++ Standard.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Aha! This answer has been super helpful! Thank you! The only part that I am still unclear about is to why the pointer can be said to point to the array's first position - I was thinking that pointers point originally to the variable memory address. Are the memory address and array equivalent? Thanks! :) – George_ Feb 08 '14 at 14:23
  • @George_ Because array consists from several objects. When a pointer points to a regular object it contains the address of the object. When you have an array then the pointer points to its first element. It is in fact is the memory address of the array. Arrays are placed in memory starting from their first elements. – Vlad from Moscow Feb 08 '14 at 14:27
  • OK. I think it's just a thing I'll have to accept. Although it sounds unusual to me haha. So a pointer pointing to a variable points to its address. A pointer pointing to an array points to the first element of the array by default. In any case, what's the use of a pointer pointing to a variable's memory address? – George_ Feb 08 '14 at 14:29
  • In your latest edit -- are you referring to the memory address of int and how that's not affected? Is the expression sizeof(int) effectively evaluating the number of bit occupied by int, and hence the memory address of int? – George_ Feb 08 '14 at 14:42
  • sizeof( int ) means the number of bytes occupied by an object of type int. So p + 1 means that the value of p is increased by sizeof( int ) that the pointer would be moced to point to the next element in the array. This is so-called pointer arithmetic. – Vlad from Moscow Feb 08 '14 at 14:47
  • Ok - but wouldn't that imply that each element of the array occupies 4 bits? Bit confused, sorry. – George_ Feb 08 '14 at 14:50
0

Then somehow we use the += operator on the pointer? How? What's going add? What are we adding the 3 to?

letter is in fact pointing to the first element of the string Hello. By adding 3 to it letter will point to the element 3 of the string (4th element).

And now here we use '*letter' instead of 'letter' - this means it's dereferenced, right? What does this actually DO?

Defreference means you are retrieving the value stored at the location letter points to.

ereferencing a pointer (e.g. *pointer = 2;) is used to change the value of the variable (or array position, for that matter) when you want to?

By doing *pointer = 2; you are storing 2 to the location pointer points to, ie, changing the value of the variable..

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Thank you - this clears it up a little. The string[4] = '!'; part of the code does work though - are you saying it shouldn't ? :) – George_ Feb 08 '14 at 14:17
  • @George_; That was typo. I corrected it. It will work. – haccks Feb 08 '14 at 14:18
  • OK sure, so basically letter pointer points to first element. I thought pointers by default point to a variable memory address though? That's what I don't understand. Is an array a sort of memory address in that sense? Why don't we need to use '&'?? – George_ Feb 08 '14 at 14:21
  • When you declare a pointer then it can point anywhere in the memory (dangling pointer). You need to initialize it by passing variable address to it before using it in the program. If the variable is not a pointer or array type ( in case of array, array names converted to pointer to its first element in most cases) then you do not need `&` operator, otherwise you need `&` operator. – haccks Feb 08 '14 at 14:24
0

Incrementing a pointer by an integer value (explained with an example):

Suppose you have int* x pointing to memory address 1000, and you increment it by 3: x += 3.

Variable x will now point to memory address 1000 + sizeof(int).

The size of int depends on your compiler; let's assume it is 4 bytes on your system.

So variable x is pointing to memory address 1012 (1000+3*4).

Now, suppose you use x in order to read the value at the memory address pointed by x:

int y = *x;

During runtime, the CPU will read 4 bytes from the memory address pointed by x, and store them into variable y, so that y will then contain the same value as the one stored in address range 1012-1015.

barak manos
  • 29,648
  • 10
  • 62
  • 114