Pointer Help

This is a discussion on Pointer Help within the C Programming forums, part of the General Programming Boards category; hi all, im new to pointers and im having problems with the program below, theres an error with pointer argument. ...

  1. #1
    cman
    Guest

    Pointer Help

    hi all, im new to pointers and im having problems with the program below, theres an error with pointer argument.
    What i want it to basically do is to get two numbers, compare them, highest one is stored in one pointer, and the other in another pointer.

    Code:
    #include <stdio.h>
    
    void int_sort(int*,int*);
    
    int main()
    {
    	int val1, *pval1, val2, *pval2;
    
    	printf( "Enter two numbers: " );
    	scanf( "%d %d", val1, val2);
    
    	pval1 = &val1;
    	pval2 = &val2;
    
    	int_sort(pval1,pval2);
    
    	printf( "val1: %d\tval2: %d\n", val1, val2 );
    
    	return 0;
    }
    
    void int_sort( int *pval1, int *pval2 )
    {
    	int store;
    
    	if ( *pval1 > *pval2 )
    		store = *pval1;
    	else 
    		store = *pval2;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your assignment is wrong.

    a = b;

    This puts the value of b into a, not the other way around. See what you have:
    Code:
    if ( *pval1 > *pval2 )
    		store = *pval1;
    	else 
    		store = *pval2;
    Your assignment is backwards.

    *pval1 = store;

    This will make the value of whatever 'pval1' points at hold whatever is currently in the value 'store'. Otherwise what happens is that you put the value of 'pval1'-contents into 'store'. Since 'store' is a local variable, it is destroyed when the function ends.

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

  3. #3
    .........
    Join Date
    Nov 2002
    Posts
    303
    Also this.
    Code:
    scanf( "%d %d", val1, val2);
    Should be.
    Code:
    scanf("%d %d", &val1, &val2);

  4. #4
    cman
    Guest
    scanf("%d %d", &val1, &val2);
    why do i need to put & on the val1 and 2 ?

    i assign it just after the scanf with the pointers?

    hmm

    im not sure

  5. #5
    .........
    Join Date
    Nov 2002
    Posts
    303
    You need to pass scanf an address otherwise it does not know where to put the value you entered.

  6. #6
    cman
    Guest
    i tried adding the ampersand, my program compiles but it doesnt work as planned..
    Whats wrong with it?

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    cman, your program works with the changes people told you:
    Code:
    #include <stdio.h>
    
    void int_sort(int*,int*);
    
    int main()
    {
    	int val1, *pval1, val2, *pval2;
    
    	printf( "Enter two numbers: " );
    	scanf( "%d %d", &val1, &val2);
    
    	pval1 = &val1;
    	pval2 = &val2;
    
    	int_sort(pval1,pval2);
    
    	printf( "val1: %d\tval2: %d\n", val1, val2 );
    	return 0;
    }
    
    void int_sort( int *pval1, int *pval2 )
    {
    	int store;
    
    	if ( *pval1 > *pval2 )
    		store = *pval1;
    	else 
    		store = *pval2;
    	
    	printf("store = %d  ",store);
    }

  8. #8
    cman
    Guest
    yep i know it works but its not what i want it to do.
    i want to store one value in one pointer and another value in another pointer, compare the two values, and then return the larger value first then the smaller one second.

    the program here just returns the higher value. sorry for the bad explanation.

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I'm not sure I understood you, but:
    Code:
    #include <stdio.h>
    
    void int_sort(int*,int*);
    
    int main()
    {
    	int val1, *pval1, val2, *pval2;
    
    	printf( "Enter two numbers: " );
    	scanf( "%d %d", &val1, &val2);
    
    	pval1 = &val1;
    	pval2 = &val2;
    
    	int_sort(pval1,pval2);
    
    	printf("The smaller: %d, and the bigger: %d\n",val2,val1);
    	return 0;
    }
    
    void int_sort( int *pval1, int *pval2 )
    {
    	int tmp;
    	
    	//this will put the max value in val2 and the smaller in val2
    	if (*pval1 < *pval2) {
    		tmp = *pval1;
    		*pval1 = *pval2;
    		*pval2 = tmp;
    	}
    }
    And you don't need pval1 and pval2 here, this could be like this too:
    Code:
    #include <stdio.h>
    
    void int_sort(int*,int*);
    
    int main()
    {
    	int val1,val2;
    	printf( "Enter two numbers: " );
    	scanf( "%d %d", &val1, &val2);
    
    	int_sort(&val1,&val2);
    
    	printf("The smaller: %d, and the bigger: %d\n",val2,val1);
    	return 0;
    }
    
    void int_sort( int *pval1, int *pval2 )
    {
    	int tmp;
    	
    	//this will put the max value in val2 and the smaller in val2
    	if (*pval1 < *pval2) {
    		tmp = *pval1;
    		*pval1 = *pval2;
    		*pval2 = tmp;
    	}
    }
    But maybe you want something special, I don't know.
    Ahh and you need somechanges to see if they're equal
    Last edited by Vber; 03-20-2003 at 05:16 AM.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by cman
    yep i know it works but its not what i want it to do.
    i want to store one value in one pointer and another value in another pointer, compare the two values, and then return the larger value first then the smaller one second.

    the program here just returns the higher value. sorry for the bad explanation.
    You can't return more than one value, so maybe you mean you want to swap the numbers around if one is bigger than the other.

    Does this help you:
    Code:
    #include <stdio.h>
    
    void changer(int *i1, int *i2)
    {
      int temp;
      if (*i1 > *i2)
      {
        temp = *i1;
        *i1 = *i2;
        *i2 = temp;
      }
    }
    
    int main(void)
    {
      int i, j;
      
      i = 1;
      j = 3;
      changer(&i, &j);
      printf ("Passing 1,3 gets %d, %d\n", i, j);
      
      i = 3;
      j = 1;
      changer(&i, &j);
      printf ("Passing 3,1 gets %d, %d\n", i, j);
      
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    cman
    Guest

    Smile

    thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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