首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >函数调用中初始化为参数的Struct成员char *

函数调用中初始化为参数的Struct成员char *
EN

Stack Overflow用户
提问于 2015-08-26 05:37:44
回答 3查看 57关注 0票数 0

这更多的是出于好奇,而不是我希望或需要它的工作方式。但当我做一些模特儿想测试一些东西的时候,我最终得到了这样的结果.不知道为什么不像我预料的那样起作用。

代码语言:javascript
运行
复制
typedef struct {
    char *a;
    char *b;
    char *c;
}mystruct;

void init_chars (char *arg)
{
    arg = malloc (sizeof (char)*10);
    arg = "0123456789";
    printf ("%s\n", arg);
}

int main ()
{
    mystruct *msp = malloc (sizeof (mystruct));
    init_chars (msp->a);
    init_chars (msp->b);
    init_chars (msp->c);
    printf ("%s, %s, %s\n", msp->a, msp->b, msp->c);
    return 0;
}

指纹..。

代码语言:javascript
运行
复制
0123456789

0123456789

0123456789

(null),(null),(null)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-26 05:40:08

当将值传递给函数参数时,C中有两件事

  1. 按值传递
  2. 参照通过

您正在按值传递,因此您将一些未初始化的值传递给该函数,并在函数之外不可见的函数中初始化它,您正在尝试在main()中使用未初始化的值,这将导致未定义的行为。

代码语言:javascript
运行
复制
arg = "0123456789";

如果要将字符串复制到某个内存位置,则需要memcpy()strcpy()

注意:使用未初始化的值会导致未定义的行为。

票数 0
EN

Stack Overflow用户

发布于 2015-08-26 05:50:54

您的代码有几个问题。这是固定代码:

代码语言:javascript
运行
复制
/* Don't forget to include <stdio.h>, <stdlib.h> and <string.h> */

typedef struct {
    char *a;
    char *b;
    char *c;
}mystruct;

void init_chars (char **arg) /* This should be char** as the address of a char* is passed */
{
    *arg = malloc ( /*sizeof(char)* */ 11); /* sizeof(char) is 1 always. You don't need it */
                                            /* Note the change of `arg` to `*arg` too */
                                            /* And that I've used 11 and not 10 because there needs to be space for the NUL-terminator */

    if(*arg == NULL) /* If the above malloc failed */
    {
        printf("Oops! malloc for *arg failed!");
        exit(-1);
    }

    //arg = "0123456789"; /* You just lost the allocated memory as you make the pointer point to a string literal */

    strcpy(*arg, "0123456789"); /* Use strcpy instead */
    printf ("%s\n", *arg);      /* *arg here */
}

int main ()
{
    mystruct *msp = malloc (sizeof (mystruct));

    if(msp == NULL) /* If the above malloc failed */
    {
        printf("Oops! malloc for msp failed!");
        return -1;
    }

    init_chars (&(msp->a));
    init_chars (&(msp->b)); 
    init_chars (&(msp->c));  /* Pass address of variables rather than their value */

    printf ("%s, %s, %s\n", msp->a, msp->b, msp->c);

    free(msp->a);
    free(msp->b);
    free(msp->c);

    free(msp);    /* Free everything after use */

    return 0;
}
票数 0
EN

Stack Overflow用户

发布于 2015-08-26 05:56:50

代码语言:javascript
运行
复制
typedef struct {
    char *a;
    char *b;
    char *c;
}mystruct;

void init_chars (char **arg)
{
    *arg = malloc (sizeof (char)*11);
    if(*arg == NULL)
    {
        printf("malloc failed!");
        exit(-1);
    }
    strcpy(*arg,"0123456789");
    printf ("%s\n", *arg);
}

int main ()
{
    mystruct *msp = malloc (sizeof (mystruct));
    if(msp == NULL)
    {
        printf("malloc failed");
        exit(-1);
    }
    init_chars (&msp->a);
    init_chars (&msp->b);
    init_chars (&msp->c);
    printf ("%s, %s, %s\n", msp->a, msp->b, msp->c);
    free(msp->a);
    free(msp->b);
    free(msp->c);
    free(msp)
    return 0;
}

产出:

0123456789

0123456789

0123456789

0123456789,0123456789,0123456789

与您的代码进行比较,您将了解为什么

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32218726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档