HowTo increase 1 value to the pointer and pass by ref?

This is a discussion on HowTo increase 1 value to the pointer and pass by ref? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #define maxM 50 #define maxN 50 #define bool short #define true 1 #define false 0 ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    HowTo increase 1 value to the pointer and pass by ref?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define maxM 50
    #define maxN 50
    #define bool short
    #define true 1
    #define false 0
    const short dirt[][] = 
    	{{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}};
    
    /* global varibles */
    short m, n, half_tree;
    short jw[maxM][maxN];	// jw = jump way
    bool exist_way;
    /* End of global varibles */
    
    void extra_find(short x, short y, short *depth)
    {
    	int new_x, new_y, i;
    	for (i = 0; i < 8; i++) {
    		new_x = x+dirt[i][0];
    		new_y = y+dirt[i][1];
    		if ((new_x >= 0) && (new_y < m) && (new_y >= 0) && (new_y < n))
    			if (jw[new_x][new_y] == 0) {
    				jw[new_x][new_y] = half_tree + *depth;
    				if (*depth == m*n) {
    					exist_way = true;
    					exit(0);
    				}
    				else extra_find(new_x, new_y, &((*depth)++));
    				jw[new_x][new_y] = 0;
    			}
    	}
    }
    
    void find(short x, short y, short *depth)
    {
    	short x_coord, y_coord, new_x, new_y, max_depth, i;
    	bool expand;
    	x_coord = x;
    	y_coord = y;
    	expand = false;
    	for (i = 0; i < 8; i++) {
    		new_x = x+dirt[i][0];
    		new_y = y+dirt[i][1];
    		if ((new_x >= 0) && (new_y < m) && (new_y >= 0) && (new_y < n))
    			if (jw[new_x][new_y] == 0) {
    				expand = true;
    				jw[new_x][new_y] = *depth;
    				if (*depth == m*n) exit(0);
    				else if (*depth < half_tree)
    					find(new_x, new_y, &((*depth)++));
    				jw[new_x][new_y] = 0;
    			}
    	}
    	if (!expand) {
    		max_depth = *depth;
    		extra_find(x_coord, y_coord, &max_depth);
    	}
    }
    
    void double_bfs()
    {
    	short x_coord, y_coord, i, j;
    	for (i = 0; i < m; i++)
    		for (j = 0; j < n; j++)
    			jw[i][j] = 0;
    	for (x_coord = 0; x_coord < m; x_coord++)
    		for (y_coord = 0; y_coord < n; y_coord++) {
    			jw[x_coord][y_coord] = 1;
    			find(x_coord, y_coord, 2);
    		}
    }
    
    int main()
    {
    	FILE *fr, *fw;
    	short i, j; 
    	
    	fr = fopen("horse.dat", "rt");
    	fw = fopen("horse.out", "wt");
    	fscanf(fr, "%d %d", &m, &n);
    	half_tree = (int)((m*n)/2);
    	double_bfs();
    	if (exist_way)
    		for (i = 0; i < m; i++) {
    			for (j = 0; j < n; j++)
    				fprintf(fw, "%4d", jw[i][j]);
    			fprintf(fw, "\n");
    		}
    	else fprintf(fw, "No solution.");
    	fclose(fr);
    	fclose(fw);
    	return 0;
    }
    Code:
    horse.c:8: error: array type has incomplete element type
    horse.c: In function ‘extra_find’:
    horse.c:30: error: invalid lvalue in unary ‘&’
    horse.c: In function ‘find’:
    horse.c:52: error: invalid lvalue in unary ‘&’
    horse.c: In function ‘double_bfs’:
    horse.c:71: warning: passing argument 3 of ‘find’ makes pointer from integer without a cast
    HowTo increase 1 value to the depth pointer and pass by ref in find and extra_find function? What's wrong in this instruction?
    find(x_coord, y_coord, 2);

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    When you say "pass by ref", I assume you mean "pass by reference". There is no such thing in C, you are always passing a value.

    You can pass a pointer, but you are still passing a value. If you just want to increment the a pointer, you just pass its value:
    Code:
    foo(++pointer); /* increase the pointer, and pass the new value */
    foo(pointer++); /* increase the pointer, and pass the old value */
    When you pass the value of a pointer, you are passing the address of what the pointer points to. When you pass the value of &pointer, you are passing the address of the pointer.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Passing by reference is commonplace in C++, referred to as aliasing. Thus, you may want to consider C++ if you want to do pass by reference.

    I'm a little puzzled by your logic but what really has me scratchin' my head is the way you try to dereference the passed variable. It's looks like you're trying to dereference a single char out of a double pointer but yet you pass a numeric literal to the function as the variable to be dereferenced. If you can do that with a numeric literal, then it's one for the Guinness book of records.

    But anyway, here's a compiler simulation and I want to stress the importance of "compiler simulation" phrase of pass by reference:

    Code:
    #include <stdio.h>
    
    void PassByRef( int iInA, int *iInB );
    
    int main( void )
    {
        int iA = 100;
        int iB = 200;
        printf("Before iA = %d iB = %d\n", iA, iB );
        PassByRef( iA, &iB );
        printf("After  iA = %d iB = %d\n", iA, iB );
        return 0;
    }
    
    void PassByRef( int iInA, int *iInB )
    {
        *iInB = iInA; 
    }
    Uh, oh, I think I hear the ANSI C Standard Gestapo coming. Gotta run. Otherwise, they'll lock me up in the gulag for this pass by reference blasphemy.

    Later

    Bob

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Your code is perfectly fine ANSI C, and it's quite normal to do such a thing. It's just not really pass by reference, as you already clarified.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Mathsniper,

    You really don't need to do "passing by reference" in your code. It's really not relevent in the recursion. A short int variable will do fine. Also, I would strongly suggest you rethink your use of recursion in the sample posted. That sample will trash your stack space in no time flat.

    Good luck

    Bob

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    I am trying to solution the NP problem. the problem is: there are 2 varible m*n matrix. can we have a way jumping all the check which doesn't repeat by horse jumping method? obviously, m and n varible couldn't be big.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    This may be a little (way) over my head since I'm not a math major. But I'll at least give it a try.

    Can you provide a sample of your input and desired output? Also, a brief synopsis of the processing would be really beneficial.

    This math newbie's understanding of NP is that we are traveling between coordinates. We try to determine the next coordinate that we go to and finally we must reach an end point. If we dead end, then there is no solution. Also, we must do this traveling within a certain constraint. The constraint is the F(x) of the problem.

    Finally, can you direct me to tutorials, examples etc. of NP from a layperson's point of view?

    Bob

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    determine the varibles m, n. m = 3, n = 8, then we have give out a way.
    Code:
    1 4 7 10
    6 9 2 12
    3 11 5 8
    start at the check[0][0]. then go through all check which doesn't repeat. 1 -> 2 -> ... -> 11 --> 12.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have to be a math major. Just grab a knight and move it around on chess board of given dimensions N x M, and don't land on the same square twice.

    However, the above example fails.
    Code:
    01 04 07 10
    06 09 02 12
    03 11 05 08
    You cannot get from 10 to 11.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    sorry for giving out a wrong sample. however, the concept of method is like sample.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Well, I did some preliminary reading on NP and it was downright intimidating. Especially when it comes to completeness.

    But anyway, my understanding of the problem is that we have a 3 row by 8 column numeric array. We move the way a chess knight moves "L" shaped movement from the low value to the next lowest value and continue this movement to the highest value. If we cannot reach the highest value, then there is no solution.

    Is this correct?

    Bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21