Hey guys, I'm just trying to get to grasp with structs and I have written a small test program to print out a persons name and age. Currently I am getting nothing when the program runs. Help :(

#include <stdio.h>

struct person
{
  char *name;
  int age;
};

struct person people[5];

int main( void )
{

  people[0].name = "Bob";
  people[0].age = 45;
  people[1].name = "Steve";
  people[1].age = 32;

  int i;
  for(i = 0; i < 5; i++)
  {
    char *value = people[i].name;
    int value2 = people[i].age;

    printf("%s %d\n", value, value2);
  }    
  return(0);

I just ran your program and got results..What are you compiling/running this on?

Edited 5 Years Ago by gerard4143: n/a

char *value = people[i].name;

is potentially dangerous because you did not initialize / declare a variable for the name member from indices 2 to 4. Due to this, you might get garbage results, or the program could crash.

char *value = people[i].name;

is potentially dangerous because you did not initialize / declare a variable for the name member from indices 2 to 4. Due to this, you might get garbage results, or the program could crash.

Not really, the structure was created in the global space so it will be bitwise zeroed..So you'll have zeros and not garbage.

This question has already been answered. Start a new discussion instead.