访问/修改结构中的字符串数组

发布于 2024-09-28 22:52:40 字数 232 浏览 2 评论 0原文

假设我有以下代码:

typedef struct
{
    char **p;
} STRUCT;

int main()
{
    STRUCT s;
    *(s.p) = "hello";
    printf("%s\n", *(s.p));

    return 0;
}

这显然不起作用,但它应该显示我想要做什么。我将如何初始化、访问、打印结构中的字符串数组等?

Suppose I have the following code:

typedef struct
{
    char **p;
} STRUCT;

int main()
{
    STRUCT s;
    *(s.p) = "hello";
    printf("%s\n", *(s.p));

    return 0;
}

which obviously doesn't work, but it should show what I want to do. How would I go about initialising, accessing, printing, etc the array of strings in the structure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风渺 2024-10-05 22:52:40

我想,你有两个 * 而你只需要一个。尝试:

typedef struct
{
    char *p;
} STRUCT;

int main()
{
    STRUCT s;
    s.p = "hello";
    printf("%s\n", s.p);

    return 0;
}

如果您确实想要双重间接寻址,则需要为要取消引用的指针分配一些空间。原始程序中的 *(sp) 取消引用未初始化的指针。在这种情况下:

typedef struct
{
    char **p;
} STRUCT;

int main()
{
    STRUCT s;
    s.p = malloc(sizeof(char *));
    *(s.p) = "hello";
    printf("%s\n", *(s.p));
    free(s.p);
    return 0;
}

第二个程序只为一个字符串指针分配空间;如果你想要一个数组,只需分配适当的空间即可。

You have two * where you want just one, I think. Try:

typedef struct
{
    char *p;
} STRUCT;

int main()
{
    STRUCT s;
    s.p = "hello";
    printf("%s\n", s.p);

    return 0;
}

If you do really want to have the double indirection, you need to allocate some space for the pointer you're dereferencing. *(s.p) in your original program dereferences an uninitialized pointer. In this case:

typedef struct
{
    char **p;
} STRUCT;

int main()
{
    STRUCT s;
    s.p = malloc(sizeof(char *));
    *(s.p) = "hello";
    printf("%s\n", *(s.p));
    free(s.p);
    return 0;
}

This second program allocates space for just one string pointer; if you want an array, just allocate the appropriate amount of space.

夜血缘 2024-10-05 22:52:40

目前没有数组,但我假设您想创建一个。您需要首先分配您想要的字符串数量的 char * :

int main()
{
    STRUCT s;
    int N = 10; // number of strings you want
    s.p = (char **)malloc(N * sizeof(char *));
    s.p[0] = "hello";
    s.p[1] = "world";
    ...
    printf("%s\n", s.p[0]);
    free(s.p);

    return 0;
}

There is no array at the moment, but I assume you want to create one. You need to first allocate as many char *s as you want strings:

int main()
{
    STRUCT s;
    int N = 10; // number of strings you want
    s.p = (char **)malloc(N * sizeof(char *));
    s.p[0] = "hello";
    s.p[1] = "world";
    ...
    printf("%s\n", s.p[0]);
    free(s.p);

    return 0;
}
誰認得朕 2024-10-05 22:52:40

您需要通过向结构添加计数成员或使用 NULL 标记值来了解数组中包含多少个字符串。以下示例使用 NULL 标记:

分配和初始化:

STRUCT s;
s.p = malloc(sizeof *s.p * (number_of_strings + 1));
if (s.p)
{
  size_t i;
  for (i = 0; i < number_of_strings; i++)
  {
    s.p[i] = malloc(length_of_ith_string + 1);
    if (s.p[i])
      strcpy(s.p[i], ith_string);
  }
  s.p[i] = NULL;
}

获取 number_of_stringslength_of_ith_stringith_string 的适当值。

访问/打印:

for (i = 0; s.p[i] != NULL; i++)
  printf("String %d: %s\n", i, s.p[i]);

解除分配:

for (i = 0; s.[i] != NULL; i++)
  free(s.p[i]);
free(s.p);

You're going to need to know how many strings are contained in the array, either by adding a count member to the struct or by using a NULL sentinel value. The following examples use the NULL sentinel:

Allocating and initializing:

STRUCT s;
s.p = malloc(sizeof *s.p * (number_of_strings + 1));
if (s.p)
{
  size_t i;
  for (i = 0; i < number_of_strings; i++)
  {
    s.p[i] = malloc(length_of_ith_string + 1);
    if (s.p[i])
      strcpy(s.p[i], ith_string);
  }
  s.p[i] = NULL;
}

for appropriate values of number_of_strings, length_of_ith_string, and ith_string.

Accessing/printing:

for (i = 0; s.p[i] != NULL; i++)
  printf("String %d: %s\n", i, s.p[i]);

Deallocating:

for (i = 0; s.[i] != NULL; i++)
  free(s.p[i]);
free(s.p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文