C语言 在 C 中传递字符指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14331411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:02:40  来源:igfitidea点击:

Passing char pointer in C

cpointerschar

提问by Sarp Kaya

Okay so I am trying to pass a char pointer to another function. I can do this with an array of a char but cannot do with a char pointer. Problem is I don't know the size of it so I cannot declare anything about the size within the main()function.

好的,所以我正在尝试将字符指针传递给另一个函数。我可以用一个字符数组来做到这一点,但不能用一个字符指针来做到这一点。问题是我不知道它的大小,所以我不能在main()函数内声明任何关于大小的信息。

#include <stdio.h>

void ptrch ( char * point) {
    point = "asd";
}

int main() {
    char * point;
    ptrch(point);
    printf("%s\n", point);
    return 0;
}

This does not work however, these two works:

但是,这不起作用,这两个有效:

1)

1)

#include <stdio.h>

int main() {
    char * point;
    point = "asd";
    printf("%s\n", point);
    return 0;
}

2)

2)

#include <stdio.h>
#include <string.h>

void ptrch ( char * point) {
    strcpy(point, "asd");
}

int main() {
    char point[10];
    ptrch(point);
    printf("%s\n", point);
    return 0;
}

So I am trying to understand the reason and a possible solution for my problem

所以我试图了解我的问题的原因和可能的解决方案

采纳答案by Karthik T

void ptrch ( char * point) {
    point = "asd";
}

Your pointer is passed by value, and this code copies, then overwrites the copy. So the original pointer is untouched.

您的指针按 value 传递,此代码复制,然后覆盖 copy。所以原始指针没有受到影响。

P.S. Point to be noted that when you do point = "blah"you are creating a string literal, and any attempt to modifyis Undefined behaviour, so it should really be const char *

PS 要注意的是,当你point = "blah"创建一个字符串文字时,任何修改的尝试都是未定义的行为,所以它真的应该是const char *

To Fix- pass a pointer to a pointeras @Hassan TM does, or return the pointeras below.

修复-像@Hassan TM 一样传递一个指向指针的指针,或者如下返回指针

const char *ptrch () {
    return "asd";
}

...
const char* point = ptrch();

回答by hmatar

This should work since pointer to the char pointer is passed. Therefore any changes to the pointer will be seen outside thereafter.

这应该工作,因为指向 char 指针的指针被传递。因此,此后将在外部看到对指针的任何更改。

void ptrch ( char ** point) {
    *point = "asd";
}

int main() {
    char * point;
    ptrch(&point);
    printf("%s\n", point);
    return 0;
}

回答by Ricky Stewart

Here:

这里:

int main() { char * point; ptrch(point);

int main() { char * point; ptrch(point);

You're passing pointby value. Then, ptrchsets its own local copy of pointto point to "asd", leaving the pointin mainuntouched.

你是point按值传递的。然后,ptrch将其自己的本地副本设置point为指向"asd",而保持pointinmain不变。

A solution would be to pass a pointer to main's point:

一个解决方案是将指针传递给main's point

void ptrch(char **pp) { *pp = "asd"; return; }

void ptrch(char **pp) { *pp = "asd"; return; }

回答by KamikazeCZ

If you change the value of the pointer in a function, it will remain changed only in that one function call. Don't mess your head with pointers and try:

如果你改变了一个函数中指针的值,它只会在那个函数调用中保持改变。不要用指针弄乱你的头,然后尝试:

void func(int i){
  i=5;
}
int main(){
  int i=0;
  func(i);
  printf("%d\n",i);
  return 0;
}

The same with your pointer. You do notchange the address it points to.

与您的指针相同。您不会更改它指向的地址。

If you assign to a variable passed by value, the variable outside the function will remain unchanged. You could pass it by a pointer (to pointer) and change it by dereferrencing it and it's the same with an int - in this case, it doesn't matter if the type is int or char * .

如果给一个按值传递的变量赋值,函数外的变量将保持不变。您可以通过一个指针(到 pointer)传递它并通过取消引用它来更改它,它与 int 相同 - 在这种情况下,类型是 int 还是 char * 并不重要。

回答by shoaib soudagar

first declare funtion......like this

首先声明函数......像这样

 #include<stdio.h>
 void function_call(char s)

next write main code.....

接下来写主代码.....

void main()
{
    void (*pnt)(char);  //  pointer function declaration....
    pnt=&function_call;  //    assign address of function
    (*pnt)('b');   //   call funtion....
}