0

我在使用以下代码段时遇到了一些问题:

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

我想创建一个结构数组,将结构传递给一个方法,并将更多项目弹出到数组中。到目前为止,我所尝试的一切都以惨败告终,我很难想出办法摆脱这个难题。我可以毫无问题地打印这些值:

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

我希望输出看起来像:

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

请帮忙。我不擅长c,所以要温柔!

编辑:澄清了代码示例以将焦点从错误的区域上移开。

4

3 回答 3

3

这里:

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

的类型somestring[5]char,不是char*。如果你需要一个字符串数组,你需要定义:

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

并根据您的实际目标以某种方式管理这些字符串。

If you want to add a new number into entries then define it with more than 4 items and keep track of valid locations:

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

#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;
}
于 2013-03-15T01:18:33.483 回答
1

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:

void add_number(numb *a, int position) {
        a[position].somestring = "widdley";
}
于 2013-03-15T01:37:26.960 回答
0

You commented out the call to add_numbers(), so of course, the structs in your array won't change. I suspect you did this because you are getting a compiler error. a->somestring[5] = "widdley"; should be 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().

于 2013-03-15T01:20:31.847 回答