4 Replies - 608 Views - Last Post: 19 March 2010 - 04:28 PM Rate Topic: -----

#1 Guest_Andy*


Reputation:

Pointers Help

Posted 19 March 2010 - 03:44 PM

So I have a project for school and it is about pointers, and I cant figure it out. The program is supposed to swap the contents of two memory locations using a function. Swapping should occur only within the function. And it has to be done with pointers. Here is the code I got so far

#include <stdio.h>

void swap(int x, int y)
{
	int local_int = x;
	x = y;
	y = local_int;

	printf("\nIn the function first number is %d\nIn the function second number is %d\n", x, y );
 system ("pause");
}

void main()
{
	int first_num = 10;
	int second_num = 50;
	

	printf("\nThe value of first number is %d \nThe value of second number is %d \n",  
                           first_num,  second_num );

    swap(&first_num, &second_num); 

	printf("\nThe value of first number is %d \nThe value of second number is %d \n",  
                           first_num,  second_num );
                           
}



Is This A Good Question/Topic? 0

Replies To: Pointers Help

#2 Guest_Andy*


Reputation:

Re: Pointers Help

Posted 19 March 2010 - 03:47 PM

oh and it is in C not C++
Was This Post Helpful? 0

#3 hlemke  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 18-January 10

Re: Pointers Help

Posted 19 March 2010 - 04:11 PM

Your swap function accepts integers, but you're passing it integer pointers. &first_num is a pointer, since it's an address.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Andy*


Reputation:

Re: Pointers Help

Posted 19 March 2010 - 04:19 PM

well I kind of got it but now in the output it says "In the function the first number is 2621268" and "In the function the first number is 2621264" and it is supposed to say 50 and 10 so idk why it is happening

#include <stdio.h>

void main()
{
	int first_num = 10;
	int second_num = 50;
	
	printf("Before swapping\n");
	printf("\nThe value of first number is %d \nThe value of second number is %d \n",  
                           first_num,  second_num );

    swap(&first_num,&second_num); 

    printf("\nAfter swapping\n");
	printf("\nThe value of first number is %d \nThe value of second number is %d \n",  
                           first_num,  second_num );
    getch();                     
}
swap(int *x,int *y)
{
	int local_int = *x;
	*x = *y;
	*y = local_int;

	printf("\nIn the function first number is %d\nIn the function second number is %d\n", x, y );
    return(0);
}


Was This Post Helpful? 0

#5 hlemke  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 18-January 10

Re: Pointers Help

Posted 19 March 2010 - 04:28 PM

When you say this:
printf("\nIn the function first number is %d\nIn the function second number is %d\n", x, y );

you are printing x and y, which are pointers. Therefore addresses are what's getting printed, rather than the values stored at those addresses. If you print *x and *y instead, it will work.

This post has been edited by hlemke: 19 March 2010 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1