简体   繁体   English

c将结构传递给更新方法

[英]c pass struct to method for updating

I'm having some issues with the following code snippet: 以下代码段存在一些问题:

#include <stdio.h>

struct some_numbers 
{
int id;
char *somestring;
};

typedef struct some_numbers numb;

void print_numbers(numb *a)
{
printf("%d: %s\n", a->id, a->somestring);
}

void add_number(numb *a)
{

 // do someting magical
 // push the new result to the existing struct
 // put something into like:
  a->somestring[5] = "widdley";
}

int main(void)
{

// put some stuff in the struct
numb entries[50];
int x;
for(x=0; x < 4; x++)
{
    numb a = entries[x];
    a.id = x;
    a.somestring = "goats";
    print_numbers(&a);
}

add_numbers(&a);  // i want to call a method 

return 0;
}

I want to create an array of structs, pass the struct to a method, and pop more items into the array. 我想创建一个结构数组,将该结构传递给方法,然后将更多项目弹出该数组。 Everything I've tried thus far has failed miserably, and I'm having a hard time thinking my way out of this conundrum. 到目前为止,我尝试过的所有方法都失败了,我很难思考如何摆脱这个难题。 I can print the values without any issue: 我可以打印这些值而没有任何问题:

> ./struct 
0: goats
1: goats
2: goats
3: goats
> 

I would like the output to look like: 我希望输出看起来像:

> ./struct 
0: goats
1: goats
2: goats
3: goats
4: widdley
>

Please help. 请帮忙。 I'm not good at c, so be gentle! 我不擅长c,所以要温柔!

edit: clarified the code example to take the focus off of the wrong areas. 编辑:澄清了代码示例以将焦点从错误的区域移开。

Here: 这里:

a->somestring[5] = "widdley";

the type of somestring[5] is char , not char* . somestring[5]的类型是char ,而不是char* If you need a string array you need to define: 如果需要字符串数组,则需要定义:

struct some_numbers {
  int id;
  char *somestring[20];  // 20 is an example
};

And somehow manage these strings depending on your actual goal. 并根据您的实际目标以某种方式管理这些字符串。

If you want to add a new number into entries then define it with more than 4 items and keep track of valid locations: 如果要将新数字添加到entries ,请使用四个以上的entries进行定义,并跟踪有效位置:

numb entries[20]; // 20 is an example
int num_entries = 0;
entries[num_entries++] = new_entry(); // some function that returns an entry

or just use a dynamic array, which requires dynamic memory management (malloc/realloc); 或仅使用需要动态内存管理(malloc / realloc)的动态数组;

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
struct some_numbers 
{
  int id; 
  char *somestring;
};

typedef struct some_numbers numb;

void print_numbers(numb *a) {
  printf("%d: %s\n", a->id, a->somestring);
}

void add_entry(numb **list, int *n, int id, const char *str) {
  int cnt = *n; 
  *list = realloc(*list, sizeof(numb)*(cnt + 1));
  (*list)[cnt].id = id; 
  (*list)[cnt].somestring = malloc(strlen(str)+1);
  strcpy((*list)[cnt].somestring, str);
  *n = cnt + 1;
}

int main(void)
{

  // put some stuff in the struct
  numb *entries = 0;
  int x, num_entries=0;
  for(x=0; x < 4; x++)
  {
    add_entry(&entries, &num_entries, x, "goats");
  }

  for (x=0; x<num_entries; x++)
    print_numbers(&entries[x]);
  printf("\n\n");
  add_entry(&entries, &num_entries, 6, "widdley"); 
  for (x=0; x<num_entries; x++) 
    print_numbers(&entries[x]);

  return 0;
}

If you want to add more values to your array, you need to either allocate enough memory upfront to store the maximum possible number of structs, or allocate the memory dynamically. 如果要向数组添加更多值,则需要预先分配足够的内存以存储最大数量的结构,或者动态分配内存。 So: 所以:

numb entries[100];

Or 要么

numb *entries = malloc(sizeof(numb)*100);

Then, you'll need to pass a variable to the add_number function to keep track of where your array ends: 然后,您需要将变量传递给add_number函数以跟踪数组的结束位置:

void add_number(numb *a, int position) {
        a[position].somestring = "widdley";
}

You commented out the call to add_numbers() , so of course, the structs in your array won't change. 您已注释掉对add_numbers()的调用,因此,数组中的结构当然不会改变。 I suspect you did this because you are getting a compiler error. 我怀疑您这样做是因为您遇到了编译器错误。 a->somestring[5] = "widdley"; should be a->somestring = "widdley"; 应该是a->somestring = "widdley"; since you want to set the value of the whole string, not just one char in that string. 因为您要设置整个字符串的值,而不仅仅是该字符串中的一个字符。 In the future, please post any compiler errors that you get. 将来,请发布您遇到的所有编译器错误。 After this one change to your code, you should print out the array after calling add_numbers() . 在对代码进行这一更改之后,应调用add_numbers() 之后打印出数组。

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

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