1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
14
Rep Power
0

Pointer problem

I've been stuck on this for hours, and it would be great if someone can help me out here. When I run my program below to generate all permutations for numbers from 1 to 4, calling print_ptr (green) produces the correct results. However, when calling place with the same pointer, I find that within the place function, somehow it has not received the correct input. How can this be corrected?

#include <stdio.h>
#include <stdlib.h>
void print_ptr(char*pointer, int n)
{int i;

for (i = 0; i < n; i++)
{printf("%d", pointer[i]);
}
printf("\n");
}
void place(char*input,char*output, int n)
{int i, j, k;
print_ptr(input, n-1);
for (i = 0; i < n; i++)
{ output[i] = 0;//initializing output array
}
output[input[0]] = 1;//can input first term directly
for (j = 1; j < n - 1; j++)//for each element in input array
{ k = 0;
while (input[j] > -1)
{
if (output[k] == 0)
{input[j] = input[j] - 1;
}
k = k + 1;
}
output[k-1] = j + 1;
}
i = 0;
while (output[i] != 0)
{
i++;
}
output[i] = n;
print_ptr(output, n);
}
int main (void)
{int a,b,c,d,e,f,g,h,i,j,k,l,n;
char*ptr, *ans;

n = 4;
ptr = (char*)malloc((n-1)*sizeof(char));
ans = (char*)malloc(n*sizeof(char));

{ for (a = 0; a < 4; a++)
{ ptr[0] = a;
for (b = 0; b < 3; b++)
{ ptr[1] = b;
for (c = 0; c < 2; c++)
{ ptr[2] = c;
//place(ptr, ans, n);
//print_ptr(ptr, n-1);
}
}
}
}
return 0;
}
2. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
37
Inside the function place() you change the contents of the dynamic array ptr inside a while loop. I believe that leads you to think the function "has not received the correct input".

Anyway: how did you found out the input was not correct?

Also, for pedantism, you really should free the allocated memory before terminating the program :)
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
14
Rep Power
0
Originally Posted by bdb
Inside the function place() you change the contents of the dynamic array ptr inside a while loop. I believe that leads you to think the function "has not received the correct input".

Anyway: how did you found out the input was not correct?

Also, for pedantism, you really should free the allocated memory before terminating the program :)
Yeah, I'm still quite new to C :) but while what you said is right, I printed out the input with print_ptr(input, n-1); before changing the pointer inside the place() function. This is different to the input that should be there - which I printed out with the print_ptr () function inside main. Negative numbers start appearing...
Edit: Very strangely, exactly half the elements in the input array are right, with the other half containing the aforesaid negative numbers
4. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
37
Inside place() you call print_ptr() twice. The first time at the beginning of the function and the second time at the end. Between these two times you cahnge the contents of the array.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
14
Rep Power
0
Originally Posted by bdb
Inside place() you call print_ptr() twice. The first time at the beginning of the function and the second time at the end. Between these two times you cahnge the contents of the array.
Yes, but the first time is to print the input array, the second time to print the output array. Here is my part of my output to show you what I mean:
000 (first element of input)
1234 (first element of output)
0-11 (second element of input, this should be 001)
1430 (output wrong due to incorrect output)
010
1324
0-11 (this should be 011)
1430
6. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
37
Ok. This is what happens:

1) in main, set ptr[0], ptr[1], ptr[2]
2) call place()
3) inside function place() call print_ptr() with correct output
4) still inside function place() change input[1] and input[2] (corresponding to ptr[1] and ptr[2] of main())
5) call print_ptr() with the output
6) inside main() again, in the foor loop for the variable c, change ptr[2]. Notice ptr[1] has what print_ptr() put there.
7) call place() again
8) call print_ptr() and print pointer[0] (value 0), pointer[1] (value -1), and pointer[2] (value 1)
9) etc ...
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
14
Rep Power
0
Thanks! You are quite right. :)