2

What is the difference between first and second case, why does the first work as expected, while the second doesn't? (in the second case i am trying to introduce a pointer that dereferences the pointer to pointer, in order to avoid typing extra asterisks).

int _tmain(int argc, _TCHAR* argv[])
{
    int* test = NULL;
    foo(&test);
}

case 1:

void foo(int** ppPar)
{
    *ppPar = (int*)malloc(sizeof(int));
    **ppPar = 7;
}

case 2:

void foo(int** ppPar)
{
    int* pPar = *ppPar;
    pPar = (int*)malloc(sizeof(int));
    *pPar = 6;
}
anatolyg
  • 26,506
  • 9
  • 60
  • 134
Brans Ds
  • 4,039
  • 35
  • 64

3 Answers3

6
*ppPar = ...

This reassigns the pointer test that was in main;

**ppPar = 7

This changes the value pointed at by test in main.


int* pPar = *ppPar;
pPar = 

This makes a copy of the pointer test that was in main, and then reassigns the copy. (So now there is no connection to test at all.

*pPar = 6;

This changes the value pointed at by pPar, which is no longer associated with test.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • what is reccomended way to introduce new variable to not to type extra asterix everyvere in foo (for pointer to pointer)? Or i should live it ? – Brans Ds Aug 07 '14 at 17:00
  • 1
    @BransDs: If you want to refer to an _existing_ variable, you have to use pointers. (C++ has alternatives, but not C) You could use a macro, but that's generally considered to be bad form. Use a pointer. – Mooing Duck Aug 07 '14 at 17:08
1

Let's take the main idea of your code, and simplify it:

int *x = malloc(sizeof(int));
int y = *x;
y = 5;

In this code, it's relatively clear that while the value of y has changed, the value of *x has not.

In your code, you've added an additional level of indirection to everything. This doesn't change the main thrust of my point though:

int** ppPar = (something useful);
int* pPar = *ppPar;
pPar = (int*)malloc(sizeof(int));

Again, it should be clear that while pPar has changed, *ppPar has not.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • As it stands this answer ist difficult to understand. (I do however) – alk Aug 07 '14 at 17:27
  • @alk: That's fair. I've rewritten it some, but it still may not be clear to someone who doesn't already understand the reasoning. Oh well. – Bill Lynch Aug 07 '14 at 17:35
1

In the second example you assign to the local pointer pPar twice:

void foo(int** ppPar)
{
  int* pPar = *ppPar; /* 1st assigment */
  pPar = (int*)malloc(sizeof(int));  /* 2nd assignment */

As the 2nd assigmnent overwrites the result of the 1st assignment, the 1st assignment obviously is redundant, useless.

Then you assigen 6 to what had been allocated

  *pPar = 6;  

but do not return the reference to it back up.

To do so add

  *ppPar = pPar;
}

The final code might look like this:

void foo(int** ppPar)
{
  int * pPar = malloc(sizeof(int));
  /* of even better: int * pPar = malloc(sizeof(*pPar)); */
  *pPar = 6;
  *ppPar = pPar; 
}
alk
  • 69,737
  • 10
  • 105
  • 255