-10

Why and when should I use one method or another? I am trying to learn more about pointers, but I cannot understand this kind of usage scenario.

int i = 12;
i += 1; // 13
i = 55; // 55

int x = 6;
int * y = &x;
*y += 1; // 7
*y = 91; // 91

I have researched this question already but could not find an answer, thus the post on SO. I am not asking the difference between what they do, I understand their effect on memory. I do not understand which should be used in the style of scenario above. The title was updated to reflect this misunderstanding.

jscs
  • 63,694
  • 13
  • 151
  • 195
MBarton
  • 2,087
  • 2
  • 14
  • 17
  • 2
    Where are you seeing code like the second half of the example? What leads you to believe that it's an actual practice? Without context, _there is no reason_ to use the second technique, so it's impossible to really figure out what to tell you. – jscs Aug 06 '15 at 19:41

4 Answers4

1

You'd use a pointer whenever access to an object needs to be done indirectly, such as:

  • when an object declared outside a function needs to be modified within a function (or when an array is passed to a function)
  • when an object is dynamically allocated with malloc or other allocation functions
  • when you're working with data structures (e.g. linked lists, tables, trees)
doppelheathen
  • 388
  • 4
  • 8
0
int i = 12;
i += 1; // 13
i = 55; // 55

In your example, you are creating a temporary integer variable i and storing the value 12 in it. You are then taking the value already stored in i (12) and adding 1 to it. i += 1; is the same as i = i + 1; in your case i = i + 1 is equivalent to this math:
i = 12 + 1 You are then throwing away the value that you have just stored in i and replacing it with 55. You have overwritten the data stored in the temporary variable i.

int x = 6;
int * y = &x;
*y += 1; // 7
*y = 91; // 91

Now, in the case of x and y, you x is a temporary integer variable just like i above. By saying int * y = [something]; however, you are declaring a temporary integer pointer to the memory location of some integer. Usually the memory address of some variable that has already been created. Setting y equal to the address of x sets the thing that the pointer is pointing to the location in memory of x. Under this setup, dereferencing y (*y) will yield the value of x, not the address of it. If you print out y, you will print the address of x. If you print out *y, you will print out the value of x. *y += 1 yields the value of x + 1. Setting *y to 91 resets the value of x, in the same way, that you have done above with i and 55.

Allow me to demonstrate the usefulness of this pointer through an example.

Let's say you had 4 integer pointers all set to point to the address of x (&x). Changing the value of any one of those pointers after dereferencing them (*ptr) will change the value stored in x and thus the value pointed to by each of the other 3 pointers you have initialized to the address of x.

By contrast, let's say you have 4 variables (int m, int n, int j, int k) all set equal to 4. When you increment k by 1, it will not change the value stored in m, n or j. It will only change k.

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Jeff Austin
  • 141
  • 4
-1

I know my answer will raise many eyebrows here but I can relate the question you asked here, so replying.

To keep your programming simple you can avoid the use of pointers as long as possible. Most of the programs you will write initially will not require pointers at all.

However, as you start writing complicated programs where you'll also need to keep the execution time as minimal as possible, you may want to use pointers.

Apart from time constraints, there are many other places where pointers will be used e.g. when you want to pass/return structures to/from a function. Because passing/returning structures as the value will put a load on the stack.

However, pointers are complicated and you may want to avoid them for writing simple programs if you really don't have any limitation of memory/time in your system.

You can anyways google (or read some books) about pointers for more information.

NIMISHAN
  • 1,265
  • 4
  • 20
  • 29
Pawan
  • 1,537
  • 1
  • 15
  • 19
-2

the basic difference between * and & as follows:
* - indicates the value of the operator
and
& - indicate the address of the operator

NIMISHAN
  • 1,265
  • 4
  • 20
  • 29
Riyaz Shaikh
  • 141
  • 9