Jump to content

Cryptic instructions!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
arctic_blizzard

arctic_blizzard

    Newbie

  • Members
  • Pip
  • 9 posts
Hello all,

I have a homework assignment in my C programming class that I do not understand.

We had to take a previous program that swapped the contents of two memory locations using a function. However, the professor has asked us now to modify our program so that the swapping performed by the function will appear in the main program after calling the function.

These were his exact words, and I have no idea what he is trying to get at.

The professor as usual answers NO questions, and I think I am just as confused as the rest of my classmates.

We are currently covering pointers if that helps any.

Here is my original program.


#include <stdio.h>


void main()

{

	void swap(int x, int y);


	int first_num = 10;

	int second_num = 50;


	printf("\nThe value of first_num is %d \n The value of second num is %d \n",  

                           first_num,  second_num );


	swap(first_num, second_num);


	printf("\nThe value of first_num is %d \n The value of second num is %d \n",  

                           first_num,  second_num );

                           

 system("pause");

}


void swap(int x, int y)

{

	int local_int;


	local_int = x;

	x = y;

	y = local_int;


	printf("\nIn the function first_num is %d\nIn the function second_num is ", x, 

                      y );

}


Thanks in advance for all of your help!

Cheers

~AB

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
As this appears to be C, I'll tailor my reply in that manner.

C is pass-by-value. As you see, your current program swaps the copies in the called function but the originals remain the same in the calling function. You can simulate pass-by-reference by using pointers. The result is that the values will be changed in the calling function as well.

#3
arctic_blizzard

arctic_blizzard

    Newbie

  • Members
  • Pip
  • 9 posts
Ah, OK

byRef, not byVal

so, I would need to use pointers in my swap function to directly change the original values?

Thanks

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Yes. This amounts to several changes in the swap function -- both the prototype and the swap code -- and in the called function. The prototype must receive pointers and the called function must pass them.

#5
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
In C, since you can't actually pass in a reference as you can in C++, you need to pass in a pointer to the memory location you want to modify. As DCS pointed out, C is pass by value, so passing in a regular ol' variable will not do anything at all!!

So do something like:
void swap(int* x, int* y)
{
   // ... Your code here.
}

Make sure to dereference your variables to modify them.

#6
arctic_blizzard

arctic_blizzard

    Newbie

  • Members
  • Pip
  • 9 posts
Dereference is the addition of the & sign to variables- correct?

Would I add dereference only to the variables in my swap function, or also to main?

#7
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
To dereference a pointer...

First you declare the pointer, such as int* i; DO NOT dereference this immediately as this is dereferencing a NULL pointer and will cause your program to CRASH. Once you assign a value to i, you can then dereference it, by saying something like int temp = *i;

The & operator gives the memory address of a variable, whereas a pointer points to a memory address. Dereferencing a pointer will modify the data at the memory that it points to. Get it?

#8
arctic_blizzard

arctic_blizzard

    Newbie

  • Members
  • Pip
  • 9 posts
lol, I guess I got a little over zealous with the *'s, and I was wondering why it was crashing, thanks for the explanation!

#9
arctic_blizzard

arctic_blizzard

    Newbie

  • Members
  • Pip
  • 9 posts
Problems fixed. Check out this code...


#include <stdio.h>

void swap(int *x, int *y);

void main()

{


	int first_num = 10;

	int second_num = 50;


	printf("\n\nThe value of first_num is %d \n The value of second num is %d \n",  

                           first_num,  second_num );


	swap(&first_num, &second_num);


	printf("\n\nThe value of first_num is %d \n The value of second num is %d \n",  

                           first_num,  second_num );

                           

 system("pause");

}


void swap(int *x, int *y)

{

	int local_int;


	local_int = *x;

	*x = *y;

	*y = local_int;


	printf("\n\nIn the function first_num is %d\nIn the function second_num is ", *x, 

                      *y );

}


Thank you all for your help!

~AB

#10
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Looks perfect, good job. At least some people look up their own questions and find answers on their own, and learn something...