Bobby
Bobby

Reputation: 223

Pointers in C (Passing addresses into function)

I'm trying have a crack at this problem. The question says, *"swap_nums seems to work, but not swap_pointers. Fix it."* I'm a beginner by the way :)

I believe I can work the solution out myself but the trouble is I have a bit of difficulty understanding some programming concepts in C. Here I have shown the given code that requires editing. Below that I will show my thought processes so far. Please note: I want some hints NOT the full solution please. :-)

#include <stdio.h>
#include <stdlib.h>

void swap_nums(int *x, int *y);
void swap_pointers (char *x, char *y);

int main (int argc, char *argv[]){

   int a = 3, b = 4;
   char *s1, *s2;
   swap_nums(&a, &b);
   printf("a is %d\n", a);
   printf("b is %d\n", b);

   s1 = "I should print second";
   s2 = "I should print first";

   swap_pointers(s1, s2);
   printf("s1 is %s\n", s1);
   printf("s2 is %s\n", s2);

   return EXIT_SUCCESS; } 

void swap_nums(int *x, int *y){

   int temp;
   temp = *x;
   *x = *y;
   *y = temp; }

void swap_pointers (char *x, char *y){

   char *temp;
   temp = x;
   x = y;
   y = temp; }

My thought process: This is a program that I believe will supposedly swap the integer variables a and b. It will then take the two declared strings and swap them as well.

Main function:

int a = 3, b = 4;

Assigns variable a and b to 3 and 4 respectively.

char *s1, *s2;

Creates a character pointer variable (which will hold the address of a character)

swap_nums(&a, &b);

The function swap_nums is taking place. I will go to it now to explain my thought processes.

void swap_nums(int *x, int *y){

So I'm not that familiar with passing things into functions, could someone correct what I am about to say if I am wrong here?

The way I see it, we are passing the addresses of a and b as indicated by the ampersand into the function swap_nums. But how come the we have int *x and int *y? I'm a bit confused here. Could someone explain to me this bit?

Upvotes: 2

Views: 6983

Answers (6)

roger_that
roger_that

Reputation: 9791

This is what you need i guess. Basic and sufficient.

void swap(char **s1, char **s2){
char *temp=*s1;
*s1=*s2;
*s2=temp;
}

int main(){
char *s1="second";
char *s2="first";
swap(&s1,&s2);
printf("%s",s1);
printf("%s",s2);
return 0;

}  

Hope this helps.

Upvotes: 0

amulous
amulous

Reputation: 732

Thinking of a pointer as just another variable, we have to pass a pointer to this variable to the function swap_pointers().

Upvotes: 0

pallavi
pallavi

Reputation: 114

Hint :Study call by value and call by reference to get a clear understanding of your query.

Upvotes: 0

user2134086
user2134086

Reputation:

A pointer may be an address, but it is still passed by value to functions (like everything in C). To swap two pointers, you need pointers to pointers.


Here's what you want:

#include <stdio.h>

int main(void)
{
    char x='s';
    char y='o';
    char *a = &x;
    char *b = &y;
    printf("a is %x\n",a);
    printf("b is %x\n",b);
    printf("swapping\n");
    swap_pointers(&a,&b);
    printf("a is %x\n",a);
    printf("b is %x\n",b);

}

void swap_pointers(char **a, char **b)
{
    char *temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

In C, everything is passed as values, including pointers. Therefore, your code that swaps pointers manipulates pointer copies, leaving originals unchanged.

In order to swap pointers, you need to pass pointers to pointers, not simply pointers. Of course inside the function you need to add a level of dereference the same way that you did in swap_numbers:

void swap_pointers (char **x, char **y) {
    char *temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

Upvotes: 8

jh314
jh314

Reputation: 27802

Here is a hint, there is something wrong with this:

s1 = "I should print second";
s2 = "I should print first";

What is s1's type, and what is "I should print second"? And how do you save a string literal to a variable?

Upvotes: 0

Related Questions