简体   繁体   中英

How do I correctly assign and print structure pointers in c?

I have the following two structures.

struct string_counter {
  char *string;
  int count;
};


struct container{
  struct string_counter *counters;
  int size;
};

And the following functions.

void add_string(struct *container, char *string){
    struct string_counter *counter_ptr;
    counter_ptr = new_count(string);

    container->size++;
    container->counters = realloc(container->counters, sizeof(struct string_counter)*container->size);
    container->counters[container->size-1] = *counter_ptr;
    print_string_counter(&container->counters[container->size-1]);
}

struct counter *new_count(char *string){
    struct string_counter *counter_ptr;

    counter_ptr = malloc(sizeof(struct string_counter));
    counter_ptr->string = string;
    counter_ptr->count = 1;

    return counter_ptr;
}

In my main function I am looping over several times calling add_string(container_ptr, string) with the same container pointer and a different string each time, attempting to add the new string to the end of the string_counter *counters . Later on in my code when I try printing the contents of the counters using the following two functions:

void print_container(struct container *container_ptr){
    int i;
    for(i = 0; i < container_ptr->size; i++){
         print_string_counter(&container_ptr->counters[i]);
    }
}

void print_string_counter(struct string_counter *counter_ptr){
    printf("%s : %d\n", counter_ptr->string, counter_ptr->count);
}

I get extremely incorrect results such as my output below. But earlier when I printed with :

print_string_counter(&container->counters[container->size-1])

just below :

container->counters[container->size-1] = *counter_ptr;

in my add_string() function, I got the correct results.

我的输出来自<code> print_container </ code>

Initially my container values are

struct_counter = NULL;
int size = 0;

Why is my output using the loop later on different from originally when I printed it?

The main problem is likely that your string_counter struct does nat actually store the strings -- it just stores pointers, which will be point at whatever you pass in. In new_count , if that string you pass in is in a temporary buffer (say the buffer you use to read input in main ), then when that buffer goes away, the pointer in the string_counter will be left dangling and when you later go to print, you'll get whatever garbage happens to be in memory where your original buffer was.

Related (but indpendent) your container struct maintains an array of string_counter objects, not pointers, so when you call add_string , it extends the array AND calls new_count to allocate a new string_counter . It then copies that newly allocated object and leaks it.

So what you likely need is in new_counter to copy the string as well. You can also fix the second problem by having new_count return an object rather than a pointer:

struct counter new_count(char *string){
    struct string_counter counter;

    counter.string = strdup(string);
    counter.count = 1;

    return counter;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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