0

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);
3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by gerard4143
0

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

Edited by gerard4143: n/a

0
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.

0
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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.