简体   繁体   English

C中的指针(将地址传递给函数)

[英]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 :) 问题是,*“swap_nums似乎有效,但不是swap_pointers。修复它。”*顺便说一下,我是初学者:)

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. 我相信我可以自己解决这个问题但问题是我在理解C中的一些编程概念时遇到了一些困难。这里我展示了需要编辑的给定代码。 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. 我的思考过程:这是一个我认为应该交换整数变量a和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. 将变量a和b分别分配给3和4。

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. 函数swap_nums正在发生。 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. 我看到它的方式,我们将&符号指示的a和b的地址传递给函数swap_nums。 But how come the we have int *x and int *y? 但是为什么我们有int * x和int * y? I'm a bit confused here. 我在这里有点困惑。 Could someone explain to me this bit? 有人可以向我解释这一点吗?

In C, everything is passed as values, including pointers. 在C中,所有内容都作为值传递,包括指针。 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 : 当然,在函数内部,你需要添加一个取消引用级别,就像你在swap_numbers

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

A pointer may be an address, but it is still passed by value to functions (like everything in C). 指针可以是一个地址,但它仍然通过值传递给函数(就像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;
}

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"? 什么是s1的类型,什么是“我应该打印第二”? And how do you save a string literal to a variable? 如何将字符串文字保存到变量?

提示:按值学习调用并通过引用调用以清楚地了解您的查询。

将指针视为另一个变量,我们必须将指向此变量的指针传递给函数swap_pointers()

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM