You are here:

C/Call by Reference

Advertisement


Question
* Using pointers, make a program that reads two integers, then calls the swap function to swap them.

I made my own code and here it goes: (please correct my mistakes)

#include<stdio.h>
int swap(int a, int z);
int main(void)

{
  int a, z;
  printf("Enter the first number\n");
  scanf("%d", &a);
  printf("Enter the second number\n");
  scanf("%d", &z);

  printf("The numbers you entered were %d and %d\n", a, z);
  nelson(&a, &z);
  printf("When swapped they will be %d and %d\n", a, z);
}

int swap(int a, int z)
{
  int temp;
  temp = *a;
  *a = *z;
  *z = temp;

  return 0;
}

P.S. I haven't tried this one out yet since our school is the only one in our place which provides Linux OS. Furthermore, its Saturday today so there's no class. Please do help me out I'm due on Monday - it's required for me to finish the semester. And if you don't mind, would you help me out by giving me some tips on how to solve the problem below? But if not, it's still fine.

Thanks a million times! =)


* Write and test two functions:

double     get_double(void);
int        get_int(void);

which loop, prompting the user, until a valid double/valid integer are entered.

Answer
I will not be able to give solution, if I am not able to understand what is the problem.

You have given a piece of code, which you haven't yet tried.
So, you also don't know what is the problem.
First of all try to find out what is the problem and whether you are able to correct them.
If not, then explain your problem and I will try to look into it.

And you are asking me to write some test functions.
I don't want to take up assignments like this.
I don't have to time to invest in such things.
It is left to you to try and I am always here to try to help you, in case you run into problem.

But remember, you must clearly tell what problem you are facing.
What inputs you tried and what is the (correct) expected output.
If the input is a data file, then post that also.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.