通过函数传递动态数组结构

发布于 2024-09-30 20:46:26 字数 478 浏览 3 评论 0原文

struct aPoint {
        int somaVertical;
        int somaHorizontal;
        int valor;
};

我在 main() 中动态创建了一个结构数组,如下所示:

struct aPoint *ps = malloc( sizeof(struct aPoint) * columns * rows )

并且我想在具有 sscanf()结构值代码>.数组的初始化也在 main() 上处理

如何通过该函数传递结构数组并设置它的一些结构值?唉,我讨厌指点

谢谢!

struct aPoint {
        int somaVertical;
        int somaHorizontal;
        int valor;
};

I have an array of structs dynamically created in main(), like so:

struct aPoint *ps = malloc( sizeof(struct aPoint) * columns * rows )

And I want to work with its struct values outside of main() in a function that has sscanf(). The initialization of the array is also taken care of on the main().

How can I pass the array of structs through that function and set some struct values of it aswell? Argh I hate pointering!

Thanks!

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

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

发布评论

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

评论(4

生生漫 2024-10-07 20:46:26

那将是:

    int readStuff(struct aPoint *ps, size_t len, const char *someVar)
    {
        unsigned int i;
        for (i = 0; i < len; i++) {
           sscanf(someVar, "%d", &(ps[i].somaVertical));
           /* And so on for the other fields */
        }
        /* Return whatever you're returning here */
    }

    const size_t len = colunas * linhas;
    struct aPoint *ps = calloc(len, sizeof(struct aPoint));
    int success = readStuff(ps, len, arrayOfNumbers);

That would be:

    int readStuff(struct aPoint *ps, size_t len, const char *someVar)
    {
        unsigned int i;
        for (i = 0; i < len; i++) {
           sscanf(someVar, "%d", &(ps[i].somaVertical));
           /* And so on for the other fields */
        }
        /* Return whatever you're returning here */
    }

    const size_t len = colunas * linhas;
    struct aPoint *ps = calloc(len, sizeof(struct aPoint));
    int success = readStuff(ps, len, arrayOfNumbers);
薄情伤 2024-10-07 20:46:26

这对我有用

/* #include <assert.h> */
#include <stdio.h>
#include <stdlib.h>

struct aPoint {
  int somaVertical;
  int somaHorizontal;
  int valor;
};

int readStuff(struct aPoint *data, int rows, int cols) {
  sscanf("42", "%d", &data[3].somaVertical);
  sscanf("142", "%d", &data[3].somaHorizontal);
  sscanf("-42", "%d", &data[3].valor);
  return 0;
}

int main(void) {
  struct aPoint *ps;
  int colunas, linhas;

  colunas = 80;
  linhas = 25;
  ps = malloc(sizeof *ps * colunas * linhas);
  /* assert(ps); */ /* thanks Tim */
  if (ps) {
    readStuff(ps, linhas, colunas);
    printf("%d %d %d\n", ps[3].somaVertical, ps[3].somaHorizontal, ps[3].valor);
    free(ps);
  } else {
    fprintf(stderr, "no memory.\n");
    exit(EXIT_FAILURE);
  }
  return 0;
}

This works for me

/* #include <assert.h> */
#include <stdio.h>
#include <stdlib.h>

struct aPoint {
  int somaVertical;
  int somaHorizontal;
  int valor;
};

int readStuff(struct aPoint *data, int rows, int cols) {
  sscanf("42", "%d", &data[3].somaVertical);
  sscanf("142", "%d", &data[3].somaHorizontal);
  sscanf("-42", "%d", &data[3].valor);
  return 0;
}

int main(void) {
  struct aPoint *ps;
  int colunas, linhas;

  colunas = 80;
  linhas = 25;
  ps = malloc(sizeof *ps * colunas * linhas);
  /* assert(ps); */ /* thanks Tim */
  if (ps) {
    readStuff(ps, linhas, colunas);
    printf("%d %d %d\n", ps[3].somaVertical, ps[3].somaHorizontal, ps[3].valor);
    free(ps);
  } else {
    fprintf(stderr, "no memory.\n");
    exit(EXIT_FAILURE);
  }
  return 0;
}
倚栏听风 2024-10-07 20:46:26

我认为你需要

readStuff(ps); 
...
sscanf(someVar, "%d", &(ps[index].valor)); // using index in readStuff

或者

readStuff(ps + index); // using index in main
...
sscanf(someVar, "%d", &(ps->valor)); // or &ps[0].valor, that's equivalent 

I think you need either

readStuff(ps); 
...
sscanf(someVar, "%d", &(ps[index].valor)); // using index in readStuff

or

readStuff(ps + index); // using index in main
...
sscanf(someVar, "%d", &(ps->valor)); // or &ps[0].valor, that's equivalent 
桜花祭 2024-10-07 20:46:26

C 中的所有函数都按值传递参数,因此您可以将指针传递给要修改的结构数组:

int readStuff(struct aPoint *p, int numStruct)
{
   ...
   for(i=0; i<numStruct; i++)
   {
      sscanf(someVar, "%d", &(*(p+i).valor) );
   }
   ...
}

您可以使用以下方式调用此函数:

struct aPoint *ps = malloc( sizeof(struct aPoint) * columns * rows );
...
readStuff(ps, columns * rows);

All functions in C are passed arguments by value, so you can pass a pointer to the struct array you wish to modify:

int readStuff(struct aPoint *p, int numStruct)
{
   ...
   for(i=0; i<numStruct; i++)
   {
      sscanf(someVar, "%d", &(*(p+i).valor) );
   }
   ...
}

You can call this function with:

struct aPoint *ps = malloc( sizeof(struct aPoint) * columns * rows );
...
readStuff(ps, columns * rows);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文