如何使用预制数据初始化指向指针的指针?

发布于 2024-12-29 08:05:35 字数 90 浏览 4 评论 0原文

我有一个指向指针的指针,因为我无法将动态数组传递给函数。但是,如果我想使用预制数据初始化该指针到指针,那么如何设置它,因为数组的 {a,b,c} 表示法不适用于指针?

I have a pointer to a pointer, since I can't pass dynamic arrays to functions. However, if I want to initialize that pointer-to-pointer with premade data, how can I set it since {a,b,c} notation for arrays won't work for pointers?

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

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

发布评论

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

评论(3

白日梦 2025-01-05 08:05:35

您可以这样做:

static int row1[] = {1, 2, 3};
static int row2[] = {4, 5, 6, 7};
static int row3[] = {8, 9, 10, 11, 12, 13};
static int *pptr[] = {row1, row2, row3};

此时,可以将 pptr 分配给 int**

int **p = pptr;

You can do this:

static int row1[] = {1, 2, 3};
static int row2[] = {4, 5, 6, 7};
static int row3[] = {8, 9, 10, 11, 12, 13};
static int *pptr[] = {row1, row2, row3};

At this point, pptr can be assigned to an int**:

int **p = pptr;
看轻我的陪伴 2025-01-05 08:05:35

[这个答案仅在您需要双*时才相关。您的问题已被编辑为指向指针的指针 - 如果这就是您所需要的,则此答案不相关。]

您可以这样做:

double fValues[3] = { 1, 2, 3 };

变量 fValues 已经是一个指针 - 不带 [] 的数组变量是指向数组第一个元素的指针。这不是动态数组,因此您不需要分配/释放其内存。

假设您的函数采用双指针,如下所示:

void Func(double* pDbl) {...}

您可以这样调用它:

Func(fValues);代码>

[This answer is only relevant if you need a double*. Your question was edited to say pointer to pointer - if that's what you need, this answer is not relevant.]

You can do this instead:

double fValues[3] = { 1, 2, 3 };

The variable fValues is already a pointer - array variables without the [] are pointers to the first element of the array. This is not a dynamic array, so you don't need to allocate/free its memory.

Assuming your function that takes a double pointer looks something like this:

void Func(double* pDbl) {...}

you'd call it like this:

Func(fValues);

待天淡蓝洁白时 2025-01-05 08:05:35

您可以递归地创建小型动态数组,如下所示:

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

typedef struct
{
  int* pValues;
  size_t Count;
} List;

const List ListEnd = { NULL, 0 };

List NewList(int Value, List SubList)
{
  List l;

  l.Count = SubList.Count + 1;

  if (SubList.Count == 0)
  {
    l.pValues = malloc(sizeof(int));
  }
  else
  {
    l.pValues = realloc(SubList.pValues, l.Count * sizeof(int));
  }

  if (l.pValues == NULL)
  {
    // do proper error handling here
    abort();
  }

  // moving data isn't necessary if the list elements are
  // in the reverse order
  memmove(&l.pValues[1], &l.pValues[0], SubList.Count * sizeof(int));

  l.pValues[0] = Value;

  return l;
}

void PrintDynArr(int* pValues, size_t Count)
{
  while (Count--)
  {
    printf("%d\n", *pValues++);
  }
}

int main(void)
{
  int* p;

  PrintDynArr(p = NewList(1,
                  NewList(2,
                  NewList(3,
                  NewList(4, ListEnd)))).pValues,
              4);

  free(p);

  return 0;
}

输出:

1
2
3
4

You can create small dynamic arrays recursively something like this:

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

typedef struct
{
  int* pValues;
  size_t Count;
} List;

const List ListEnd = { NULL, 0 };

List NewList(int Value, List SubList)
{
  List l;

  l.Count = SubList.Count + 1;

  if (SubList.Count == 0)
  {
    l.pValues = malloc(sizeof(int));
  }
  else
  {
    l.pValues = realloc(SubList.pValues, l.Count * sizeof(int));
  }

  if (l.pValues == NULL)
  {
    // do proper error handling here
    abort();
  }

  // moving data isn't necessary if the list elements are
  // in the reverse order
  memmove(&l.pValues[1], &l.pValues[0], SubList.Count * sizeof(int));

  l.pValues[0] = Value;

  return l;
}

void PrintDynArr(int* pValues, size_t Count)
{
  while (Count--)
  {
    printf("%d\n", *pValues++);
  }
}

int main(void)
{
  int* p;

  PrintDynArr(p = NewList(1,
                  NewList(2,
                  NewList(3,
                  NewList(4, ListEnd)))).pValues,
              4);

  free(p);

  return 0;
}

Output:

1
2
3
4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文