Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

got a question about pointer and address operator on scanf()

Status
Not open for further replies.

meatmeatof

Technical User
Jul 9, 2007
73
US
program 1
int main() {
int a = 2;
int b = 3;
swap (&a, &b); //extract the address of a and b
return 0;
}
void swap(int* a, int*b) {
*a = *a + 2; //dereference it and assign new value to a.
*b = *b + 2;
}

In program 1, we need to use *.

Program 2
/*This program adds two integers read from the keyboard and prints the results
written by : xxx
Date : xxx
*/
#include <stdio.h>

//Function Declaration
void getData(int* a, int* b);
int main(void){
//Local Declaration
int a;
int b;

getData(&a, &b);
printf("**Main:\t\ta = %d;\tb = %d\n", a, b);


system("PAUSE");
return 0;
}

/*---------------getData()----------------------------------------
This function reads two integers from the keyboard.
pre Parameters a and b are addersses, it stores address
post Data read into parameter address
------------------------------------------------------------------*/
void getData(int* a, int* b) {
printf("Please enter two integer number: ");
scanf("%d %d", a,b);
printf("**getData:\ta = %d;\tb = %d\n", *a, *b);
return;
}
Please enter two integer number: 333 22
**getData: a = 333; b = 22
**Main: a = 333; b = 22

In program 2, the scanf function didn't use any & or * on variable a and b. why?? how does scanf directly access a and b in the calling function? The textbook said if you want modify the content of a variable in calling function, you need to use their address.
This point confuse me. I don't understand why scanf didn't use any address point or dereference thing.
I am so lost.
 
The book explain: "there's no address operators for the variable. we don't need address operator because the parameters are already addresses. we have used the address operator (&) up to this example to tell the compiler that we want the address of the variable, not its content. since the parameter were passed as address, we don't need the address operator."
i am lost here.

what it said is that if we don't need address operator in scanf.
why do we need address operator or dereference (*) in swap()????? he said parameter is already address variable. i am so lost. what so special about scanf function comparing to regular expression that needs * or & to access the variable in calling function??
 
how do i edit my second post? i type little bit fast and it has bad grammar. sorry.
 
i think i got it :"(
= =""'
how do i delete this post??? my fault.
 
Swap means a <=> b (exchange of values). Think about another name for your function. See the function body in C with a wonderful += operator:
Code:
void addTwos(int* a, int* b)
{
  *a += 2;
  *b += 2;
}
The scanf library function wants pointer (address of) arguments (input targets). The printf function wants values, not pointers. That's why you need operator & in this snippet:
Code:
int a;
scanf("%d",&a);
Now the second program case:
Code:
void getA(int* a)
{
  scanf("%d",a); /* parameter a IS a pointer here */
  printf("%d\n",*a); /* we want a value, use dereferencing */
}
Don't forget: it's no matter what's a name you have selected for your variable in the main and for parameter in another function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top