简体   繁体   English

结构成员char *在函数调用中初始化为参数

[英]Struct member char * initialized in a function call as a parameter

This is more out of curiosity, and is not how I want or need it to work. 这更多是出于好奇,而不是我想要或需要它的方式。 But when I was doing some mockups and wanted to test something out, I ended up with something like this... and was wondering why it doesn't work as I expected. 但是,当我做一些样机并想测试某些东西时,我最终得到了这样的东西……并且想知道为什么它不能按我预期的那样工作。

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

Prints... 打印...

0123456789

0123456789

0123456789

(null),(null),(null)

There are 2 things in C when it comes to passing the value to function parameters 将值传递给函数参数时,C中有两件事

  1. Pass by value 传递价值
  2. Pass by reference 通过参考

You are doing pass by value so you are passing some unintiialized value to the function and initializing it in the function which is not visible outside the function, you are trying to use uninitialized values in main() which will lead to undefined behavior. 您正在按值传递,因此您要将一些未初始化的值传递给该函数,并在该函数外部看不见的函数中对其进行初始化,因此您试图在main()使用未初始化的值,这将导致未定义的行为。

arg = "0123456789";

If you want to copy a string to some memory location then you need memcpy() or strcpy() 如果要将字符串复制到某个内存位置,则需要memcpy()strcpy()

Note: Using uninitialized values leads to undefined behavior. 注意:使用未初始化的值会导致未定义的行为。

Your code has several problems. 您的代码有几个问题。 Here is the fixed code: 这是固定代码:

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

output: 输出:

0123456789 0123456789

0123456789 0123456789

0123456789 0123456789

0123456789, 0123456789, 0123456789 0123456789、0123456789、0123456789

compare with your code u will understand why 与您的代码进行比较,您将理解为什么

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

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