简体   繁体   中英

Printing an array from a struct

When I try to print an array contained in an instance of a struct part of the result is what I'm expecting and other parts seem to be gibberish. What is going on here?

Example output:

$./makevector test

NAME: test 16481592918288327671592918096327670000000000100011809530144490000159291832032767

My code is as follows:

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

static int vec_length = 30;

typedef struct {
  char* name;
  int* vector;
} word_entry;

static word_entry entry_one = {NULL,NULL}; 

void MakeEntry(char* word, word_entry* entry){
 entry->name = word;
 int i;
 int this_vector[vec_length];
 srand(time(NULL));
 for(i=0;i<vec_length;i++){
   this_vector[i] = rand()%2;
 } 
entry->vector = this_vector;
}

int main(int argc, char* argv[]){
  int i;
  MakeEntry(argv[1], &entry_one);
  printf("NAME: %s\n", entry_one.name); 
  for (i=0;i<vec_length;i++){
    printf("%d",entry_one.vector[i]);
  }
  printf("\n");
}

this_vector is a local array to MakeEntry() . When that function ends, that array goes out of scope. So entry->vector is pointing to something invalid and you get undefined behavior.

Mulitple issues here - "this_vector" is local to MakeEntry, and goes out of scope when MakeEntry returns.

Also - I think you want some \\n in your printfs

Finally - what output were you expecting?

Try something along these lines:

void MakeEntry(char* word, word_entry* entry)
{

      int i = 0;
      int *this_vector = calloc(vec_length+1, sizeof(int)); //Allocate an array of             Nelements x sizeof(int)

      entry->name = word;

      srand(time(NULL));

     for(i = 0; i < vec_length; i++)
       this_vector[i] = rand()%2;

      entry->vector = this_vector;
}

You create this_vector on the stack . But then assign it to entry->vector , for use outside the function MakeEntry .

Once MakeEntry returns, this_vector is no longer valid, and is likely the source of your garbage.

void MakeEntry(char* word, word_entry* entry){
    int this_vector[vec_length];
    [...]
    entry->vector = this_vector;
}

In my opinion the problem is that you are printing numbers without any spacing, try with

printf("%d ",entry_one.vector[i]);

so that you can identify all the single ints in your array.

Oh well, read chrisaycock answer, I didn't notice you were assigning the whole vector from a stack allocated variabile. It goes out of scope after the local scope, you should dynamically allocate it:

entry->vector = calloc(vec_length,sizeof(int));
for(i=0;i<vec_length;i++){
  entry->vector[i] = rand()%2;
} 
entry->vector = this_vector;

this_vector is a local array type variable and is lost as soon as the function returns. But still the struct member hold reference to it.

Instead of allocating the array on stack, use malloc or any other dynamic memory allocater. Remember to free it when no longer needed.

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