Reputation: 22064
void swap(char *a,char *b){
char t;
t = *a;
*a = *b;
*b = t;
}
int main(void){
char a = '1';
char b = '2';
swap(&a,&b);
printf("The value is %c and %c respectively\n",a,b);
return 0;
}
in the above code, there's a spot that confuse me
I think if a
is a pointer, and *a
is the value it points to
int *ptr, a = 1;
ptr = &a;
printf("The value of *ptr should be a: %d\n",*ptr);
printf("The value of *a should be an hex address: %p\n",ptr);
so in the swap(char *a, char *b)
function,it takes the value not pointer( *a not a),
swap(&a, &b)
but it actually pass the pointer value to it as the parameter, and the code works. Anybody can explain it to me?(I think for swap(char *a){...}
part, the declaration doesn't mean it require *a to pass in, it means declare a pointer value a
, not the value a points to as *a
in elsewhere means).
Upvotes: 0
Views: 109
Reputation: 1
In this our aim is to swap the value
when you write char a='1'; i.e you are putting '1' at address 662442(suppose &a=662442). char b='2'; i.e you are putting '2' at address 662342(suppose &b=662342).
now consider swapping a and b,here our aim is to change value at 662442(&a) to value at 662342(&b) and value at 662342(&b) to value at 662442(&a).
now for swapping we take a temporary variable char temp and will do the following
temp=*a; i.e assigning value at address 662442(&a) to temp.
*a = *b; i.e assigning value at address 662342(&b) to value at address 662442(&a).
now we will put previous value of a (i.e value kept in variable temp) at address 662342(&b)
*b = temp; i.e assigning temp to address 662442(&a)
Upvotes: 0
Reputation: 59617
Yes, the asterisk *
has multiple meanings related to pointers:
It be used in a declaration to introduce a variable that contains an address - a pointer:
int *ptr = NULL;
Similarly in an argument list, such as void swap(char *, char *)
.
It can also be used to dereference an existing pointer, or get the value pointed to, as within the function swap
:
t = *a;
This generally causes a good deal of confusion for students who are new to C, but it's actually quite simple.
Upvotes: 0
Reputation: 19037
*
is confusing because it means two different, but closely related, things. In a variable declaration, *
means "pointer". In an expression, *
means "dereference the pointer".
It's intended to be a helpful mnemonic: if you have char *a
in your code it means that *a
is a char
.
Upvotes: 2
Reputation: 150108
Your function
swap(char *a, char *b)
takes two parameters, both of which are of type char *
. Quite literally that means they point to a character somewhere in memory.
When you dereference the pointer
t = *a;
You are saying "grab whatever a
is pointing to and put it in t
.
Perhaps the confusion is from the fact that *
means two related but different things. In the case char *
, it's defining a type, specifically one that points to a character somewhere in memory. In the case *a
, the *
means "look at the character being pointed to by a and let me know what it is".
Upvotes: 2
Reputation: 88378
In main
:
a
is a char
&a
is a pointer to the char a
.In swap
:
a
is a pointer to a char*a
is the char pointed to by a
.You passed a pointer to a pointer, and it worked beautifully. Try drawing a picture with boxes and errors. Clears things up every time. Just remember your code has two a
s and two b
s.
Upvotes: 0