在指针C(传递地址转换成函数) [英] Pointers in C (Passing addresses into function)

查看:220
本文介绍了在指针C(传递地址转换成函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有这个问题一条缝。这个问题说:*swap_nums似乎工作,但不swap_pointers修复它。*我的方式初学者:)

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 :)

我相信我可以工作,解掉自己,但麻烦的是我有一点很难理解在某些C.编程概念在这里,我已经证明给定的code,需要编辑。下面,我将展示我的思维过程至今。请注意:我想一些提示不完整的解决方案吧。 : - )

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; }

我的思维过程:这是我相信会理应交换整型变量a和b的程序。那么这将需要两个声明字符串和交换他们。

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.

主要功能:

int a = 3, b = 4;

分配变量a和b分别为3和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);

该函数swap_nums正在发生。我会去,现在解释我的思考过程。

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?

我看到它的方式,我们传递的a和b的地址由符号到函数swap_nums所示。但是怎么来的,我们是int * x和INT * Y?我有点糊涂了这里。可能有人对我来说这有点解释?

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?

推荐答案

在C,一切都为值,其中包括指针传递。因此,您的code的指针,交换指针操纵复印件,原件留不变。

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

为了要交换的指针,你需要传递的指针的指针,而不是简单的指针。当然,里面的功能,你需要解引用您在 swap_numbers做了同样的方法添加水平

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;
}

这篇关于在指针C(传递地址转换成函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆