简体   繁体   English

在C中传递char指针

[英]Passing char pointer in C

Okay so I am trying to pass a char pointer to another function. 好吧所以我试图将char指针传递给另一个函数。 I can do this with an array of a char but cannot do with a char pointer. 我可以使用char数组来完成此操作,但不能使用char指针。 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 所以我试图理解我的问题的原因和可能的解决方案

This should work since pointer to the char pointer is passed. 这应该工作,因为传递指向char指针的指针。 Therefore any changes to the pointer will be seen outside thereafter. 因此,之后将在外部看到对指针的任何改变。

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

int main() {
    char * point;
    ptrch(&point);
    printf("%s\n", point);
    return 0;
}
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. 所以原始指针是不变的。

PS Point to be noted that when you do point = "blah" you are creating a string literal, and any attempt to modify is Undefined behaviour , so it should really be const char * PS Point要注意的是,当你执行point = "blah"你正在创建一个字符串文字,并且任何修改的尝试都是未定义的行为 ,所以它应该是const char *

To Fix - pass a pointer to a pointer as @Hassan TM does, or return the pointer as below. 修复 - 将指针传递给指针,如@Hassan TM所做,或返回如下指针

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

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

Here: 这里:

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

You're passing point by value. 你是逐point传递的。 Then, ptrch sets its own local copy of point to point to "asd" , leaving the point in main untouched. 然后, ptrch设置它自己的point的本地副本point "asd" ,保持mainpoint不变。

A solution would be to pass a pointer to main 's point : 一个解决方案是传递一个指向main point的指针:

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

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 not change 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 * . 你可以通过指针( 指针 )传递它并通过dereferrencing它来改变它,它与int相同 - 在这种情况下,如果类型是int或char *则无关紧要。

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

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

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